Skip to content

Commit

Permalink
chore(dossier): backfill dossier depose traitement revision
Browse files Browse the repository at this point in the history
  • Loading branch information
tchak committed Mar 4, 2025
1 parent b4179fb commit 0485ae1
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module Maintenance
class T20250304BackfillTraitementRevisionIdTask < MaintenanceTasks::Task

Check warning on line 4 in app/tasks/maintenance/t20250304_backfill_traitement_revision_id_task.rb

View check run for this annotation

Codecov / codecov/patch

app/tasks/maintenance/t20250304_backfill_traitement_revision_id_task.rb#L3-L4

Added lines #L3 - L4 were not covered by tests
# Documentation: cette tâche assigne la revision_id qui manque aux traitements des vieux dossiers

include RunnableOnDeployConcern
include StatementsHelpersConcern

Check warning on line 8 in app/tasks/maintenance/t20250304_backfill_traitement_revision_id_task.rb

View check run for this annotation

Codecov / codecov/patch

app/tasks/maintenance/t20250304_backfill_traitement_revision_id_task.rb#L7-L8

Added lines #L7 - L8 were not covered by tests

# 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

Check warning on line 19 in app/tasks/maintenance/t20250304_backfill_traitement_revision_id_task.rb

View check run for this annotation

Codecov / codecov/patch

app/tasks/maintenance/t20250304_backfill_traitement_revision_id_task.rb#L14-L19

Added lines #L14 - L19 were not covered by tests

def process(traitement)
revisions = traitement.dossier
.procedure
.revisions
.where.not(published_at: nil)
.reorder(:published_at)

Check warning on line 26 in app/tasks/maintenance/t20250304_backfill_traitement_revision_id_task.rb

View check run for this annotation

Codecov / codecov/patch

app/tasks/maintenance/t20250304_backfill_traitement_revision_id_task.rb#L21-L26

Added lines #L21 - L26 were not covered by tests

revision = revisions.where(published_at: ..traitement.processed_at).last
traitement.update!(revision:) if revision.present?
end
end
end

Check warning on line 32 in app/tasks/maintenance/t20250304_backfill_traitement_revision_id_task.rb

View check run for this annotation

Codecov / codecov/patch

app/tasks/maintenance/t20250304_backfill_traitement_revision_id_task.rb#L28-L32

Added lines #L28 - L32 were not covered by tests
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

0 comments on commit 0485ae1

Please sign in to comment.