Skip to content

Commit

Permalink
Refactor #recently_updated_application_choice?
Browse files Browse the repository at this point in the history
  • Loading branch information
inulty-dfe committed Oct 19, 2023
1 parent 35ae4d3 commit d1d1dea
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 48 deletions.
7 changes: 1 addition & 6 deletions app/models/application_choice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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
Expand Down
26 changes: 20 additions & 6 deletions app/queries/recently_updated_application_choice.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
45 changes: 9 additions & 36 deletions spec/models/application_choice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 58 additions & 0 deletions spec/queries/recently_updated_application_choice_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d1d1dea

Please sign in to comment.