feat(attestation_template): assign attestations templates to procedures

This commit is contained in:
Paul Chavard 2022-11-08 12:25:46 +00:00
parent af25411f97
commit 55c5de2c2c
2 changed files with 71 additions and 0 deletions

View file

@ -0,0 +1,38 @@
namespace :after_party do
desc 'Deployment task: assign_attestation_templates_to_procedures'
task assign_attestation_templates_to_procedures: :environment do
puts "Running deploy task 'assign_attestation_templates_to_procedures'"
procedures = Procedure.joins(revisions: :attestation_template, draft_revision: :attestation_template).distinct
progress = ProgressReport.new(procedures.count)
procedures.find_each do |procedure|
draft_revision = procedure.draft_revision
other_revisions = procedure.revisions - [draft_revision]
draft_attestation_template = draft_revision.attestation_template
if draft_attestation_template.present?
other_attestation_templates = other_revisions.map(&:attestation_template).compact - [draft_attestation_template]
AttestationTemplate.transaction do
if other_attestation_templates.present?
AttestationTemplate.where(id: other_attestation_templates.map(&:id)).update_all(procedure_id: nil)
end
AttestationTemplate.where(procedure_id: procedure.id).update_all(procedure_id: nil)
ProcedureRevision
.where(attestation_template_id: draft_attestation_template.id)
.update_all(attestation_template_id: nil)
draft_attestation_template.update_column(:procedure_id, procedure.id)
end
end
progress.inc
end
progress.finish
# Update task as completed. If you remove the line below, the task will
# run with every deploy (or every time you call after_party:run).
AfterParty::TaskRecord
.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp
end
end

View file

@ -0,0 +1,33 @@
describe '20221108114545_assign_attestation_templates_to_procedures' do
let(:rake_task) { Rake::Task['after_party:assign_attestation_templates_to_procedures'] }
let(:procedure) { create(:procedure, :published) }
let(:attestation_template) { create(:attestation_template) }
let(:attestation_template_old) { create(:attestation_template, procedure: procedure) }
let(:procedure2) { create(:procedure, :published) }
let(:attestation_template2) { create(:attestation_template) }
subject(:run_task) do
rake_task.invoke
end
before do
procedure.draft_revision.add_type_de_champ(type_champ: :integer_number, libelle: 'l1')
procedure.publish_revision!
procedure.draft_revision.update!(attestation_template: attestation_template)
procedure.published_revision.update!(attestation_template: attestation_template)
procedure.revisions.first.update!(attestation_template: attestation_template_old)
procedure2.draft_revision.update!(attestation_template: attestation_template2)
procedure2.published_revision.update!(attestation_template: attestation_template2)
end
after { rake_task.reenable }
it "assigns attestation template to procedure" do
expect(procedure.attestation_template).to eq(attestation_template_old)
expect(procedure2.attestation_template).to be_nil
run_task
expect(procedure.reload.attestation_template).to eq(attestation_template)
expect(attestation_template_old.reload.procedure_id).to be_nil
expect(procedure2.reload.attestation_template).to eq(attestation_template2)
end
end