diff --git a/rails_application/app/controllers/application_controller.rb b/rails_application/app/controllers/application_controller.rb index 2a070e7f8..a9e87b938 100644 --- a/rails_application/app/controllers/application_controller.rb +++ b/rails_application/app/controllers/application_controller.rb @@ -1,4 +1,6 @@ class ApplicationController < ActionController::Base + around_action :set_time_zone + def event_store Rails.configuration.event_store end @@ -10,4 +12,10 @@ def command_bus def not_found render file: "#{Rails.root}/public/404.html", layout: false, status: :not_found end + + private + + def set_time_zone(&block) + Time.use_zone(cookies[:timezone], &block) + end end diff --git a/rails_application/app/javascript/controllers/timezone_controller.js b/rails_application/app/javascript/controllers/timezone_controller.js new file mode 100644 index 000000000..819f60f4b --- /dev/null +++ b/rails_application/app/javascript/controllers/timezone_controller.js @@ -0,0 +1,15 @@ +// app/javascript/controllers/timezone_controller.js +import { Controller } from "@hotwired/stimulus" + +export default class extends Controller { + + connect() { + this.detectAndSetTimezone() + } + + detectAndSetTimezone() { + const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone + + document.cookie = `timezone=${timezone}` + } +} diff --git a/rails_application/app/read_models/time_promotions/create_time_promotion.rb b/rails_application/app/read_models/time_promotions/create_time_promotion.rb index f68ba572b..c811a0419 100644 --- a/rails_application/app/read_models/time_promotions/create_time_promotion.rb +++ b/rails_application/app/read_models/time_promotions/create_time_promotion.rb @@ -6,8 +6,8 @@ def call(event) #{time_promotion.label} #{time_promotion.discount} - #{time_promotion.start_time} - #{time_promotion.end_time} + #{time_promotion.start_time.strftime("%Y-%m-%d %H:%M")} + #{time_promotion.end_time.strftime(("%Y-%m-%d %H:%M"))} HTML ) diff --git a/rails_application/app/views/layouts/application.html.erb b/rails_application/app/views/layouts/application.html.erb index 436c3dda7..35888b61b 100644 --- a/rails_application/app/views/layouts/application.html.erb +++ b/rails_application/app/views/layouts/application.html.erb @@ -44,4 +44,5 @@ <%= render 'footer' %> +
diff --git a/rails_application/app/views/time_promotions/index.html.erb b/rails_application/app/views/time_promotions/index.html.erb index 5c848a3c9..1a1a32c2e 100644 --- a/rails_application/app/views/time_promotions/index.html.erb +++ b/rails_application/app/views/time_promotions/index.html.erb @@ -24,8 +24,8 @@ <%= time_promotion.label %> <%= time_promotion.discount %> - <%= time_promotion.start_time %> - <%= time_promotion.end_time %> + <%= time_promotion.start_time.strftime("%Y-%m-%d %H:%M") %> + <%= time_promotion.end_time.strftime("%Y-%m-%d %H:%M") %> <% end %> diff --git a/rails_application/test/integration/time_promotions_test.rb b/rails_application/test/integration/time_promotions_test.rb index cacbb3934..a28d0dfd3 100644 --- a/rails_application/test/integration/time_promotions_test.rb +++ b/rails_application/test/integration/time_promotions_test.rb @@ -3,37 +3,49 @@ class TimePromotionsTest < InMemoryRESIntegrationTestCase def test_happy_path + cookies[:timezone] = "Europe/Warsaw" + post "/time_promotions", params: { label: "Last Minute June 2022", discount: "50", - start_time: "2022-06-30 15:00:00 UTC", - end_time: "2022-07-01 00:00:00 UTC" + start_time: "2022-06-30 15:00", + end_time: "2022-07-01 00:00" } follow_redirect! assert_response :success + time_promotion = TimePromotions::TimePromotion.find_by(label: "Last Minute June 2022") + + assert_equal("2022-06-30 13:00:00 UTC", time_promotion.start_time.to_s) + assert_equal("2022-06-30 22:00:00 UTC", time_promotion.end_time.to_s) + assert_select("p", "Time promotion was successfully created") post "/time_promotions", params: { label: "Black Monday July 2022", discount: "40", - start_time: "2022-07-04 01:00:00 UTC", - end_time: "2022-07-05 00:00:00 UTC" + start_time: "2022-07-04 01:00:00", + end_time: "2022-07-05 00:00:00" } follow_redirect! assert_response :success + time_promotion = TimePromotions::TimePromotion.find_by(label: "Black Monday July 2022") + + assert_equal("2022-07-03 23:00:00 UTC", time_promotion.start_time.to_s) + assert_equal("2022-07-04 22:00:00 UTC", time_promotion.end_time.to_s) + assert_select("p", "Time promotion was successfully created") get "/time_promotions" assert_select("td", "Last Minute June 2022") assert_select("td", "50") - assert_select("td", "2022-06-30 15:00:00 UTC") - assert_select("td", "2022-07-01 00:00:00 UTC") + assert_select("td", "2022-06-30 15:00") + assert_select("td", "2022-07-01 00:00") assert_select("td", "Black Monday July 2022") assert_select("td", "40") - assert_select("td", "2022-07-04 01:00:00 UTC") - assert_select("td", "2022-07-05 00:00:00 UTC") + assert_select("td", "2022-07-04 01:00") + assert_select("td", "2022-07-05 00:00") end end