Skip to content

Commit

Permalink
Add job class to allow for one time in progress reminders (#12459)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanbwright authored Apr 24, 2023
1 parent 76ee2f0 commit 052c5ae
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
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
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
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

0 comments on commit 052c5ae

Please sign in to comment.