Skip to content

Commit 6962e42

Browse files
authored
Bmt3/126967 notification model update (#25503)
* BMT3/121448 - add basic client and push service BMT3/121448 - add start of VANotify new push client and send_push method * BMT3/121448 - rubocop fixes * BMT3/121448 - update push notification service * BMT3/121448 - VaNotify service use UuidValidator, spec fixes * BMT3/121448 - replace sentry logging with shared logging * BMT3/121448 - rubocop fixes * BMT3/121448 - add feature flag around VaNotify Client creation * BMT3/121448 - bad merge revert * BMT3/121448 - pr review updates * BMT3/121448 - missing PR comment review updates * BMT3/121448 - api key parsing fix * BMT3/121452 - add push notification model * BMT3/121452 - removing EventBusGatewayPushNotification model and adding to subsequent PR * BMT3/121452 - new event bus push sidekiq jobs * BMT3/121452 - add more error logging * BMT3/121452 - file renaming due to Zeitwerk error * BMT3/121452 - consistent skipped email and push logging * BMT3/125679 - allow for percentage rollout of push notifications * BMT3/124679 - check for bad data prior to feature flag * BMT3/126967 - Event bus notification schema update * BMT3/126967 - revert logging updates * BMT3/126967 - merge conflict fix * BMT3/126967 - revert model updates * BMT3/126967 - model updates * BMT3/126967 - spec updates
1 parent ec00833 commit 6962e42

File tree

5 files changed

+93
-11
lines changed

5 files changed

+93
-11
lines changed

app/models/event_bus_gateway_notification.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
class EventBusGatewayNotification < ApplicationRecord
4-
belongs_to :user_account
4+
belongs_to :user_account, optional: true
55

66
validates :va_notify_id, presence: true
77
validates :template_id, presence: true
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
class EventBusGatewayPushNotification < ApplicationRecord
4-
belongs_to :user_account
4+
belongs_to :user_account, optional: true
55

66
validates :template_id, presence: true
77
end

app/sidekiq/event_bus_gateway/letter_ready_push_job.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,24 @@ def send_push_notification(icn, template_id)
6161
personalisation: {}
6262
)
6363

64-
EventBusGatewayPushNotification.create!(
64+
create_push_notification_record(template_id, icn)
65+
end
66+
67+
def create_push_notification_record(template_id, icn)
68+
notification = EventBusGatewayPushNotification.create(
6569
user_account: user_account(icn),
6670
template_id:
6771
)
72+
73+
return if notification.persisted?
74+
75+
::Rails.logger.warn(
76+
'LetterReadyPushJob notification record failed to save',
77+
{
78+
errors: notification.errors.full_messages,
79+
template_id:
80+
}
81+
)
6882
end
6983

7084
def notify_client

spec/sidekiq/event_bus_gateway/letter_ready_email_job_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,32 @@
157157
end.not_to raise_error
158158
end
159159
end
160+
161+
context 'when user_account is nil' do
162+
before do
163+
allow_any_instance_of(described_class).to receive(:user_account).and_return(nil)
164+
end
165+
166+
it 'successfully creates notification record without user_account' do
167+
job_instance = subject.new
168+
169+
expect do
170+
job_instance.send(:send_email_notification, participant_id, template_id, first_name, mpi_profile.icn)
171+
end.to change(EventBusGatewayNotification, :count).by(1)
172+
173+
notification = EventBusGatewayNotification.last
174+
expect(notification.user_account).to be_nil
175+
expect(notification.template_id).to eq(template_id)
176+
expect(notification.va_notify_id).to eq(notification_id)
177+
end
178+
179+
it 'still sends email successfully' do
180+
job_instance = subject.new
181+
182+
expect(va_notify_service).to receive(:send_email).with(expected_email_args)
183+
job_instance.send(:send_email_notification, participant_id, template_id, first_name, mpi_profile.icn)
184+
end
185+
end
160186
end
161187

162188
describe 'PII protection with AttrPackage' do

spec/sidekiq/event_bus_gateway/letter_ready_push_job_spec.rb

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,31 @@
9999
job_instance.send(:send_push_notification, mpi_profile.icn, template_id)
100100
end.to change(EventBusGatewayPushNotification, :count).by(1)
101101
end
102+
103+
context 'when user_account is nil' do
104+
before do
105+
allow_any_instance_of(described_class).to receive(:user_account).and_return(nil)
106+
end
107+
108+
it 'successfully creates notification record without user_account' do
109+
job_instance = subject.new
110+
111+
expect do
112+
job_instance.send(:send_push_notification, mpi_profile.icn, template_id)
113+
end.to change(EventBusGatewayPushNotification, :count).by(1)
114+
115+
notification = EventBusGatewayPushNotification.last
116+
expect(notification.user_account).to be_nil
117+
expect(notification.template_id).to eq(template_id)
118+
end
119+
120+
it 'still sends push notification successfully' do
121+
job_instance = subject.new
122+
123+
expect(va_notify_service).to receive(:send_push).with(expected_push_args)
124+
job_instance.send(:send_push_notification, mpi_profile.icn, template_id)
125+
end
126+
end
102127
end
103128

104129
describe 'PII protection with AttrPackage' do
@@ -258,19 +283,36 @@
258283
end
259284

260285
context 'when notification creation fails' do
261-
let(:creation_error) { ActiveRecord::RecordInvalid.new }
286+
let(:invalid_notification) do
287+
instance_double(EventBusGatewayPushNotification,
288+
persisted?: false,
289+
errors: double(full_messages: ['Template must exist']))
290+
end
262291

263292
before do
264-
allow(EventBusGatewayPushNotification).to receive(:create!).and_raise(creation_error)
293+
allow(EventBusGatewayPushNotification).to receive(:create).and_return(invalid_notification)
294+
allow(Rails.logger).to receive(:warn)
265295
end
266296

267-
it 'records notification send failure and re-raises error' do
268-
expect_any_instance_of(described_class)
269-
.to receive(:record_notification_send_failure)
270-
.with(creation_error, 'Push')
297+
it 'logs a warning with error details' do
298+
expect(Rails.logger).to receive(:warn).with(
299+
'LetterReadyPushJob notification record failed to save',
300+
{
301+
errors: ['Template must exist'],
302+
template_id:
303+
}
304+
)
271305

272-
expect { subject.new.perform(participant_id, template_id) }
273-
.to raise_error(creation_error)
306+
subject.new.perform(participant_id, template_id)
307+
end
308+
309+
it 'still sends the push notification successfully' do
310+
expect(va_notify_service).to receive(:send_push)
311+
subject.new.perform(participant_id, template_id)
312+
end
313+
314+
it 'does not raise an error' do
315+
expect { subject.new.perform(participant_id, template_id) }.not_to raise_error
274316
end
275317
end
276318
end

0 commit comments

Comments
 (0)