Merge pull request #10139 from mfo/US/fix-missing-champs-task

fix(data): apply fix missing champ via task. FV dossiers are poping way too much at helpdesk
This commit is contained in:
mfo 2024-03-18 17:26:45 +00:00 committed by GitHub
commit aafe6a7bb6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 43 additions and 0 deletions

View file

@ -0,0 +1,24 @@
# frozen_string_literal: true
# bundle exec maintenance_tasks perform Maintenance::FixMissingChampsTask --arguments procedure_ids:id1,id2,id3
module Maintenance
class FixMissingChampsTask < MaintenanceTasks::Task
attribute :procedure_ids, array: true, default: []
def collection
Dossier.joins(:procedure).where(procedure: { id: procedure_ids }).in_batches
end
def process(dossiers)
# rubocop:disable Rails/FindEach
DossierPreloader.new(dossiers).all.each do |dossier|
# rubocop:enable Rails/FindEach
maybe_fixable = [dossier, dossier.editing_forks.first].compact.any? { _1.champs.size < _1.revision.types_de_champ.size }
if maybe_fixable
added_champ_count = DataFixer::DossierChampsMissing.new(dossier:).fix
rake_puts "fixed: #{dossier.id}, adding: #{added_champ_count}"
end
end
end
end
end

View file

@ -0,0 +1,19 @@
# frozen_string_literal: true
require "rails_helper"
module Maintenance
RSpec.describe FixMissingChampsTask do
describe "#process" do
subject(:process) { described_class.process(dossiers) }
let(:procedure) { create(:procedure, types_de_champ_public:) }
let(:types_de_champ_public) { [{ type: :text, libelle: 'l1' }, { type: :text, libelle: 'l2' }] }
let(:dossier_1) { create(:dossier, procedure:) }
let(:dossiers) { [dossier_1] }
it "add missing champs" do
dossier_1.champs.last.destroy
expect { subject }.to change { dossier_1.champs.count }.by(1)
end
end
end
end