use traitement model

when a dossier is terminated (accepte, refuse or classe_sans_suite),
we store now `processed_at` and `motivation` in a traitement instance
This commit is contained in:
Christophe Robillard 2020-07-02 11:02:50 +02:00
parent f631acd118
commit a072d35211
14 changed files with 107 additions and 29 deletions

View file

@ -689,6 +689,7 @@ Rails/CreateTableWithTimestamps:
- db/migrate/2016*.rb
- db/migrate/2017*.rb
- db/migrate/2018*.rb
- db/migrate/20200630140356_create_traitements.rb
Rails/Date:
Enabled: false

View file

@ -42,6 +42,7 @@ class Dossier < ApplicationRecord
has_many :followers_instructeurs, through: :follows, source: :instructeur
has_many :previous_followers_instructeurs, -> { distinct }, through: :previous_follows, source: :instructeur
has_many :avis, inverse_of: :dossier, dependent: :destroy
has_many :traitements, -> { order(:processed_at) }, inverse_of: :dossier, dependent: :destroy
has_many :dossier_operation_logs, -> { order(:created_at) }, dependent: :nullify, inverse_of: :dossier
@ -293,6 +294,16 @@ class Dossier < ApplicationRecord
validates :individual, presence: true, if: -> { procedure.for_individual? }
validates :groupe_instructeur, presence: true
def motivation
return nil if !termine?
traitements.any? ? traitements.last.motivation : read_attribute(:motivation)
end
def processed_at
return nil if !termine?
traitements.any? ? traitements.last.processed_at : read_attribute(:processed_at)
end
def update_search_terms
self.search_terms = [
user&.email,
@ -529,8 +540,6 @@ class Dossier < ApplicationRecord
def after_repasser_en_instruction(instructeur)
self.archived = false
self.processed_at = nil
self.motivation = nil
self.en_instruction_at = Time.zone.now
attestation&.destroy
@ -540,14 +549,12 @@ class Dossier < ApplicationRecord
end
def after_accepter(instructeur, motivation, justificatif = nil)
self.motivation = motivation
self.traitements.build(state: Dossier.states.fetch(:accepte), instructeur: instructeur, motivation: motivation, processed_at: Time.zone.now)
if justificatif
self.justificatif_motivation.attach(justificatif)
end
self.processed_at = Time.zone.now
if attestation.nil?
self.attestation = build_attestation
end
@ -558,39 +565,37 @@ class Dossier < ApplicationRecord
end
def after_accepter_automatiquement
self.traitements.build(state: Dossier.states.fetch(:accepte), instructeur: nil, motivation: nil, processed_at: Time.zone.now)
self.en_instruction_at ||= Time.zone.now
if attestation.nil?
self.attestation = build_attestation
end
self.processed_at = Time.zone.now
save!
NotificationMailer.send_closed_notification(self).deliver_later
log_automatic_dossier_operation(:accepter, self)
end
def after_refuser(instructeur, motivation, justificatif = nil)
self.motivation = motivation
self.traitements.build(state: Dossier.states.fetch(:refuse), instructeur: instructeur, motivation: motivation, processed_at: Time.zone.now)
if justificatif
self.justificatif_motivation.attach(justificatif)
end
self.processed_at = Time.zone.now
save!
NotificationMailer.send_refused_notification(self).deliver_later
log_dossier_operation(instructeur, :refuser, self)
end
def after_classer_sans_suite(instructeur, motivation, justificatif = nil)
self.motivation = motivation
self.traitements.build(state: Dossier.states.fetch(:sans_suite), instructeur: instructeur, motivation: motivation, processed_at: Time.zone.now)
if justificatif
self.justificatif_motivation.attach(justificatif)
end
self.processed_at = Time.zone.now
save!
NotificationMailer.send_without_continuation_notification(self).deliver_later
log_dossier_operation(instructeur, :classer_sans_suite, self)

View file

@ -161,7 +161,7 @@ class Instructeur < ApplicationRecord
h = {
nb_en_construction: groupe.dossiers.en_construction.count,
nb_en_instruction: groupe.dossiers.en_instruction.count,
nb_accepted: groupe.dossiers.accepte.where(processed_at: Time.zone.yesterday.beginning_of_day..Time.zone.yesterday.end_of_day).count,
nb_accepted: Traitement.where(dossier: groupe.dossiers.accepte, processed_at: Time.zone.yesterday.beginning_of_day..Time.zone.yesterday.end_of_day).count,
nb_notification: notifications_for_procedure(procedure, :not_archived).count
}

4
app/models/traitement.rb Normal file
View file

@ -0,0 +1,4 @@
class Traitement < ApplicationRecord
belongs_to :dossier
belongs_to :instructeur
end

View file

@ -156,7 +156,7 @@ def add_etats_dossier(pdf, dossier)
if dossier.en_instruction_at.present?
format_in_2_columns(pdf, "En instruction le", try_format_date(dossier.en_instruction_at))
end
if dossier.processed_at?.present?
if dossier.processed_at.present?
format_in_2_columns(pdf, "Décision le", try_format_date(dossier.processed_at))
end

View file

@ -0,0 +1,11 @@
class CreateTraitements < ActiveRecord::Migration[5.2]
def change
create_table :traitements do |t|
t.references :dossier, foreign_key: true
t.references :instructeur, foreign_key: true
t.string :motivation
t.string :state
t.timestamp :processed_at
end
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_06_11_122406) do
ActiveRecord::Schema.define(version: 2020_06_30_140356) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -560,6 +560,16 @@ ActiveRecord::Schema.define(version: 2020_06_11_122406) do
t.string "version", null: false
end
create_table "traitements", force: :cascade do |t|
t.bigint "dossier_id"
t.bigint "instructeur_id"
t.string "motivation"
t.string "state"
t.datetime "processed_at"
t.index ["dossier_id"], name: "index_traitements_on_dossier_id"
t.index ["instructeur_id"], name: "index_traitements_on_instructeur_id"
end
create_table "trusted_device_tokens", force: :cascade do |t|
t.string "token", null: false
t.bigint "instructeur_id"
@ -660,6 +670,8 @@ ActiveRecord::Schema.define(version: 2020_06_11_122406) do
add_foreign_key "received_mails", "procedures"
add_foreign_key "refused_mails", "procedures"
add_foreign_key "services", "administrateurs"
add_foreign_key "traitements", "dossiers"
add_foreign_key "traitements", "instructeurs"
add_foreign_key "trusted_device_tokens", "instructeurs"
add_foreign_key "types_de_champ", "types_de_champ", column: "parent_id"
add_foreign_key "users", "administrateurs"

