Merge pull request #8690 from tchak/feat-dossier-state-api
feat(public/api): add dossier state api
This commit is contained in:
commit
f991e1f4b8
3 changed files with 99 additions and 6 deletions
|
@ -11,18 +11,43 @@ class API::Public::V1::DossiersController < API::Public::V1::BaseController
|
|||
dossier.build_default_individual
|
||||
if dossier.save
|
||||
dossier.prefill!(PrefillParams.new(dossier, params.to_unsafe_h).to_a)
|
||||
render json: {
|
||||
dossier_url: commencer_url(@procedure.path, prefill_token: dossier.prefill_token),
|
||||
dossier_id: dossier.to_typed_id,
|
||||
dossier_number: dossier.id
|
||||
}, status: :created
|
||||
render json: serialize_dossier(dossier), status: :created
|
||||
else
|
||||
render_bad_request(dossier.errors.full_messages.to_sentence)
|
||||
end
|
||||
end
|
||||
|
||||
def index
|
||||
prefill_token = Array.wrap(params.fetch(:prefill_token, [])).flat_map { _1.split(',') }
|
||||
dossiers = @procedure.dossiers.visible_by_user.prefilled.order(:created_at).where(prefill_token:)
|
||||
if dossiers.present?
|
||||
render json: dossiers.map { serialize_dossier(_1) }
|
||||
else
|
||||
render json: []
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def serialize_dossier(dossier)
|
||||
if dossier.orphan?
|
||||
{
|
||||
dossier_url: commencer_url(@procedure.path, prefill_token: dossier.prefill_token),
|
||||
state: :prefilled
|
||||
}
|
||||
else
|
||||
{
|
||||
state: dossier.state,
|
||||
submitted_at: dossier.depose_at&.iso8601,
|
||||
processed_at: dossier.processed_at&.iso8601
|
||||
}
|
||||
end.merge(
|
||||
dossier_id: dossier.to_typed_id,
|
||||
dossier_number: dossier.id,
|
||||
dossier_prefill_token: dossier.prefill_token
|
||||
).compact
|
||||
end
|
||||
|
||||
def retrieve_procedure
|
||||
@procedure = Procedure.publiees_ou_brouillons.find_by(id: params[:id])
|
||||
render_not_found("procedure", params[:id]) if @procedure.blank?
|
||||
|
|
|
@ -274,7 +274,7 @@ Rails.application.routes.draw do
|
|||
namespace :v1 do
|
||||
resources :demarches, only: [] do
|
||||
member do
|
||||
resources :dossiers, only: :create
|
||||
resources :dossiers, only: [:create, :index]
|
||||
resources :stats, only: :index
|
||||
end
|
||||
end
|
||||
|
|
|
@ -130,6 +130,74 @@ RSpec.describe API::Public::V1::DossiersController, type: :controller do
|
|||
end
|
||||
end
|
||||
|
||||
describe '#index' do
|
||||
let(:procedure) { dossier.procedure }
|
||||
let(:dossier) { create(:dossier, prefilled: true, user: nil) }
|
||||
let(:prefill_token) { dossier.prefill_token }
|
||||
let(:params) { { id: procedure.id, prefill_token: } }
|
||||
|
||||
subject(:create_request) do
|
||||
request.headers["Content-Type"] = "application/json"
|
||||
get :index, params:
|
||||
end
|
||||
|
||||
let(:body) { JSON.parse(response.body).map(&:deep_symbolize_keys) }
|
||||
|
||||
before { create_request }
|
||||
|
||||
context 'not found' do
|
||||
let(:prefill_token) { 'invalid_token' }
|
||||
it 'should respond with and empty array' do
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(body).to eq([])
|
||||
end
|
||||
end
|
||||
|
||||
context 'when dossier prefilled' do
|
||||
it 'should respond with dossier state' do
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(body.first[:state]).to eq('prefilled')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when dossier brouillon' do
|
||||
let(:dossier) { create(:dossier, prefilled: true) }
|
||||
it 'should respond with dossier state' do
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(body.first[:state]).to eq('brouillon')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when dossier en_construction' do
|
||||
let(:dossier) { create(:dossier, :en_construction, prefilled: true) }
|
||||
it 'should respond with dossier state' do
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(body.first[:state]).to eq('en_construction')
|
||||
expect(body.first[:submitted_at]).to eq(dossier.depose_at.iso8601)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with multiple tokens' do
|
||||
let(:dossier) { create(:dossier, prefilled: true, user: nil) }
|
||||
let(:other_dossier) { create(:dossier, prefilled: true, user: nil, procedure:) }
|
||||
let(:prefill_token) { [dossier.prefill_token, other_dossier.prefill_token] }
|
||||
|
||||
it 'should respond with dossiers state' do
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(body.map { _1[:dossier_number] }).to match_array([dossier.id, other_dossier.id])
|
||||
end
|
||||
|
||||
context 'comma separated tokens' do
|
||||
let(:prefill_token) { [dossier.prefill_token, other_dossier.prefill_token].join(',') }
|
||||
|
||||
it 'should respond with dossiers state' do
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(body.map { _1[:dossier_number] }).to match_array([dossier.id, other_dossier.id])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def find_champ_by_stable_id(dossier, stable_id)
|
||||
|
|
Loading…
Reference in a new issue