feat(demarche): prefilling stats (#8436)

* force json content type for POST / PATCH / PUT

* add specs about stats dossiers funnel

* new endpoint to render stats about a procedure
This commit is contained in:
Sébastien Carceles 2023-01-20 14:28:02 +01:00 committed by GitHub
parent 60e3ebd7c7
commit 68ddae7382
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 121 additions and 1 deletions

View file

@ -1,7 +1,7 @@
class API::Public::V1::BaseController < APIController
skip_forgery_protection
before_action :check_content_type_is_json
before_action :check_content_type_is_json, if: -> { request.post? || request.patch? || request.put? }
protected

View file

@ -0,0 +1,20 @@
class API::Public::V1::StatsController < API::Public::V1::BaseController
before_action :retrieve_procedure
def index
render json: {
funnel: @procedure.stats_dossiers_funnel.as_json,
processed: @procedure.stats_termines_states.as_json,
processed_by_week: @procedure.stats_termines_by_week.as_json,
processing_time: @procedure.stats_usual_traitement_time.as_json,
processing_time_by_month: @procedure.stats_usual_traitement_time_by_month_in_days.as_json
}
end
private
def retrieve_procedure
@procedure = Procedure.publiees_ou_brouillons.opendata.find_by(id: params[:id])
render_not_found("procedure", params[:id]) if @procedure.blank?
end
end

View file

@ -273,6 +273,7 @@ Rails.application.routes.draw do
resources :demarches, only: [] do
member do
resources :dossiers, only: :create
resources :stats, only: :index
end
end
end

View file

@ -0,0 +1,77 @@
RSpec.describe API::Public::V1::StatsController, type: :controller do
describe '#index' do
let(:params) { { id: procedure.id } }
subject(:index_request) { get :index, params: params }
shared_examples 'the procedure is found' do
before do
create(:dossier, :en_instruction, procedure: procedure)
create(:dossier, :accepte, procedure: procedure)
index_request
end
it { expect(response).to be_successful }
it {
expect(JSON.parse(response.body)).to match({
funnel: procedure.stats_dossiers_funnel.as_json,
processed: procedure.stats_termines_states.as_json,
processed_by_week: procedure.stats_termines_by_week.as_json,
processing_time: procedure.stats_usual_traitement_time.as_json,
processing_time_by_month: procedure.stats_usual_traitement_time_by_month_in_days.as_json
}.with_indifferent_access)
}
end
shared_examples 'the procedure is not found' do
before { index_request }
it { expect(response).to have_http_status(:not_found) }
it { expect(response).to have_failed_with("procedure #{procedure.id} is not found") }
end
context 'when the procedure is found' do
context 'when the procedure is publiee' do
context 'when the procedure is opendata' do
it_behaves_like 'the procedure is found' do
let(:procedure) { create(:procedure, :published, opendata: true) }
end
end
context 'when the procedure is not opendata' do
it_behaves_like 'the procedure is not found' do
let(:procedure) { create(:procedure, :published, opendata: false) }
end
end
end
context 'when the procedure is brouillon' do
context 'when the procedure is opendata' do
it_behaves_like 'the procedure is found' do
let(:procedure) { create(:procedure, :draft, opendata: true) }
end
end
context 'when the procedure is not opendata' do
it_behaves_like 'the procedure is not found' do
let(:procedure) { create(:procedure, :draft, opendata: false) }
end
end
end
context 'when the procedure is not publiee and not brouillon' do
it_behaves_like 'the procedure is not found' do
let(:procedure) { create(:procedure, :closed) }
end
end
end
context 'when the procedure is not found' do
it_behaves_like 'the procedure is not found' do
let(:procedure) { double(Procedure, id: -1) }
end
end
end
end

View file

@ -1,4 +1,26 @@
describe ProcedureStatsConcern do
describe '#stats_dossiers_funnel' do
let(:procedure) { create(:procedure) }
subject(:stats_dossiers_funnel) { procedure.stats_dossiers_funnel }
before do
create_list(:dossier, 2, :brouillon, procedure: procedure)
create(:dossier, :en_instruction, procedure: procedure)
end
it "returns the funnel stats" do
expect(stats_dossiers_funnel).to match(
[
['Démarrés', procedure.dossiers.count],
['Déposés', procedure.dossiers.state_not_brouillon.count],
['Instruction débutée', procedure.dossiers.state_instruction_commencee.count],
['Traités', procedure.dossiers.state_termine.count]
]
)
end
end
describe '#usual_traitement_time_for_recent_dossiers' do
let(:procedure) { create(:procedure) }