View file

@ -0,0 +1,18 @@
namespace :after_party do
desc 'Deployment task: add_traitements_from_dossiers'
task add_traitements_from_dossiers: :environment do
puts "Running deploy task 'add_traitements_from_dossiers'"
dossiers_termines = Dossier.state_termine
progress = ProgressReport.new(dossiers_termines.count)
dossiers_termines.find_each do |dossier|
dossier.traitements.create!(state: dossier.state, motivation: dossier.motivation, processed_at: dossier.processed_at)
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: '20200630154829'
end
end

View file

@ -127,11 +127,15 @@ FactoryBot.define do
end
trait :accepte do
after(:create) do |dossier, _evaluator|
transient do
motivation { nil }
end
after(:create) do |dossier, evaluator|
dossier.state = Dossier.states.fetch(:accepte)
dossier.en_construction_at ||= dossier.created_at + 1.minute
dossier.en_instruction_at ||= dossier.en_construction_at + 1.minute
dossier.processed_at ||= dossier.en_instruction_at + 1.minute
dossier.traitements.build(state: Dossier.states.fetch(:accepte), processed_at: dossier.en_instruction_at + 1.minute, motivation: evaluator.motivation)
dossier.save!
end
end
@ -141,7 +145,7 @@ FactoryBot.define do
dossier.state = Dossier.states.fetch(:refuse)
dossier.en_construction_at ||= dossier.created_at + 1.minute
dossier.en_instruction_at ||= dossier.en_construction_at + 1.minute
dossier.processed_at ||= dossier.en_instruction_at + 1.minute
dossier.traitements.build(state: Dossier.states.fetch(:refuse), processed_at: dossier.en_instruction_at + 1.minute)
dossier.save!
end
end
@ -151,14 +155,14 @@ FactoryBot.define do
dossier.state = Dossier.states.fetch(:sans_suite)
dossier.en_construction_at ||= dossier.created_at + 1.minute
dossier.en_instruction_at ||= dossier.en_construction_at + 1.minute
dossier.processed_at ||= dossier.en_instruction_at + 1.minute
dossier.traitements.build(state: Dossier.states.fetch(:sans_suite), processed_at: dossier.en_instruction_at + 1.minute)
dossier.save!
end
end
trait :with_motivation do
after(:create) do |dossier, _evaluator|
dossier.motivation = case dossier.state
motivation = case dossier.state
when Dossier.states.fetch(:refuse)
'Lentreprise concernée nest pas agréée.'
when Dossier.states.fetch(:sans_suite)
@ -166,6 +170,7 @@ FactoryBot.define do
else
'Vous avez validé les conditions.'
end
dossier.traitements.last.update!(motivation: motivation)
end
end

View file

