From 7ebaa5c5d43d845e58cc79ea2b5349924a52ee5b Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Mon, 2 Oct 2023 20:22:42 +0100 Subject: [PATCH] convert date range filter to client's timezone before filtering orders --- .../admin/reports/_date_range_form.html.haml | 4 ++++ lib/reporting/reports/customers/base.rb | 16 +++++++++++++ spec/lib/reports/customers_report_spec.rb | 23 +++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/app/views/admin/reports/_date_range_form.html.haml b/app/views/admin/reports/_date_range_form.html.haml index cebe2ce8f2a7..d98c9fd6c59c 100644 --- a/app/views/admin/reports/_date_range_form.html.haml +++ b/app/views/admin/reports/_date_range_form.html.haml @@ -8,3 +8,7 @@ %span.range-divider %i.icon-arrow-right = f.text_field "#{field}_lt", :class => 'datetimepicker datepicker-to', :placeholder => t(:stop), data: { controller: "flatpickr", "flatpickr-enable-time-value": true, "flatpickr-default-date-value": "endOfDay" } += hidden_field_tag "time_zone" +:javascript + document.querySelector("input[name='time_zone']").value = Intl.DateTimeFormat().resolvedOptions().timeZone + diff --git a/lib/reporting/reports/customers/base.rb b/lib/reporting/reports/customers/base.rb index a1c2382e557b..c00324388fea 100644 --- a/lib/reporting/reports/customers/base.rb +++ b/lib/reporting/reports/customers/base.rb @@ -55,6 +55,10 @@ def filter_to_completed_at(orders) return orders if min.blank? || max.blank? + if client_time_zone.present? + min = convert_to_client_time_zone(min) + max = convert_to_client_time_zone(max) + end orders.where(completed_at: [min..max]) end @@ -81,6 +85,18 @@ def last_completed_order(orders) def last_completed_order_date(orders) last_completed_order(orders).completed_at&.to_date end + + def convert_to_client_time_zone(datetime) + DateTime.parse(datetime).change(offset: utc_offset) + end + + def client_time_zone + ActiveSupport::TimeZone[params[:time_zone] || ""] + end + + def utc_offset + ActiveSupport::TimeZone[client_time_zone].formatted_offset + end end end end diff --git a/spec/lib/reports/customers_report_spec.rb b/spec/lib/reports/customers_report_spec.rb index 8dddf65a3b4e..b1c24b92bbd8 100644 --- a/spec/lib/reports/customers_report_spec.rb +++ b/spec/lib/reports/customers_report_spec.rb @@ -277,6 +277,29 @@ module Customers allow(subject).to receive(:params).and_return(order_cycle_id: oc1.id) expect(subject.filter(orders)).to eq([order1]) end + + it "filters by date range using client's time zone" do + time_zone = ActiveSupport::TimeZone["Berlin"].formatted_offset + completed_at1 = DateTime.parse("2023/10/03 18:30") + completed_at2 = DateTime.parse("2023/10/03 19:30") + + completed_at1 = completed_at1.change(offset: time_zone) + completed_at2 = completed_at2.change(offset: time_zone) + + o1 = create(:order, completed_at: completed_at1) + o2 = create(:order, completed_at: completed_at2) + + allow(subject).to receive(:params).and_return( + { + q: { + completed_at_gt: "2023/10/03 18:00", + completed_at_lt: "2023/10/03 19:00" + }, + time_zone: "Berlin" + } + ) + expect(subject.filter(orders)).to eq([o1]) + end end end end