From 42b314bb970dc3c81e252ea9fa0dde73d775fe2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20G=C3=BCnther?= Date: Wed, 13 Oct 2021 14:02:41 +0200 Subject: [PATCH] Allow setting only one date, assumed to be first=last date --- .../user_preference_representer.rb | 4 ++ ...ser_preference_representer_parsing_spec.rb | 54 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/lib/api/v3/user_preferences/user_preference_representer.rb b/lib/api/v3/user_preferences/user_preference_representer.rb index 5c968f0382..ba9d5305d9 100644 --- a/lib/api/v3/user_preferences/user_preference_representer.rb +++ b/lib/api/v3/user_preferences/user_preference_representer.rb @@ -86,6 +86,10 @@ module API end, setter: ->(fragment:, **) do self.pause_reminders = fragment.transform_keys(&:underscore) + + if pause_reminders['last_day'].blank? && pause_reminders['first_day'] + pause_reminders['last_day'] = pause_reminders['first_day'] + end end property :workdays diff --git a/spec/lib/api/v3/user_preferences/user_preference_representer_parsing_spec.rb b/spec/lib/api/v3/user_preferences/user_preference_representer_parsing_spec.rb index 62723c2164..6c26e7d746 100644 --- a/spec/lib/api/v3/user_preferences/user_preference_representer_parsing_spec.rb +++ b/spec/lib/api/v3/user_preferences/user_preference_representer_parsing_spec.rb @@ -96,4 +96,58 @@ describe ::API::V3::UserPreferences::UserPreferenceRepresenter, }) end end + + describe 'pause_reminders' do + let(:request_body) do + { + "pauseReminders" => { + "enabled" => true, + "firstDay" => first_day, + "lastDay" => last_day + } + } + end + + context 'with all set' do + let(:first_day) { '2021-10-10' } + let(:last_day) { '2021-10-20' } + + it 'sets both dates' do + expect(parsed.pause_reminders) + .to eql({ + "enabled" => true, + "first_day" => first_day, + "last_day" => last_day + }) + end + end + + context 'with first only set' do + let(:first_day) { '2021-10-10' } + let(:last_day) { nil } + + it 'uses the first day for the last day' do + expect(parsed.pause_reminders) + .to eql({ + "enabled" => true, + "first_day" => first_day, + "last_day" => first_day + }) + end + end + + context 'with last only set' do + let(:first_day) { nil } + let(:last_day) { '2021-10-10' } + + it 'uses the first day for the last day' do + expect(parsed.pause_reminders) + .to eql({ + "enabled" => true, + "first_day" => nil, + "last_day" => last_day + }) + end + end + end end