From 6be39c96257aa6709c5cc38ec5e99ff297f43266 Mon Sep 17 00:00:00 2001 From: Mantas Date: Fri, 27 Aug 2021 00:55:38 +0300 Subject: [PATCH] Fixes #2366 - "Warten auf Erinnerung" - Inconsistent timestamps --- app/models/transaction/notification.rb | 4 +- spec/models/transaction/notification_spec.rb | 45 ++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 spec/models/transaction/notification_spec.rb diff --git a/app/models/transaction/notification.rb b/app/models/transaction/notification.rb index dc0df9ac0..7ede5f1fa 100644 --- a/app/models/transaction/notification.rb +++ b/app/models/transaction/notification.rb @@ -125,11 +125,13 @@ class Transaction::Notification identifier = user.login end + already_notified_cutoff = Time.use_zone(Setting.get('timezone_default').presence || 'UTC') { Time.current.beginning_of_day } + already_notified = History.where( history_type_id: History.type_lookup('notification').id, history_object_id: History.object_lookup('Ticket').id, o_id: ticket.id - ).where('created_at > ?', Time.zone.now.beginning_of_day).exists?(['value_to LIKE ?', "%#{identifier}(#{@item[:type]}:%"]) + ).where('created_at > ?', already_notified_cutoff).exists?(['value_to LIKE ?', "%#{identifier}(#{@item[:type]}:%"]) next if already_notified end diff --git a/spec/models/transaction/notification_spec.rb b/spec/models/transaction/notification_spec.rb new file mode 100644 index 000000000..0058c8ec0 --- /dev/null +++ b/spec/models/transaction/notification_spec.rb @@ -0,0 +1,45 @@ +# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/ + +require 'rails_helper' + +RSpec.describe Transaction::Notification, type: :model do + describe 'pending ticket reminder repeats after midnight at selected time zone' do + let(:group) { create(:group) } + let(:user) { create(:agent) } + let(:ticket) { create(:ticket, owner: user, state_name: 'open', pending_time: Time.current) } + + before do + user.groups << group + ticket + + Setting.set('timezone_default', 'America/Santiago') + freeze_time + run(ticket, user, 'reminder_reached') + OnlineNotification.destroy_all + end + + it 'notification not sent at UTC midnight' do + travel_to Time.current.end_of_day + 1.minute + + expect { run(ticket, user, 'reminder_reached') }.not_to change(OnlineNotification, :count) + end + + it 'notification sent at selected time zone midnight' do + travel_to Time.use_zone('America/Santiago') { Time.current.end_of_day + 1.minute } + + expect { run(ticket, user, 'reminder_reached') }.to change(OnlineNotification, :count).by(1) + end + end + + def run(ticket, user, type) + described_class.new( + object: ticket.class.name, + type: type, + object_id: ticket.id, + interface_handle: 'scheduler', + changes: nil, + created_at: Time.current, + user_id: user.id + ).perform + end +end