@ -35,6 +35,7 @@ describe TagsSubstitutionConcern, type: :model do
let(:individual) { nil }
let(:etablissement) { create(:etablissement) }
let!(:dossier) { create(:dossier, procedure: procedure, individual: individual, etablissement: etablissement) }
let(:instructeur) { create(:instructeur) }
before { Timecop.freeze(Time.zone.now) }
@ -242,7 +243,7 @@ describe TagsSubstitutionConcern, type: :model do
end
context 'when the dossier has a motivation' do
let(:dossier) { create(:dossier, motivation: 'motivation') }
let(:dossier) { create(:dossier, :accepte, motivation: 'motivation') }
context 'and the template has some dossier tags' do
let(:template) { '--motivation-- --numéro du dossier--' }
@ -318,9 +319,13 @@ describe TagsSubstitutionConcern, type: :model do
context "when using a date tag" do
before do
dossier.en_construction_at = Time.zone.local(2001, 2, 3)
dossier.en_instruction_at = Time.zone.local(2004, 5, 6)
dossier.processed_at = Time.zone.local(2007, 8, 9)
Timecop.freeze(Time.zone.local(2001, 2, 3))
dossier.passer_en_construction!
Timecop.freeze(Time.zone.local(2004, 5, 6))
dossier.passer_en_instruction!(instructeur)
Timecop.freeze(Time.zone.local(2007, 8, 9))
dossier.accepter!(instructeur, nil, nil)
Timecop.return
end
context "with date de dépôt" do

View file

@ -209,8 +209,17 @@ describe Dossier do
let(:date1) { 1.day.ago }
let(:date2) { 1.hour.ago }
let(:date3) { 1.minute.ago }
let(:dossier) { create(:dossier, :with_entreprise, user: user, procedure: procedure, en_construction_at: date1, en_instruction_at: date2, processed_at: date3, motivation: "Motivation") }
let!(:follow) { create(:follow, instructeur: instructeur, dossier: dossier) }
let(:dossier) do
d = create(:dossier, :with_entreprise, user: user, procedure: procedure)
Timecop.freeze(date1)
d.passer_en_construction!
Timecop.freeze(date2)
d.passer_en_instruction!(instructeur)
Timecop.freeze(date3)
d.accepter!(instructeur, "Motivation", nil)
Timecop.return
d
end
describe "followers_instructeurs" do
let(:non_following_instructeur) { create(:instructeur) }
@ -399,8 +408,8 @@ describe Dossier do
end
it { expect(dossier.state).to eq(Dossier.states.fetch(:accepte)) }
it { expect(dossier.traitements.last.processed_at).to eq(beginning_of_day) }
it { expect(dossier.processed_at).to eq(beginning_of_day) }
end
context 'when dossier is refuse' do
@ -801,7 +810,7 @@ describe Dossier do
dossier.procedure.update_column(:web_hook_url, '/webhook.json')
expect {
dossier.update_column(:motivation, 'bonjour')
dossier.update_column(:search_terms, 'bonjour')
}.to_not have_enqueued_job(WebHookJob)
expect {
@ -809,7 +818,7 @@ describe Dossier do
}.to have_enqueued_job(WebHookJob)
expect {
dossier.update_column(:motivation, 'bonjour2')
dossier.update_column(:search_terms, 'bonjour2')
}.to_not have_enqueued_job(WebHookJob)
expect {
@ -904,8 +913,11 @@ describe Dossier do
after { Timecop.return }
it { expect(dossier.traitements.last.motivation).to eq('motivation') }
it { expect(dossier.motivation).to eq('motivation') }
it { expect(dossier.traitements.last.instructeur).to eq(instructeur) }
it { expect(dossier.en_instruction_at).to eq(dossier.en_instruction_at) }
it { expect(dossier.traitements.last.processed_at).to eq(now) }
it { expect(dossier.processed_at).to eq(now) }
it { expect(dossier.state).to eq('accepte') }
it { expect(last_operation.operation).to eq('accepter') }

View file

@ -478,7 +478,7 @@ describe Instructeur, type: :model do
before do
procedure_to_assign.update(declarative_with_state: "accepte")
DeclarativeProceduresJob.new.perform
dossier.update(processed_at: Time.zone.yesterday.beginning_of_day)
dossier.traitements.last.update(processed_at: Time.zone.yesterday.beginning_of_day)
dossier.reload
end

View file

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe Traitement, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

View file

@ -66,7 +66,7 @@ describe NotificationService do
before do
procedure.update(declarative_with_state: "accepte")
DeclarativeProceduresJob.new.perform
dossier.update(processed_at: Time.zone.yesterday.beginning_of_day)
dossier.traitements.last.update!(processed_at: Time.zone.yesterday.beginning_of_day)
dossier.reload
end