From 1881b09c6dc9a3c4ec9b077e18a6fb0399824e35 Mon Sep 17 00:00:00 2001 From: DariusPirvulescu Date: Tue, 8 Oct 2024 21:46:02 -0300 Subject: [PATCH] 6026: Improve timezones on org settings sent emails (#6043) * add partial specs for to_user_timezone helper * add to_user_timezone helper * replace emails created default time zone with user's time zone * run linter * fix views rspec * update tests and to_user_timezone helper * change syntax for calling the helper * add specs for user_timezone * split to_user_timezone method * add a happy path test --- app/controllers/concerns/users/time_zone.rb | 14 ++++++ app/views/casa_org/_sent_emails.html.erb | 2 +- .../concerns/users/time_zone_spec.rb | 48 +++++++++++++++++++ spec/views/casa_orgs/edit.html.erb_spec.rb | 3 ++ 4 files changed, 66 insertions(+), 1 deletion(-) diff --git a/app/controllers/concerns/users/time_zone.rb b/app/controllers/concerns/users/time_zone.rb index f0f937a927..6b378be685 100644 --- a/app/controllers/concerns/users/time_zone.rb +++ b/app/controllers/concerns/users/time_zone.rb @@ -4,6 +4,7 @@ module TimeZone included do helper_method :browser_time_zone + helper_method :to_user_timezone end def browser_time_zone @@ -12,5 +13,18 @@ def browser_time_zone rescue TZInfo::UnknownTimezone, TZInfo::InvalidTimezoneIdentifier Time.zone end + + def to_user_timezone(time_date) + return "" if time_date.nil? || (time_date.instance_of?(String) && time_date.empty?) + + time_zone = user_timezone + return time_date.in_time_zone(time_zone) if time_date.respond_to?(:in_time_zone) + + time_date.to_time(time_zone) + end + + def user_timezone + (browser_time_zone && browser_time_zone != Time.zone) ? browser_time_zone : "Eastern Time (US & Canada)" + end end end diff --git a/app/views/casa_org/_sent_emails.html.erb b/app/views/casa_org/_sent_emails.html.erb index b82f166ca7..23106c6b03 100644 --- a/app/views/casa_org/_sent_emails.html.erb +++ b/app/views/casa_org/_sent_emails.html.erb @@ -25,7 +25,7 @@ <%= sent_email.user.display_name %> <<%= sent_email.sent_address %>> - <%= sent_email.created_at.strftime("%l:%M%P %d %b %Y") %> + <%= to_user_timezone(sent_email.created_at).strftime("%l:%M%P %d %b %Y") %> <% end %> diff --git a/spec/controllers/concerns/users/time_zone_spec.rb b/spec/controllers/concerns/users/time_zone_spec.rb index 299f9fa8d4..7fce225868 100644 --- a/spec/controllers/concerns/users/time_zone_spec.rb +++ b/spec/controllers/concerns/users/time_zone_spec.rb @@ -6,6 +6,8 @@ class MockController < ApplicationController RSpec.describe MockController, type: :controller do let(:browser_time_zone) { "America/Los_Angeles" } + let(:default_time_zone) { "Eastern Time (US & Canada)" } + let(:time_date) { Time.zone.now } before do allow(controller).to receive(:cookies).and_return(browser_time_zone: browser_time_zone) end @@ -37,4 +39,50 @@ class MockController < ApplicationController end end end + + describe "#to_user_timezone" do + it "takes a time_date and converts it to user's time zone" do + expected = controller.to_user_timezone(time_date) + returned = time_date.in_time_zone(browser_time_zone) + expect(expected.zone).to eq(returned.zone) + expect(expected.day).to eq(returned.day) + expect(expected.hour).to eq(returned.hour) + end + + context "when invalid param is sent" do + it "returns the empty string for nil param" do + expect(controller.to_user_timezone(nil)).to eq("") + end + + it "returns empty string if empty string param provided" do + expect(controller.to_user_timezone("")).to eq("") + end + + it "returns nil for invalid date string" do + expect(controller.to_user_timezone("invalid-date")).to eq(nil) + end + end + end + + describe "#user_timezone" do + context "when browser time zone has an invalid value" do + before do + allow(controller).to receive(:cookies).and_return(browser_time_zone: "Invalid/Timezone") + end + + it "returns the default time zone" do + expect(controller.user_timezone).to eq(default_time_zone) + end + end + + context "when browser time zone is not set" do + before do + allow(controller).to receive(:cookies).and_return({}) + end + + it "returns the default time zone" do + expect(controller.user_timezone).to eq(default_time_zone) + end + end + end end diff --git a/spec/views/casa_orgs/edit.html.erb_spec.rb b/spec/views/casa_orgs/edit.html.erb_spec.rb index c0a64e1851..3f23238657 100644 --- a/spec/views/casa_orgs/edit.html.erb_spec.rb +++ b/spec/views/casa_orgs/edit.html.erb_spec.rb @@ -115,6 +115,9 @@ organization = build_stubbed(:casa_org) admin = build_stubbed(:casa_admin, casa_org: organization) allow(view).to receive(:current_organization).and_return(organization) + without_partial_double_verification do + allow(view).to receive(:to_user_timezone).and_return(Time.zone.local(2021, 1, 2, 12, 30, 0)) + end sent_email = build_stubbed(:sent_email, user: admin, created_at: Time.zone.local(2021, 1, 2, 12, 30, 0)) assign(:sent_emails, [sent_email])