From d1d1deae23a00796a9c3308e87805660374b2f41 Mon Sep 17 00:00:00 2001 From: Iain McNulty Date: Wed, 18 Oct 2023 17:29:44 +0100 Subject: [PATCH] Refactor #recently_updated_application_choice? --- app/models/application_choice.rb | 7 +-- .../recently_updated_application_choice.rb | 26 +++++++-- spec/models/application_choice_spec.rb | 45 +++----------- ...ecently_updated_application_choice_spec.rb | 58 +++++++++++++++++++ 4 files changed, 88 insertions(+), 48 deletions(-) create mode 100644 spec/queries/recently_updated_application_choice_spec.rb diff --git a/app/models/application_choice.rb b/app/models/application_choice.rb index a094eb8d200..41d0970d5a0 100644 --- a/app/models/application_choice.rb +++ b/app/models/application_choice.rb @@ -32,8 +32,6 @@ class ApplicationChoice < ApplicationRecord audited associated_with: :application_form dateable :sent_to_provider, :offered - UPDATED_RECENTLY_DAYS = 40 - # Note that prior to October 2020, we used to have awaiting_references and # application_complete statuses. These will still show up in older audit logs. enum status: { @@ -268,10 +266,7 @@ def supplementary_statuses end def updated_recently_since_submitted? - return false if sent_to_provider_at.blank? - - since = [UPDATED_RECENTLY_DAYS.days.ago, sent_to_provider_at].max - RecentlyUpdatedApplicationChoice.new.call(application_choice: self, since: since).exists? + RecentlyUpdatedApplicationChoice.new(application_choice: self).call end private diff --git a/app/queries/recently_updated_application_choice.rb b/app/queries/recently_updated_application_choice.rb index 3d1a1122c63..4a82507dd6a 100644 --- a/app/queries/recently_updated_application_choice.rb +++ b/app/queries/recently_updated_application_choice.rb @@ -1,19 +1,33 @@ # Find audits for updates to editable sections on the application form class RecentlyUpdatedApplicationChoice - def call(application_choice:, since: application_choice.created_at) + UPDATED_RECENTLY_DAYS = 40 + + def initialize(application_choice:) + @application_choice = application_choice + end + + def call + return false if @application_choice.sent_to_provider_at.blank? + + since = [UPDATED_RECENTLY_DAYS.days.ago, @application_choice.sent_to_provider_at].max + Audited::Audit - .where('audits.created_at >= :since AND audits.action = \'update\'', since: since) - .where(auditable_type: 'ApplicationForm', auditable_id: application_choice.application_form_id) + .where('audits.created_at >= ? AND audits.action = \'update\'', since) + .where(auditable_type: 'ApplicationForm', auditable_id: @application_choice.application_form_id) .where(application_form_audits_filter_sql) - .order('audits.created_at DESC') + .exists? end +private + def application_form_audits_filter_sql - attributes.map do |change| - "jsonb_exists(audited_changes, '#{change}')" + attributes.map do |attribute| + "jsonb_exists(audited_changes, '#{attribute}')" end.join(' OR ') end + attr_reader :application_choice + def attributes [ # Personal Information diff --git a/spec/models/application_choice_spec.rb b/spec/models/application_choice_spec.rb index 64670849ab2..8775fb4f156 100644 --- a/spec/models/application_choice_spec.rb +++ b/spec/models/application_choice_spec.rb @@ -548,51 +548,24 @@ end describe '#updated_recently_since_submitted?' do - let(:choice) do - create(:application_choice, { - created_at: 7.months.ago, - sent_to_provider_at:, - updated_at: edited_at + 1.second, - }) - end - let(:sent_to_provider_at) { 6.months.ago } - before do - create(:application_form_audit, { - created_at: edited_at, - application_choice: choice, - changes: { 'date_of_birth' => %w[01-01-2000 02-01-2000] }, - }) - end - - context 'update_at is before than UPDATED_RECENTLY_DAYS days ago' do - let(:edited_at) { described_class::UPDATED_RECENTLY_DAYS.days.ago + 1 } - - it 'is not recently updated' do - expect(choice).to be_updated_recently_since_submitted - end + allow(RecentlyUpdatedApplicationChoice).to receive(:new).and_return( + instance_double(RecentlyUpdatedApplicationChoice, call: service_response), + ) end - context 'update_at is after than UPDATED_RECENTLY_DAYS days ago' do - let(:edited_at) { described_class::UPDATED_RECENTLY_DAYS.days.ago - 1 } - - it 'is recently updated' do - expect(choice).not_to be_updated_recently_since_submitted - end - end + let(:choice) { build_stubbed(:application_choice) } - context 'false unless sent_to_provider_at' do - let(:edited_at) { described_class::UPDATED_RECENTLY_DAYS.days.ago + 1 } - let(:sent_to_provider_at) { nil } + context 'when the service returns true' do + let(:service_response) { true } it 'is not recently updated' do - expect(choice).not_to be_updated_recently_since_submitted + expect(choice).to be_updated_recently_since_submitted end end - context 'when update is before sent_to_provider_at' do - let(:edited_at) { described_class::UPDATED_RECENTLY_DAYS.days.ago - 2 } - let(:sent_to_provider_at) { described_class::UPDATED_RECENTLY_DAYS.days.ago - 1 } + context 'when the service returns false' do + let(:service_response) { false } it 'is not recently updated' do expect(choice).not_to be_updated_recently_since_submitted diff --git a/spec/queries/recently_updated_application_choice_spec.rb b/spec/queries/recently_updated_application_choice_spec.rb new file mode 100644 index 00000000000..c2f4fb9cbaa --- /dev/null +++ b/spec/queries/recently_updated_application_choice_spec.rb @@ -0,0 +1,58 @@ +require 'rails_helper' + +RSpec.describe RecentlyUpdatedApplicationChoice, :with_audited do + subject(:service) { described_class.new(application_choice:).call } + + describe '#call' do + let(:application_choice) do + create(:application_choice, { + created_at: 7.months.ago, + sent_to_provider_at:, + updated_at: edited_at + 1.second, + }) + end + let(:sent_to_provider_at) { 6.months.ago } + + before do + create(:application_form_audit, { + created_at: edited_at, + application_choice: application_choice, + changes: { 'date_of_birth' => %w[01-01-2000 02-01-2000] }, + }) + end + + context 'update_at is before than UPDATED_RECENTLY_DAYS days ago' do + let(:edited_at) { described_class::UPDATED_RECENTLY_DAYS.days.ago + 1 } + + it 'is not recently updated' do + expect(service).to be(true) + end + end + + context 'update_at is after than UPDATED_RECENTLY_DAYS days ago' do + let(:edited_at) { described_class::UPDATED_RECENTLY_DAYS.days.ago - 1 } + + it 'is recently updated' do + expect(service).to be(false) + end + end + + context 'false unless sent_to_provider_at' do + let(:edited_at) { described_class::UPDATED_RECENTLY_DAYS.days.ago + 1 } + let(:sent_to_provider_at) { nil } + + it 'is not recently updated' do + expect(service).to be(false) + end + end + + context 'when update is before sent_to_provider_at' do + let(:edited_at) { described_class::UPDATED_RECENTLY_DAYS.days.ago - 2 } + let(:sent_to_provider_at) { described_class::UPDATED_RECENTLY_DAYS.days.ago - 1 } + + it 'is not recently updated' do + expect(service).to be(false) + end + end + end +end