Merge pull request #10974 from demarches-simplifiees/remove_empty_brouillon

Tech: ajoute un scope pour détecter les brouillons vides
This commit is contained in:
Colin Darie 2024-11-05 09:32:16 +00:00 committed by GitHub
commit 4e80f16b9f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 57 additions and 0 deletions

View file

@ -0,0 +1,29 @@
# frozen_string_literal: true
module DossierEmptyConcern
extend ActiveSupport::Concern
included do
scope :empty_brouillon, -> (created_at) do
dossiers_ids = Dossier.brouillon.where(created_at:).ids
dossiers_with_value = Dossier.select('id').includes(:champs)
.where.not(champs: { value: nil })
.where(id: dossiers_ids)
dossier_with_geo_areas = Dossier.select('id').includes(champs: :geo_areas)
.where.not(geo_areas: { id: nil })
.where(id: dossiers_ids)
dossier_with_pj = Dossier.select('id')
.joins(champs: :piece_justificative_file_attachments)
.where(id: dossiers_ids)
brouillon
.where.not(id: dossiers_with_value)
.where.not(id: dossier_with_geo_areas)
.where.not(id: dossier_with_pj)
.where(id: dossiers_ids)
end
end
end

View file

@ -12,6 +12,7 @@ class Dossier < ApplicationRecord
include DossierSectionsConcern include DossierSectionsConcern
include DossierStateConcern include DossierStateConcern
include DossierChampsConcern include DossierChampsConcern
include DossierEmptyConcern
enum state: { enum state: {
brouillon: 'brouillon', brouillon: 'brouillon',

View file

@ -0,0 +1,27 @@
# frozen_string_literal: true
RSpec.describe DossierEmptyConcern do
describe 'empty_brouillon' do
let(:types) { [{ type: :text }, { type: :carte }, { type: :piece_justificative }] }
let(:procedure) { create(:procedure, types_de_champ_public: types) }
let!(:empty_brouillon) { create(:dossier, procedure:) }
let!(:empty_en_construction) { create(:dossier, :en_construction, procedure:) }
let!(:value_filled_dossier) { create(:dossier, procedure:) }
let!(:carte_filled_dossier) { create(:dossier, procedure:) }
let!(:pj_filled_dossier) { create(:dossier, procedure:) }
let(:geo_area) { build(:geo_area, :selection_utilisateur, :polygon) }
let(:attachment) { { io: StringIO.new("toto"), filename: "toto.png", content_type: "image/png" } }
subject { Dossier.empty_brouillon(2.days.ago..) }
before do
value_filled_dossier.champs.first.update(value: 'filled')
carte_filled_dossier.champs.second.update(geo_areas: [geo_area])
pj_filled_dossier.champs.third.piece_justificative_file.attach(attachment)
end
it do
is_expected.to eq([empty_brouillon])
end
end
end