Skip to content

Commit

Permalink
Purge unsubmitted journey sessions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rjlynch committed Jul 1, 2024
1 parent 3fd9cbf commit fd5f93c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
8 changes: 4 additions & 4 deletions app/jobs/purge_unsubmitted_claims_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions app/models/journeys/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
32 changes: 26 additions & 6 deletions spec/jobs/purge_unsubmitted_claims_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fd5f93c

Please sign in to comment.