diff --git a/app/models/ticket.rb b/app/models/ticket.rb index 3fbd1089b..61b45d33b 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -1479,20 +1479,8 @@ result recipients_checked = [] recipients_raw.each do |recipient_email| - skip_user = false users = User.where(email: recipient_email) - users.each do |user| - next if user.preferences[:mail_delivery_failed] != true - next if !user.preferences[:mail_delivery_failed_data] - - till_blocked = ((user.preferences[:mail_delivery_failed_data] - Time.zone.now - 60.days) / 60 / 60 / 24).round - next if till_blocked.positive? - - logger.info "Send no trigger based notification to #{recipient_email} because email is marked as mail_delivery_failed for #{till_blocked} days" - skip_user = true - break - end - next if skip_user + next if users.any? { |user| !trigger_based_notification?(user) } # send notifications only to email addresses next if recipient_email.blank? @@ -1510,7 +1498,7 @@ result next # no usable format found end - recipient_email = "#{$2}@#{$3}" + recipient_email = "#{$2}@#{$3}" # rubocop:disable Lint/OutOfRangeRegexpRef end end @@ -1789,4 +1777,20 @@ result created_by_id: 1, ) end + + def trigger_based_notification?(user) + blocked_in_days = trigger_based_notification_blocked_in_days(user) + return true if blocked_in_days.zero? + + logger.info "Send no trigger based notification to #{user.email} because email is marked as mail_delivery_failed for #{blocked_in_days} day(s)" + false + end + + def trigger_based_notification_blocked_in_days(user) + return 0 if !user.preferences[:mail_delivery_failed] + return 0 if user.preferences[:mail_delivery_failed_data].blank? + + # blocked for 60 full days + (user.preferences[:mail_delivery_failed_data].to_date - Time.zone.now.to_date).to_i + 61 + end end diff --git a/spec/models/ticket_spec.rb b/spec/models/ticket_spec.rb index e47e8a718..a230bf54c 100644 --- a/spec/models/ticket_spec.rb +++ b/spec/models/ticket_spec.rb @@ -516,6 +516,43 @@ RSpec.describe Ticket, type: :model do end end + describe '#trigger_based_notification?' do + let(:ticket) { create(:ticket) } + + context 'with a normal user' do + let(:customer) { create(:customer) } + + it 'send trigger base notification' do + expect(ticket.send(:trigger_based_notification?, customer)).to eq(true) + end + end + + context 'with a permanent failed user' do + + let(:failed_date) { 1.second.ago } + + let(:customer) do + user = create(:customer) + user.preferences.merge!(mail_delivery_failed: true, mail_delivery_failed_data: failed_date) + user.save! + user + end + + it 'send no trigger base notification' do + expect(ticket.send(:trigger_based_notification?, customer)).to eq(false) + end + + context 'with failed date 61 days ago' do + + let(:failed_date) { 61.days.ago } + + it 'send trigger base notification' do + expect(ticket.send(:trigger_based_notification?, customer)).to eq(true) + end + end + end + end + describe '#subject_build' do context 'with default "ticket_hook_position" setting ("right")' do it 'returns the given string followed by a ticket reference (of the form "[Ticket#123]")' do @@ -757,7 +794,7 @@ RSpec.describe Ticket, type: :model do end describe '#pending_time' do - subject(:ticket) { create(:ticket, pending_time: Time.zone.now + 2.days) } + subject(:ticket) { create(:ticket, pending_time: 2.days.from_now) } context 'when #state is updated to any non-"pending" value' do it 'is reset to nil' do