2023-01-03 14:46:10 +01:00
|
|
|
class API::Public::V1::DossiersController < API::Public::V1::BaseController
|
|
|
|
before_action :retrieve_procedure
|
|
|
|
|
|
|
|
def create
|
|
|
|
dossier = Dossier.new(
|
|
|
|
revision: @procedure.active_revision,
|
|
|
|
groupe_instructeur: @procedure.defaut_groupe_instructeur_for_new_dossier,
|
|
|
|
state: Dossier.states.fetch(:brouillon),
|
|
|
|
prefilled: true
|
|
|
|
)
|
|
|
|
dossier.build_default_individual
|
|
|
|
if dossier.save
|
|
|
|
dossier.prefill!(PrefillParams.new(dossier, params.to_unsafe_h).to_a)
|
2023-02-23 21:55:03 +01:00
|
|
|
render json: serialize_dossier(dossier), status: :created
|
2023-01-03 14:46:10 +01:00
|
|
|
else
|
|
|
|
render_bad_request(dossier.errors.full_messages.to_sentence)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-23 21:55:03 +01:00
|
|
|
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
|
|
|
|
|
2023-01-03 14:46:10 +01:00
|
|
|
private
|
|
|
|
|
2023-02-23 21:55:03 +01:00
|
|
|
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
|
|
|
|
|
2023-01-03 14:46:10 +01:00
|
|
|
def retrieve_procedure
|
|
|
|
@procedure = Procedure.publiees_ou_brouillons.find_by(id: params[:id])
|
|
|
|
render_not_found("procedure", params[:id]) if @procedure.blank?
|
|
|
|
end
|
|
|
|
end
|