2016-01-20 15:48:46 +01:00
|
|
|
class API::V1::DossiersController < APIController
|
2018-09-26 16:49:21 +02:00
|
|
|
before_action :fetch_procedure_and_check_token
|
|
|
|
|
2018-03-08 17:42:12 +01:00
|
|
|
DEFAULT_PAGE_SIZE = 100
|
|
|
|
|
2018-03-16 10:10:43 +01:00
|
|
|
resource_description do
|
|
|
|
description AUTHENTICATION_TOKEN_DESCRIPTION
|
|
|
|
end
|
|
|
|
|
2018-09-05 14:48:42 +02:00
|
|
|
api :GET, '/procedures/:procedure_id/dossiers/', 'Liste de tous les dossiers d\'une démarche'
|
|
|
|
param :procedure_id, Integer, desc: "L'identifiant de la démarche", required: true
|
2018-03-08 17:42:12 +01:00
|
|
|
param :page, String, desc: "Numéro de la page", required: false
|
|
|
|
param :resultats_par_page, String, desc: "Nombre de résultats par page (#{DEFAULT_PAGE_SIZE} par défaut, maximum 1 000)", required: false
|
2016-02-22 19:42:47 +01:00
|
|
|
error code: 401, desc: "Non authorisé"
|
2018-09-05 14:48:42 +02:00
|
|
|
error code: 404, desc: "Démarche inconnue"
|
2016-02-22 19:42:47 +01:00
|
|
|
|
2016-01-20 15:48:46 +01:00
|
|
|
def index
|
2018-09-26 16:49:21 +02:00
|
|
|
dossiers = @procedure.dossiers.state_not_brouillon.page(params[:page]).per(per_page)
|
2016-11-15 09:54:17 +01:00
|
|
|
|
2017-04-14 17:38:08 +02:00
|
|
|
render json: { dossiers: dossiers.map{ |dossier| DossiersSerializer.new(dossier) }, pagination: pagination(dossiers) }, status: 200
|
2017-04-14 17:05:38 +02:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
2016-01-20 15:48:46 +01:00
|
|
|
render json: {}, status: 404
|
|
|
|
end
|
|
|
|
|
2018-09-05 14:48:42 +02:00
|
|
|
api :GET, '/procedures/:procedure_id/dossiers/:id', 'Informations du dossier d\'une démarche'
|
|
|
|
param :procedure_id, Integer, desc: "L'identifiant de la démarche", required: true
|
2016-02-26 14:16:17 +01:00
|
|
|
param :dossier_id, Integer, desc: "L'identifiant du dossier", required: true
|
2016-02-22 19:42:47 +01:00
|
|
|
error code: 401, desc: "Non authorisé"
|
2018-09-05 14:48:42 +02:00
|
|
|
error code: 404, desc: "Démarche ou dossier inconnu"
|
2016-02-22 19:42:47 +01:00
|
|
|
|
2016-01-25 16:08:10 +01:00
|
|
|
def show
|
2018-09-26 16:49:21 +02:00
|
|
|
dossier = @procedure.dossiers.find(params[:id])
|
2016-11-15 09:54:17 +01:00
|
|
|
|
2016-02-19 16:59:18 +01:00
|
|
|
respond_to do |format|
|
2017-04-14 17:38:08 +02:00
|
|
|
format.json { render json: { dossier: DossierSerializer.new(dossier).as_json }, status: 200 }
|
2016-02-19 16:59:18 +01:00
|
|
|
end
|
2017-04-14 17:05:38 +02:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
2016-01-25 16:08:10 +01:00
|
|
|
render json: {}, status: 404
|
|
|
|
end
|
|
|
|
|
|
|
|
def pagination(dossiers)
|
2016-01-20 15:48:46 +01:00
|
|
|
{
|
2018-01-15 19:02:12 +01:00
|
|
|
page: dossiers.current_page,
|
|
|
|
resultats_par_page: dossiers.limit_value,
|
|
|
|
nombre_de_page: dossiers.total_pages
|
2016-01-20 15:48:46 +01:00
|
|
|
}
|
|
|
|
end
|
2017-12-28 19:31:28 +01:00
|
|
|
|
|
|
|
def per_page # inherited value from will_paginate
|
2018-06-29 17:34:35 +02:00
|
|
|
[params[:resultats_par_page]&.to_i || DEFAULT_PAGE_SIZE, 1000].min
|
2017-12-28 19:31:28 +01:00
|
|
|
end
|
2018-09-26 16:49:21 +02:00
|
|
|
|
|
|
|
def fetch_procedure_and_check_token
|
|
|
|
@procedure = Procedure.includes(:administrateur).find(params[:procedure_id])
|
|
|
|
|
|
|
|
if !valid_token_for_administrateur?(@procedure.administrateur)
|
|
|
|
render json: {}, status: :unauthorized
|
|
|
|
end
|
|
|
|
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
render json: {}, status: :not_found
|
|
|
|
end
|
2017-04-04 15:27:04 +02:00
|
|
|
end
|