From 69f30c76b169e31d83292904d27e357220aab7ba Mon Sep 17 00:00:00 2001 From: Antoine Girard Date: Mon, 25 Nov 2024 15:40:32 +0100 Subject: [PATCH] Review --- .../api}/agenda/absences_controller.rb | 2 +- .../agenda/plage_ouvertures_controller.rb | 2 +- .../api}/agenda/rdvs_controller.rb | 2 +- app/controllers/admin/api/base_controller.rb | 22 +++++++++++ .../api/internal/base_controller.rb | 39 ------------------- app/views/admin/agent_agendas/show.html.slim | 2 +- .../api}/agenda/absences/index.json.jb | 0 .../agenda/plage_ouvertures/index.json.jb | 0 .../api}/agenda/rdvs/_rdv.json.jb | 0 .../agenda/rdvs/_rdv_without_details.json.jb | 0 .../api}/agenda/rdvs/index.json.jb | 0 config/routes.rb | 7 ++++ config/routes/api.rb | 10 ----- 13 files changed, 33 insertions(+), 53 deletions(-) rename app/controllers/{api/internal/admin => admin/api}/agenda/absences_controller.rb (79%) rename app/controllers/{api/internal/admin => admin/api}/agenda/plage_ouvertures_controller.rb (85%) rename app/controllers/{api/internal/admin => admin/api}/agenda/rdvs_controller.rb (92%) create mode 100644 app/controllers/admin/api/base_controller.rb delete mode 100644 app/controllers/api/internal/base_controller.rb rename app/views/{api/internal/admin => admin/api}/agenda/absences/index.json.jb (100%) rename app/views/{api/internal/admin => admin/api}/agenda/plage_ouvertures/index.json.jb (100%) rename app/views/{api/internal/admin => admin/api}/agenda/rdvs/_rdv.json.jb (100%) rename app/views/{api/internal/admin => admin/api}/agenda/rdvs/_rdv_without_details.json.jb (100%) rename app/views/{api/internal/admin => admin/api}/agenda/rdvs/index.json.jb (100%) diff --git a/app/controllers/api/internal/admin/agenda/absences_controller.rb b/app/controllers/admin/api/agenda/absences_controller.rb similarity index 79% rename from app/controllers/api/internal/admin/agenda/absences_controller.rb rename to app/controllers/admin/api/agenda/absences_controller.rb index de77b6ba0f..77cefacbd7 100644 --- a/app/controllers/api/internal/admin/agenda/absences_controller.rb +++ b/app/controllers/admin/api/agenda/absences_controller.rb @@ -1,4 +1,4 @@ -class Api::Internal::Admin::Agenda::AbsencesController < Api::Internal::BaseController +class Admin::Api::Agenda::AbsencesController < Admin::Api::BaseController def index agent = Agent.find(params[:agent_id]) @organisation = Organisation.find(params[:organisation_id]) diff --git a/app/controllers/api/internal/admin/agenda/plage_ouvertures_controller.rb b/app/controllers/admin/api/agenda/plage_ouvertures_controller.rb similarity index 85% rename from app/controllers/api/internal/admin/agenda/plage_ouvertures_controller.rb rename to app/controllers/admin/api/agenda/plage_ouvertures_controller.rb index 19f557b942..66b2460615 100644 --- a/app/controllers/api/internal/admin/agenda/plage_ouvertures_controller.rb +++ b/app/controllers/admin/api/agenda/plage_ouvertures_controller.rb @@ -1,4 +1,4 @@ -class Api::Internal::Admin::Agenda::PlageOuverturesController < Api::Internal::BaseController +class Admin::Api::Agenda::PlageOuverturesController < Admin::Api::BaseController def index @agent = Agent.find(params[:agent_id]) @organisation = Organisation.find(params[:organisation_id]) diff --git a/app/controllers/api/internal/admin/agenda/rdvs_controller.rb b/app/controllers/admin/api/agenda/rdvs_controller.rb similarity index 92% rename from app/controllers/api/internal/admin/agenda/rdvs_controller.rb rename to app/controllers/admin/api/agenda/rdvs_controller.rb index 614d1eba50..617cceac68 100644 --- a/app/controllers/api/internal/admin/agenda/rdvs_controller.rb +++ b/app/controllers/admin/api/agenda/rdvs_controller.rb @@ -1,4 +1,4 @@ -class Api::Internal::Admin::Agenda::RdvsController < Api::Internal::BaseController +class Admin::Api::Agenda::RdvsController < Admin::Api::BaseController def index agent = Agent.find(params[:agent_id]) @organisation = Organisation.find(params[:organisation_id]) diff --git a/app/controllers/admin/api/base_controller.rb b/app/controllers/admin/api/base_controller.rb new file mode 100644 index 0000000000..377f4e6c3a --- /dev/null +++ b/app/controllers/admin/api/base_controller.rb @@ -0,0 +1,22 @@ +class Admin::Api::BaseController < ApplicationController + include Admin::AuthenticatedControllerConcern + + respond_to :json + + private + + def time_range_params + start_time = params.require(:start) + end_time = params.require(:end) + Time.zone.parse(start_time)..Time.zone.parse(end_time) + end + + def date_range_params + (time_range_params.begin.to_date)..(time_range_params.end.to_date) + end + helper_method :date_range_params + + def pundit_user + AgentContext.new(current_agent) + end +end \ No newline at end of file diff --git a/app/controllers/api/internal/base_controller.rb b/app/controllers/api/internal/base_controller.rb deleted file mode 100644 index 4b88f9ad48..0000000000 --- a/app/controllers/api/internal/base_controller.rb +++ /dev/null @@ -1,39 +0,0 @@ -class Api::Internal::BaseController < ApplicationController - rescue_from Pundit::NotAuthorizedError, with: :agent_not_authorized - - before_action :authenticate_agent! - before_action :set_default_format - - private - - # On override la méthode de devise pour renvoyer une erreur JSON plutôt qu'une redirection - def authenticate_agent! - unless current_agent - render json: { error: "Unauthorized" }, status: :unauthorized - end - end - - def agent_not_authorized(exception) - policy_name = exception.policy.class.to_s.underscore - render json: { error: t("#{policy_name}.#{exception.query}", scope: "pundit", default: :default) }, status: :forbidden - end - - def set_default_format - request.format = :json - end - - def time_range_params - start_time = params.require(:start) - end_time = params.require(:end) - Time.zone.parse(start_time)..Time.zone.parse(end_time) - end - - def date_range_params - (time_range_params.begin.to_date)..(time_range_params.end.to_date) - end - helper_method :date_range_params - - def pundit_user - AgentContext.new(current_agent) - end -end diff --git a/app/views/admin/agent_agendas/show.html.slim b/app/views/admin/agent_agendas/show.html.slim index 22f6ef513b..a9a9a6151d 100644 --- a/app/views/admin/agent_agendas/show.html.slim +++ b/app/views/admin/agent_agendas/show.html.slim @@ -18,7 +18,7 @@ data-selected-event-id="#{@selected_event_id}" data-organisation-id="#{@organisation.id}" data-display-saturdays="#{current_agent.display_saturdays}" - data-event-sources-json="#{[api_internal_admin_agenda_rdvs_path(agent_id: @agent, organisation_id: current_organisation.id), api_internal_admin_agenda_absences_path(agent_id: @agent, organisation_id: current_organisation.id), api_internal_admin_agenda_plage_ouvertures_path(agent_id: @agent, organisation_id: current_organisation.id, in_background: true), OffDays.to_full_calendar_array].to_json}" + data-event-sources-json="#{[admin_api_agenda_absences_path(agent_id: @agent, organisation_id: current_organisation.id, format: :json), admin_api_agenda_rdvs_path(agent_id: @agent, organisation_id: current_organisation.id, format: :json), admin_api_agenda_plage_ouvertures_path(agent_id: @agent, organisation_id: current_organisation.id, in_background: true, format: :json), OffDays.to_full_calendar_array].to_json}" ] .mt-3.flex-grow-1.text-right .m-2 diff --git a/app/views/api/internal/admin/agenda/absences/index.json.jb b/app/views/admin/api/agenda/absences/index.json.jb similarity index 100% rename from app/views/api/internal/admin/agenda/absences/index.json.jb rename to app/views/admin/api/agenda/absences/index.json.jb diff --git a/app/views/api/internal/admin/agenda/plage_ouvertures/index.json.jb b/app/views/admin/api/agenda/plage_ouvertures/index.json.jb similarity index 100% rename from app/views/api/internal/admin/agenda/plage_ouvertures/index.json.jb rename to app/views/admin/api/agenda/plage_ouvertures/index.json.jb diff --git a/app/views/api/internal/admin/agenda/rdvs/_rdv.json.jb b/app/views/admin/api/agenda/rdvs/_rdv.json.jb similarity index 100% rename from app/views/api/internal/admin/agenda/rdvs/_rdv.json.jb rename to app/views/admin/api/agenda/rdvs/_rdv.json.jb diff --git a/app/views/api/internal/admin/agenda/rdvs/_rdv_without_details.json.jb b/app/views/admin/api/agenda/rdvs/_rdv_without_details.json.jb similarity index 100% rename from app/views/api/internal/admin/agenda/rdvs/_rdv_without_details.json.jb rename to app/views/admin/api/agenda/rdvs/_rdv_without_details.json.jb diff --git a/app/views/api/internal/admin/agenda/rdvs/index.json.jb b/app/views/admin/api/agenda/rdvs/index.json.jb similarity index 100% rename from app/views/api/internal/admin/agenda/rdvs/index.json.jb rename to app/views/admin/api/agenda/rdvs/index.json.jb diff --git a/config/routes.rb b/config/routes.rb index 038338c663..05ab6b322d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -121,6 +121,13 @@ authenticate :agent do namespace "admin" do + namespace :api, defaults: { format: :json } do + namespace :agenda do + resources :plage_ouvertures, only: [:index] + resources :rdvs, only: [:index] + resources :absences, only: [:index] + end + end resources :territories, only: %i[edit update show] do scope module: "territories" do resources :agent_roles, only: %i[update create destroy] diff --git a/config/routes/api.rb b/config/routes/api.rb index 0e731a3415..3b7d90adb8 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -63,16 +63,6 @@ end end - namespace :internal do - namespace :admin do - namespace :agenda do - resources :plage_ouvertures, only: [:index] - resources :rdvs, only: [:index] - resources :absences, only: [:index] - end - end - end - post "/coop-mediation-numerique/accounts", to: "coop_mediation_numerique/accounts#create" end