diff --git a/README.md b/README.md index 98a36ad58..365ed4b36 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,7 @@ Afin de générer la BDD de l'application, il est nécessaire d'éxécuter les c AutoArchiveProcedureJob.set(cron: "* * * * *").perform_later WeeklyOverviewJob.set(cron: "0 8 * * 0").perform_later + AutoReceiveDossiersForProcedureJob.set(cron: "* * * * *").perform_later(procedure_declaratoire_id) ## Exécution des tests (RSpec) diff --git a/app/jobs/auto_receive_dossiers_for_procedure_job.rb b/app/jobs/auto_receive_dossiers_for_procedure_job.rb new file mode 100644 index 000000000..33e4c6083 --- /dev/null +++ b/app/jobs/auto_receive_dossiers_for_procedure_job.rb @@ -0,0 +1,10 @@ +class AutoReceiveDossiersForProcedureJob < ApplicationJob + queue_as :cron + + def perform(procedure_id) + procedure = Procedure.find_by(id: procedure_id) + if procedure + procedure.dossiers.state_nouveaux.update_all(state: "received", received_at: Time.now) + end + end +end diff --git a/app/serializers/dossiers_serializer.rb b/app/serializers/dossiers_serializer.rb index e43f4b63d..34110603e 100644 --- a/app/serializers/dossiers_serializer.rb +++ b/app/serializers/dossiers_serializer.rb @@ -1,4 +1,5 @@ class DossiersSerializer < ActiveModel::Serializer attributes :id, - :updated_at + :updated_at, + :initiated_at end diff --git a/doc/apipie_examples.json b/doc/apipie_examples.json index e88b2a84c..b5a9928b5 100644 --- a/doc/apipie_examples.json +++ b/doc/apipie_examples.json @@ -13,7 +13,8 @@ { "id": 2, "nom_projet": "Demande de subvention dans le cadre d'accompagnement d'enfant à l'étranger", - "updated_at": "2008-09-01T08:05:00.000Z" + "updated_at": "2008-09-01T08:05:00.000Z", + "initiated_at": "2008-09-02T08:05:00.000Z" } ], "pagination": { diff --git a/spec/controllers/api/v1/dossiers_controller_spec.rb b/spec/controllers/api/v1/dossiers_controller_spec.rb index 63c293550..e43343325 100644 --- a/spec/controllers/api/v1/dossiers_controller_spec.rb +++ b/spec/controllers/api/v1/dossiers_controller_spec.rb @@ -25,7 +25,7 @@ describe API::V1::DossiersController do context 'when procedure is found and belongs to admin' do let(:procedure_id) { procedure.id } let(:date_creation) { Time.utc(2008, 9, 1, 10, 5, 0) } - let!(:dossier) { Timecop.freeze(date_creation) { create(:dossier, :with_entreprise, procedure: procedure, state: 'initiated') } } + let!(:dossier) { Timecop.freeze(date_creation) { create(:dossier, :with_entreprise, :initiated, procedure: procedure) } } let(:body) { JSON.parse(retour.body, symbolize_names: true) } it 'return REST code 200', :show_in_doc do @@ -53,7 +53,8 @@ describe API::V1::DossiersController do subject { super().first } it { expect(subject[:id]).to eq(dossier.id) } it { expect(subject[:updated_at]).to eq("2008-09-01T10:05:00.000Z") } - it { expect(subject.keys.size).to eq(2) } + it { expect(subject[:initiated_at]).to eq("2008-09-01T10:06:00.000Z") } + it { expect(subject.keys.size).to eq(3) } end end diff --git a/spec/factories/dossier.rb b/spec/factories/dossier.rb index 036d498ca..924696a81 100644 --- a/spec/factories/dossier.rb +++ b/spec/factories/dossier.rb @@ -73,7 +73,20 @@ FactoryGirl.define do end trait :initiated do - state 'initiated' + after(:create) do |dossier, _evaluator| + dossier.state = 'initiated' + dossier.initiated_at = dossier.created_at + 1.minute + dossier.save! + end + end + + trait :received do + after(:create) do |dossier, _evaluator| + dossier.state = 'received' + dossier.initiated_at = dossier.created_at + 1.minute + dossier.created_at = dossier.created_at + 2.minute + dossier.save! + end end end end diff --git a/spec/jobs/auto_receive_dossiers_for_procedure_job_spec.rb b/spec/jobs/auto_receive_dossiers_for_procedure_job_spec.rb new file mode 100644 index 000000000..b8071f634 --- /dev/null +++ b/spec/jobs/auto_receive_dossiers_for_procedure_job_spec.rb @@ -0,0 +1,34 @@ +require 'rails_helper' + +RSpec.describe AutoReceiveDossiersForProcedureJob, type: :job do + describe "perform" do + let(:date) { Time.utc(2017, 9, 1, 10, 5, 0) } + + before { Timecop.freeze(date) } + + subject { AutoReceiveDossiersForProcedureJob.new.perform(procedure_id) } + + context "with some dossiers" do + let(:nouveau_dossier1) { create(:dossier, :initiated) } + let(:nouveau_dossier2) { create(:dossier, :initiated, procedure: nouveau_dossier1.procedure) } + let(:dossier_recu) { create(:dossier, :received, procedure: nouveau_dossier2.procedure) } + let(:dossier_draft) { create(:dossier, procedure: dossier_recu.procedure) } + let(:procedure_id) { dossier_draft.procedure_id } + + it do + subject + expect(nouveau_dossier1.reload.received?).to be true + expect(nouveau_dossier1.reload.received_at).to eq(date) + + expect(nouveau_dossier2.reload.received?).to be true + expect(nouveau_dossier2.reload.received_at).to eq(date) + + expect(dossier_recu.reload.received?).to be true + expect(dossier_recu.reload.received_at).to eq(date) + + expect(dossier_draft.reload.draft?).to be true + expect(dossier_draft.reload.received_at).to eq(nil) + end + end + end +end