-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(dossier): backfill dossier depose traitement revision
- Loading branch information
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
app/tasks/maintenance/t20250304_backfill_traitement_revision_id_task.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
module Maintenance | ||
class T20250304BackfillTraitementRevisionIdTask < MaintenanceTasks::Task | ||
# Documentation: cette tâche assigne la revision_id qui manque aux traitements des vieux dossiers | ||
|
||
include RunnableOnDeployConcern | ||
include StatementsHelpersConcern | ||
|
||
# Uncomment only if this task MUST run imperatively on its first deployment. | ||
# If possible, leave commented for manual execution later. | ||
# run_on_first_deploy | ||
|
||
def collection | ||
Traitement.joins(dossier: :procedure) | ||
.where(state: Dossier.states.fetch(:en_construction), revision_id: nil) | ||
.where('traitements.processed_at = dossiers.depose_at') | ||
.order(:id) | ||
end | ||
|
||
def process(traitement) | ||
revisions = traitement.dossier | ||
.procedure | ||
.revisions | ||
.where.not(published_at: nil) | ||
.reorder(:published_at) | ||
|
||
revision = revisions.where(published_at: ..traitement.processed_at).last | ||
traitement.update!(revision:) if revision.present? | ||
end | ||
end | ||
end | ||
47 changes: 47 additions & 0 deletions
47
spec/tasks/maintenance/t20250304_backfill_traitement_revision_id_task_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
module Maintenance | ||
RSpec.describe T20250304BackfillTraitementRevisionIdTask do | ||
let(:dossiers) { create_list(:dossier, 2, :en_construction) } | ||
let(:dossier) { dossiers.first } | ||
let(:traitement) { dossier.traitements.first } | ||
let(:last_revision) { dossier.procedure.revisions.first } | ||
let(:previous_revision) { dossier.procedure.revisions.second } | ||
|
||
before do | ||
traitement.update(revision_id: nil) | ||
end | ||
|
||
describe "#process" do | ||
subject(:process) { described_class.process(traitement) } | ||
|
||
context "when dossier depose on last revision" do | ||
before do | ||
last_revision.update(published_at: traitement.processed_at - 1.minute) | ||
previous_revision.update(published_at: traitement.processed_at - 2.minutes) | ||
end | ||
|
||
it { expect { process }.to change { traitement.reload.revision }.from(nil).to(last_revision) } | ||
end | ||
|
||
context "when dossier depose on previous revision" do | ||
before do | ||
last_revision.update(published_at: traitement.processed_at + 1.minute) | ||
previous_revision.update(published_at: traitement.processed_at - 1.minute) | ||
end | ||
|
||
it { expect { process }.to change { traitement.reload.revision }.from(nil).to(previous_revision) } | ||
end | ||
end | ||
|
||
describe "#collection" do | ||
subject(:collection) { described_class.collection } | ||
|
||
it "returns the correct collection" do | ||
expect(collection.size).to eq(1) | ||
end | ||
end | ||
end | ||
end |