module Eco module API class UseCases class OozeSamples module Helpers module Filters FILTER_TIME_FORMAT = '%Y-%m-%dT%H:%M:%SZ' def tags_filter(tags, any: true, negate: false) tags = [tags].flatten.compact return nil if tags.empty? key, name = field_key_name(:tags) { "tags": tags, "mode": any ? "any" : "all", "negate": negate, "key": key, "name": name, "type": "tag_filter" } end def date_range_filter(from: nil, to: nil, key: :updated_at) return nil unless from || to key, name = field_key_name(key) { "relstart": "today", "time_zone": "Pacific/Auckland", "relative": false, "key": key, "name": name, "type": "date_filter" }.tap do |out| out.merge!("lbound": to_date_filter(from)) if from out.merge!("ubound": to_date_filter(to)) if to end end def to_date_filter(date) daystart(date).utc.strftime(FILTER_TIME_FORMAT) end def set_time(date, h, m, s); Time.new(date.year, date.month, date.day, h, m, s); end def weeks(num); num * days(7); end def days(num); num * 60 * 60 * 24; end def today; Date.today.to_time; end def sunday(date); date + days(7 - weekday(date)); end def midnight(date); set_time(date, 23, 59, 59); end def daystart(date); set_time(date, 0, 0, 0); end def previous_sunday(date); midnight(sunday(date - weeks(1))); end def this_monday(date); set_time(previous_sunday(as_at_date) + days(1), 0, 0, 0); end def field_key_name(value) case value when :updated_at ["updated_at", "Last updated"] when :created_at ["created_at", "Page created"] when Ecoportal::API::V2::Page::Component [value.ref_backend, value.label] when :tags ["tags", "Location Tags"] else [nil , nil] end end end end end end end end