Skip to content

Commit

Permalink
Refacto : on utilise un controleur de base dédié aux APIs internes
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineGirard committed Nov 14, 2024
1 parent 3eb351b commit 2b772b9
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 34 deletions.
22 changes: 0 additions & 22 deletions app/controllers/admin/agenda/base_controller.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Admin::Agenda::AbsencesController < Admin::Agenda::BaseController
class Api::Internal::Admin::Agenda::AbsencesController < Api::Internal::BaseController
def index
agent = Agent.find(params[:agent_id])
@organisation = Organisation.find(params[:organisation_id])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Admin::Agenda::PlageOuverturesController < Admin::Agenda::BaseController
class Api::Internal::Admin::Agenda::PlageOuverturesController < Api::Internal::BaseController
def index
@agent = Agent.find(params[:agent_id])
@organisation = Organisation.find(params[:organisation_id])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Admin::Agenda::RdvsController < Admin::Agenda::BaseController
class Api::Internal::Admin::Agenda::RdvsController < Api::Internal::BaseController
def index
agent = Agent.find(params[:agent_id])
@organisation = Organisation.find(params[:organisation_id])
Expand Down
39 changes: 39 additions & 0 deletions app/controllers/api/internal/base_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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
8 changes: 6 additions & 2 deletions app/javascript/components/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,12 @@ class CalendarRdvSolidarites {
return now >= activeStart && now <= activeEnd;
}

handleAjaxError = () => {
alert(`Le chargement du calendrier a échoué; un rapport d’erreur a été transmis à l’équipe.\nRechargez la page, et si ce problème persiste, contactez-nous à support@rdv-service-public.fr.`);
handleAjaxError = (error) => {
if (error.xhr.status === 401) {
window.location.reload();
} else {
alert(`Le chargement du calendrier a échoué; un rapport d’erreur a été transmis à l’équipe.\nRechargez la page, et si ce problème persiste, contactez-nous à support@rdv-service-public.fr.`);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/agent_agendas/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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="#{[admin_agenda_rdvs_path(agent_id: @agent, organisation_id: current_organisation.id), admin_agenda_absences_path(agent_id: @agent, organisation_id: current_organisation.id), 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="#{[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}"
]
.mt-3.flex-grow-1.text-right
.m-2
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 0 additions & 6 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,6 @@
end
end

namespace :agenda do
resources :plage_ouvertures, only: [:index]
resources :rdvs, only: [:index]
resources :absences, only: [:index]
end

resources :organisations do
get "creneaux_search" => "creneaux_search#index"
get "creneaux_search/selection_creneaux" => "creneaux_search#selection_creneaux"
Expand Down
10 changes: 10 additions & 0 deletions config/routes/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@
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

Expand Down

0 comments on commit 2b772b9

Please sign in to comment.