From fd5f93c0d894d20461fe9657607fb4dc1e4988c4 Mon Sep 17 00:00:00 2001 From: Richard Lynch Date: Mon, 1 Jul 2024 11:48:15 +0100 Subject: [PATCH] Purge unsubmitted journey sessions Now we only create a claim once the claimant has completed the journey we need to update the purge job to work on journey sessions rather than claims as all claims are submitted claims. --- app/jobs/purge_unsubmitted_claims_job.rb | 8 ++--- app/models/journeys/session.rb | 8 +++++ .../jobs/purge_unsubmitted_claims_job_spec.rb | 32 +++++++++++++++---- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/app/jobs/purge_unsubmitted_claims_job.rb b/app/jobs/purge_unsubmitted_claims_job.rb index bd8125903f..27404e92e5 100644 --- a/app/jobs/purge_unsubmitted_claims_job.rb +++ b/app/jobs/purge_unsubmitted_claims_job.rb @@ -5,13 +5,13 @@ class PurgeUnsubmittedClaimsJob < CronJob self.cron_expression = "0 0 * * *" def perform - Rails.logger.info "Purging #{old_unsubmitted_claims.count} old and unsubmitted claims from the database" - old_unsubmitted_claims.destroy_all + Rails.logger.info "Purging #{old_unsubmitted_journeys.count} old and unsubmitted journeys from the database" + old_unsubmitted_journeys.destroy_all end private - def old_unsubmitted_claims - Claim.unsubmitted.where("updated_at < ?", 24.hours.ago) + def old_unsubmitted_journeys + Journeys::Session.purgeable end end diff --git a/app/models/journeys/session.rb b/app/models/journeys/session.rb index ed2c1cc5a3..842df00c28 100644 --- a/app/models/journeys/session.rb +++ b/app/models/journeys/session.rb @@ -2,6 +2,8 @@ module Journeys class Session < ApplicationRecord self.abstract_class = true + self.table_name = "journeys_sessions" + has_one :claim, dependent: :nullify, inverse_of: :journey_session, @@ -11,6 +13,12 @@ class Session < ApplicationRecord presence: true, inclusion: {in: Journeys.all_routing_names} + scope :unsubmitted, -> { where.missing(:claim) } + + scope :purgeable, -> do + unsubmitted.where(journeys_sessions: { updated_at: ..24.hours.ago }) + end + def submitted? claim.present? end diff --git a/spec/jobs/purge_unsubmitted_claims_job_spec.rb b/spec/jobs/purge_unsubmitted_claims_job_spec.rb index 2d534777c2..050b7efc0f 100644 --- a/spec/jobs/purge_unsubmitted_claims_job_spec.rb +++ b/spec/jobs/purge_unsubmitted_claims_job_spec.rb @@ -6,17 +6,37 @@ let(:four_hours_ago) { 4.hours.ago } it "destroys any unsubmitted claims that have not been updated in the last 24 hours" do - expired_unsubmitted_claim = create(:claim, updated_at: over_24_hours_ago) - active_unsubmitted_claim = create(:claim, updated_at: four_hours_ago) + submitted_journeys = [] + unsubmitted_fresh_journeys = [] + unsubmitted_expired_journeys = [] - old_submitted_claim = create(:claim, :submitted, updated_at: over_24_hours_ago) + Journeys::JOURNEYS.each do |journey| + submitted_journeys << journey::Session.create!( + journey: journey::ROUTING_NAME, + claim: create(:claim), + updated_at: over_24_hours_ago + ) + + unsubmitted_fresh_journeys << journey::Session.create!( + journey: journey::ROUTING_NAME, + updated_at: four_hours_ago + ) + + unsubmitted_expired_journeys << journey::Session.create!( + journey: journey::ROUTING_NAME, + updated_at: over_24_hours_ago + ) + end PurgeUnsubmittedClaimsJob.new.perform - expect(Claim.exists?(expired_unsubmitted_claim.id)).to eq false + expect(submitted_journeys.each(&:reload)).to all be_persisted + + expect(unsubmitted_fresh_journeys.each(&:reload)).to all be_persisted - expect(Claim.exists?(old_submitted_claim.id)).to eq true - expect(Claim.exists?(active_unsubmitted_claim.id)).to eq true + expect( + Journeys::Session.where(id: unsubmitted_expired_journeys.map(&:id)) + ).to be_empty end end end