db(procedure): add a maintenance task to backfill closed procedures with closing reason

This commit is contained in:
Eric Leroy-Terquem 2024-02-15 11:11:35 +01:00
parent 8c322353c7
commit facb38b539
2 changed files with 53 additions and 0 deletions

View file

@ -0,0 +1,23 @@
# frozen_string_literal: true
module Maintenance
class BackfillClosingReasonInClosedProceduresTask < MaintenanceTasks::Task
def collection
Procedure
.with_discarded
.where(aasm_state: :close)
end
def process(procedure)
if procedure.replaced_by_procedure_id.present?
procedure.update!(closing_reason: Procedure.closing_reasons.fetch(:internal_procedure))
else
procedure.update!(closing_reason: Procedure.closing_reasons.fetch(:other))
end
end
def count
collection.count
end
end
end

View file

@ -0,0 +1,30 @@
# frozen_string_literal: true
require "rails_helper"
module Maintenance
RSpec.describe BackfillClosingReasonInClosedProceduresTask do
describe "#process" do
subject(:process) { described_class.process(procedure) }
context 'with a closed and replaced procedure' do
let(:published_procedure) { create(:procedure, :published) }
let(:procedure) { create(:procedure, :closed, replaced_by_procedure_id: published_procedure.id) }
it 'fills closing_reason with internal_procedure' do
subject
expect(procedure.closing_reason).to eq Procedure.closing_reasons.fetch(:internal_procedure)
end
end
context 'with a closed and not replaced procedure' do
let(:procedure) { create(:procedure, :closed) }
it 'fills closing_reason with other' do
subject
expect(procedure.closing_reason).to eq Procedure.closing_reasons.fetch(:other)
end
end
end
end
end