From eba0c99ae5807b9cbe1ddb9fad68f5f3289c9d62 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 14 Feb 2023 16:47:06 +0100 Subject: [PATCH] (bug) date shift in event creation/update --- CHANGELOG.md | 1 + .../create_availabilities_service.rb | 2 +- app/services/event_service.rb | 4 +- test/integration/events/timezone_test.rb | 54 +++++++++++++++++++ 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 test/integration/events/timezone_test.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index e89ac59d3..eb4cc43a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - Use Time instead of DateTime objects - Fix a bug: wrong times in admin/event monitoring - Fix a bug: daylight saving time is ignored and result in wrong dates and/or times when dealing around the DST day +- Fix a bug: date shift in event creation/update - Fix a bug: unable to run `rails db:seed` when first setup Fab-manager ## v5.6.11 2023 February 07 diff --git a/app/services/availabilities/create_availabilities_service.rb b/app/services/availabilities/create_availabilities_service.rb index ed2ee51fd..88d1a9258 100644 --- a/app/services/availabilities/create_availabilities_service.rb +++ b/app/services/availabilities/create_availabilities_service.rb @@ -5,7 +5,7 @@ class Availabilities::CreateAvailabilitiesService def create(availability, occurrences) occurrences = [] if occurrences.nil? - availability.update_attributes(occurrence_id: availability.id) + availability.update(occurrence_id: availability.id) create_slots(availability) occurrences.each do |o| diff --git a/app/services/event_service.rb b/app/services/event_service.rb index 7a6adcaf6..639353313 100644 --- a/app/services/event_service.rb +++ b/app/services/event_service.rb @@ -33,8 +33,8 @@ class EventService end def date_range(starting, ending, all_day) - start_date = Date.parse(starting[:date]) - end_date = Date.parse(ending[:date]) + start_date = Time.zone.parse(starting[:date]) + end_date = Time.zone.parse(ending[:date]) start_time = starting[:time] ? Time.zone.parse(starting[:time]) : nil end_time = ending[:time] ? Time.zone.parse(ending[:time]) : nil if all_day || start_time.nil? || end_time.nil? diff --git a/test/integration/events/timezone_test.rb b/test/integration/events/timezone_test.rb new file mode 100644 index 000000000..b7b5700ea --- /dev/null +++ b/test/integration/events/timezone_test.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +require 'test_helper' + +module Events; end + +class Events::TimezoneTest < ActionDispatch::IntegrationTest + setup do + admin = User.with_role(:admin).first + login_as(admin, scope: :user) + end + + test 'create an event from a negative timezone' do + # Create a new event + post '/api/events', + params: { + event: { + title: 'Street child skateboard', + description: '

Build your own skateboard for children to ride in the street

', + category_id: 2, + start_date: '2023-06-14T20:00:00.000-04:00', + end_date: '2023-06-14T20:00:00.000-04:00', + start_time: '09:48', + end_time: '11:48', + recurrence: 'none', + recurrence_end_at: '', + nb_total_places: 'NaN', + amount: '35', + advanced_accounting_attributes: { + code: '', + analytical_section: '' + }, + event_image_attributes: { + attachment: fixture_file_upload('/files/event/Skateboard.jpg') + } + } + }.to_json, + headers: default_headers + + # Check response format & status + assert_equal 201, response.status, response.body + assert_equal Mime[:json], response.content_type + + # Check the event was created correctly + event = json_response(response.body) + e = Event.find_by(id: event[:id]) + assert_not_nil e, 'Event was not created in database' + + assert_equal '2023-06-15', e.availability.start_at.to_date.iso8601 + assert_equal '2023-06-15', e.availability.end_at.to_date.iso8601 + assert_equal '09:48', e.availability.start_at.strftime('%R') + assert_equal '11:48', e.availability.end_at.strftime('%R') + end +end