Skip to content

Commit a41ee5b

Browse files
nathanbwrightbalexandr
authored andcommitted
Vanotify migrate simple_forms_api to use v2 email job (#27787)
* Migrate perform_async in modules/simple_forms_api/app/services/simple_forms_api/notification/email.rb to use v2 email job * Migrate perform_async in modules/simple_forms_api/app/services/simple_forms_api/notification/form_upload_email.rb to use v2 email job * Create enqueue_at method and migrate simple_forms to use it with v2 email job * Clarify flipper description * Scope flipper stubbing
1 parent b852426 commit a41ee5b

8 files changed

Lines changed: 368 additions & 47 deletions

File tree

config/features.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3135,6 +3135,18 @@ features:
31353135
description: >-
31363136
If enabled, BurialFormsController will use VANotify::V2::QueueEmailJob instead of
31373137
VANotify::EmailJob for confirmation emails
3138+
va_notify_v2_simple_forms_email:
3139+
actor_type: user
3140+
description: >-
3141+
If enabled, SimpleFormsApi::Notification::Email will use
3142+
VANotify::V2::QueueEmailJob instead of VANotify::EmailJob for both
3143+
immediate sends (#send_email_now) and scheduled sends (#enqueue_email)
3144+
va_notify_v2_simple_forms_upload_email:
3145+
actor_type: user
3146+
description: >-
3147+
If enabled, SimpleFormsApi::Notification::FormUploadEmail will use
3148+
VANotify::V2::QueueEmailJob instead of VANotify::EmailJob for both
3149+
immediate sends (#send_email_now) and scheduled sends (#enqueue_email)
31383150
va_notify_delivery_status_update_job:
31393151
actor_type: user
31403152
description: If enabled, VANotify::DelieveryStatusUpdateJob will be used to query VANotify::Notifications

modules/simple_forms_api/app/services/simple_forms_api/notification/email.rb

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,25 @@ def enqueue_email(at, template_id)
111111
end
112112

113113
def async_job_with_form_data(email, at, template_id)
114-
VANotify::EmailJob.perform_at(
115-
at,
116-
email,
117-
template_id,
118-
get_personalization,
119-
*email_args
120-
)
114+
if Flipper.enabled?(:va_notify_v2_simple_forms_email)
115+
callback_options = email_args.last
116+
VANotify::V2::QueueEmailJob.enqueue_at(
117+
at,
118+
email,
119+
template_id,
120+
get_personalization,
121+
'Settings.vanotify.services.va_gov.api_key',
122+
callback_options
123+
)
124+
else
125+
VANotify::EmailJob.perform_at(
126+
at,
127+
email,
128+
template_id,
129+
get_personalization,
130+
*email_args
131+
)
132+
end
121133
end
122134

123135
def async_job_with_user_account(user_account, at, template_id)
@@ -137,8 +149,18 @@ def async_job_with_user_account(user_account, at, template_id)
137149
def send_email_now(template_id)
138150
email_address = resolve_notification_email || user&.email
139151
personalization = get_personalization
152+
return unless email_address && personalization
140153

141-
if email_address && personalization
154+
if Flipper.enabled?(:va_notify_v2_simple_forms_email)
155+
callback_options = email_args.last
156+
VANotify::V2::QueueEmailJob.enqueue(
157+
email_address,
158+
template_id,
159+
personalization,
160+
'Settings.vanotify.services.va_gov.api_key',
161+
callback_options
162+
)
163+
else
142164
VANotify::EmailJob.perform_async(
143165
email_address,
144166
template_id,

modules/simple_forms_api/app/services/simple_forms_api/notification/form_upload_email.rb

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,43 @@ def check_if_form_is_supported(config)
102102
end
103103

104104
def send_email_now
105-
VANotify::EmailJob.perform_async(
106-
form_data['email'],
107-
template_id,
108-
get_personalization,
109-
*email_args
110-
)
105+
if Flipper.enabled?(:va_notify_v2_simple_forms_upload_email)
106+
VANotify::V2::QueueEmailJob.enqueue(
107+
form_data['email'],
108+
template_id,
109+
get_personalization,
110+
'Settings.vanotify.services.va_gov.api_key',
111+
{ callback_metadata: { notification_type:, form_number:, confirmation_number:, statsd_tags: } }
112+
)
113+
else
114+
VANotify::EmailJob.perform_async(
115+
form_data['email'],
116+
template_id,
117+
get_personalization,
118+
*email_args
119+
)
120+
end
111121
end
112122

113123
def enqueue_email(at)
114-
VANotify::EmailJob.perform_at(
115-
at,
116-
form_data['email'],
117-
template_id,
118-
get_personalization,
119-
*email_args
120-
)
124+
if Flipper.enabled?(:va_notify_v2_simple_forms_upload_email)
125+
VANotify::V2::QueueEmailJob.enqueue_at(
126+
at,
127+
form_data['email'],
128+
template_id,
129+
get_personalization,
130+
'Settings.vanotify.services.va_gov.api_key',
131+
{ callback_metadata: { notification_type:, form_number:, confirmation_number:, statsd_tags: } }
132+
)
133+
else
134+
VANotify::EmailJob.perform_at(
135+
at,
136+
form_data['email'],
137+
template_id,
138+
get_personalization,
139+
*email_args
140+
)
141+
end
121142
end
122143

123144
def email_args

modules/simple_forms_api/spec/requests/simple_forms_api/v1/simple_forms_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,7 @@
867867
before do
868868
sign_in(user)
869869
allow(Flipper).to receive(:enabled?).with(:simple_forms_email_delivery_callback).and_return(true)
870+
allow(Flipper).to receive(:enabled?).with(:va_notify_v2_simple_forms_email).and_return(false)
870871
end
871872

872873
describe '21_4142' do

modules/simple_forms_api/spec/services/notification/email_spec.rb

Lines changed: 117 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
describe SimpleFormsApi::Notification::Email do
1616
let(:lighthouse_updated_at) { Time.current }
1717

18+
before do
19+
allow(Flipper).to receive(:enabled?).with(:va_notify_v2_simple_forms_email).and_return(false)
20+
end
21+
1822
%i[confirmation error received].each do |notification_type|
1923
describe '#initialize' do
2024
context 'when all required arguments are passed in' do
@@ -220,6 +224,7 @@
220224
context 'flipper is on' do
221225
before do
222226
allow(Flipper).to receive(:enabled?).and_return true
227+
allow(Flipper).to receive(:enabled?).with(:va_notify_v2_simple_forms_email).and_return(false)
223228
end
224229

225230
context 'fetching the template id' do
@@ -393,6 +398,70 @@
393398
end
394399
end
395400

401+
context 'when va_notify_v2_simple_forms_email is enabled' do
402+
before do
403+
allow(Flipper).to receive(:enabled?).with(:form21_10210_confirmation_email).and_return(true)
404+
allow(Flipper).to receive(:enabled?).with(:va_notify_v2_simple_forms_email).and_return(true)
405+
allow(Flipper).to receive(:enabled?).with(:simple_forms_email_delivery_callback).and_return(false)
406+
allow(VANotify::V2::QueueEmailJob).to receive(:enqueue)
407+
end
408+
409+
it 'sends email via V2 QueueEmailJob' do
410+
subject = described_class.new(config, notification_type:)
411+
412+
subject.send
413+
414+
expect(VANotify::V2::QueueEmailJob).to have_received(:enqueue).with(
415+
a_string_matching(/@/),
416+
a_string_matching(/\S+/),
417+
a_hash_including('confirmation_number'),
418+
'Settings.vanotify.services.va_gov.api_key',
419+
a_hash_including(:callback_metadata)
420+
)
421+
end
422+
423+
it 'does not call V1 EmailJob' do
424+
allow(VANotify::EmailJob).to receive(:perform_async)
425+
subject = described_class.new(config, notification_type:)
426+
427+
subject.send
428+
429+
expect(VANotify::EmailJob).not_to have_received(:perform_async)
430+
end
431+
end
432+
433+
context 'when va_notify_v2_simple_forms_email is disabled' do
434+
before do
435+
allow(Flipper).to receive(:enabled?).with(:form21_10210_confirmation_email).and_return(true)
436+
allow(Flipper).to receive(:enabled?).with(:va_notify_v2_simple_forms_email).and_return(false)
437+
allow(Flipper).to receive(:enabled?).with(:simple_forms_email_delivery_callback).and_return(false)
438+
allow(VANotify::EmailJob).to receive(:perform_async)
439+
end
440+
441+
it 'sends email via V1 EmailJob' do
442+
subject = described_class.new(config, notification_type:)
443+
444+
subject.send
445+
446+
expect(VANotify::EmailJob).to have_received(:perform_async).with(
447+
a_string_matching(/@/),
448+
a_string_matching(/\S+/),
449+
a_hash_including('confirmation_number'),
450+
a_string_matching(/\S+/),
451+
a_hash_including(:callback_metadata)
452+
)
453+
end
454+
455+
it 'does not call V2 QueueEmailJob' do
456+
allow(VANotify::V2::QueueEmailJob).to receive(:enqueue)
457+
subject = described_class.new(config, notification_type:)
458+
459+
subject.send
460+
461+
expect(VANotify::V2::QueueEmailJob).not_to have_received(:enqueue)
462+
end
463+
end
464+
396465
context 'send at time is specified' do
397466
context 'user_account is passed in' do
398467
let(:confirmation_number) { 'confirmation_number' }
@@ -431,21 +500,56 @@
431500
end
432501

433502
context 'user and user_account are not passed in' do
434-
it 'sends the email at the specified time' do
435-
time = double
436-
allow(VANotify::EmailJob).to receive(:perform_at)
437-
subject = described_class.new(config, notification_type:)
503+
context 'when va_notify_v2_simple_forms_email is disabled' do
504+
it 'sends the email at the specified time via V1' do
505+
time = double
506+
allow(VANotify::EmailJob).to receive(:perform_at)
507+
subject = described_class.new(config, notification_type:)
438508

439-
subject.send(at: time)
509+
subject.send(at: time)
440510

441-
expect(VANotify::EmailJob).to have_received(:perform_at).with(
442-
time,
443-
a_string_matching(/@/),
444-
a_string_matching(/\S+/),
445-
a_hash_including('confirmation_number'),
446-
a_string_matching(/\S+/),
447-
a_hash_including(:callback_metadata)
448-
)
511+
expect(VANotify::EmailJob).to have_received(:perform_at).with(
512+
time,
513+
a_string_matching(/@/),
514+
a_string_matching(/\S+/),
515+
a_hash_including('confirmation_number'),
516+
a_string_matching(/\S+/),
517+
a_hash_including(:callback_metadata)
518+
)
519+
end
520+
end
521+
522+
context 'when va_notify_v2_simple_forms_email is enabled' do
523+
before do
524+
allow(Flipper).to receive(:enabled?).with(:va_notify_v2_simple_forms_email).and_return(true)
525+
allow(VANotify::V2::QueueEmailJob).to receive(:enqueue_at)
526+
end
527+
528+
it 'sends the email at the specified time via V2' do
529+
time = double
530+
subject = described_class.new(config, notification_type:)
531+
532+
subject.send(at: time)
533+
534+
expect(VANotify::V2::QueueEmailJob).to have_received(:enqueue_at).with(
535+
time,
536+
a_string_matching(/@/),
537+
a_string_matching(/\S+/),
538+
a_hash_including('confirmation_number'),
539+
'Settings.vanotify.services.va_gov.api_key',
540+
a_hash_including(:callback_metadata)
541+
)
542+
end
543+
544+
it 'does not call V1 EmailJob' do
545+
time = double
546+
allow(VANotify::EmailJob).to receive(:perform_at)
547+
subject = described_class.new(config, notification_type:)
548+
549+
subject.send(at: time)
550+
551+
expect(VANotify::EmailJob).not_to have_received(:perform_at)
552+
end
449553
end
450554
end
451555
end

0 commit comments

Comments
 (0)