-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add job class to allow for one time in progress reminders (#12459)
- Loading branch information
1 parent
76ee2f0
commit 052c5ae
Showing
3 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
modules/va_notify/app/models/va_notify/in_progress_reminders_sent.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module VANotify | ||
class InProgressRemindersSent < ApplicationRecord | ||
self.table_name = 'va_notify_in_progress_reminders_sent' | ||
|
||
belongs_to :user_account | ||
end | ||
end |
29 changes: 29 additions & 0 deletions
29
modules/va_notify/app/workers/va_notify/one_time_in_progress_reminder.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'sidekiq' | ||
|
||
module VANotify | ||
class OneTimeInProgressReminder | ||
include Sidekiq::Worker | ||
|
||
attr_reader :user_account_id, :form_name | ||
|
||
def perform(user_account_id, form_name, template_id, personalisation) | ||
@user_account_id = user_account_id | ||
@form_name = form_name | ||
|
||
return if user_already_notified? | ||
|
||
user_account = UserAccount.find(user_account_id) | ||
|
||
InProgressRemindersSent.create!(user_account_id:, form_id: form_name) | ||
VANotify::IcnJob.perform_async(user_account.icn, template_id, personalisation) | ||
end | ||
|
||
private | ||
|
||
def user_already_notified? | ||
InProgressRemindersSent.find_by(user_account_id:, form_id: form_name) | ||
end | ||
end | ||
end |
62 changes: 62 additions & 0 deletions
62
modules/va_notify/spec/workers/one_time_in_progress_reminder_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
require 'sidekiq/testing' | ||
|
||
describe VANotify::OneTimeInProgressReminder, type: :worker do | ||
describe '#perform' do | ||
let(:user_account) { create(:user_account, icn:) } | ||
let(:icn) { 'icn_here' } | ||
let(:form_name) { 'some_form_name' } | ||
|
||
context 'sends reminder' do | ||
it 'delegates to IcnJob to send email' do | ||
template_id = 'some_template_id' | ||
personalisation = { | ||
'some' => 'Fields', | ||
'thing' => 'data' | ||
} | ||
|
||
allow(VANotify::IcnJob).to receive(:perform_async) | ||
|
||
Sidekiq::Testing.inline! do | ||
# only first round should kick off the IcnJob | ||
described_class.perform_async(user_account.id, form_name, template_id, personalisation) | ||
# subsequent invocations should do nothing | ||
described_class.perform_async(user_account.id, form_name, template_id, personalisation) | ||
described_class.perform_async(user_account.id, form_name, template_id, personalisation) | ||
end | ||
|
||
# this would fail if VANotify::IcnJob received multiple invocations | ||
expect(VANotify::IcnJob).to have_received(:perform_async).with('icn_here', 'some_template_id', personalisation) | ||
end | ||
end | ||
|
||
context 'does not send reminder' do | ||
it 'if InProgressRemindersSent model already exists' do | ||
VANotify::InProgressRemindersSent.create(form_id: form_name, user_account:) | ||
|
||
allow(VANotify::IcnJob).to receive(:perform_async) | ||
|
||
Sidekiq::Testing.inline! do | ||
described_class.perform_async(user_account.id, form_name, nil, nil) | ||
end | ||
|
||
expect(VANotify::IcnJob).not_to have_received(:perform_async) | ||
end | ||
|
||
it 'if creating InProgressRemindersSent fails' do | ||
allow(VANotify::InProgressRemindersSent).to receive(:create!).and_raise(ActiveRecord::RecordInvalid) | ||
allow(VANotify::IcnJob).to receive(:perform_async) | ||
|
||
expect do | ||
Sidekiq::Testing.inline! do | ||
described_class.perform_async(user_account.id, form_name, nil, nil) | ||
end | ||
end.to raise_error(ActiveRecord::RecordInvalid) | ||
|
||
expect(VANotify::IcnJob).not_to have_received(:perform_async) | ||
end | ||
end | ||
end | ||
end |