Skip to content

Commit

Permalink
Review
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineGirard committed Nov 27, 2024
1 parent b00c00a commit 2340c2a
Show file tree
Hide file tree
Showing 19 changed files with 156 additions and 157 deletions.
Original file line number Diff line number Diff line change
@@ -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])
Expand Down
Original file line number Diff line number Diff line change
@@ -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])
Expand Down
Original file line number Diff line number Diff line change
@@ -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])
Expand Down
22 changes: 22 additions & 0 deletions app/controllers/admin/api/base_controller.rb
Original file line number Diff line number Diff line change
@@ -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
39 changes: 0 additions & 39 deletions app/controllers/api/internal/base_controller.rb

This file was deleted.

8 changes: 2 additions & 6 deletions app/javascript/components/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,8 @@ class CalendarRdvSolidarites {
return now >= activeStart && now <= activeEnd;
}

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.`);
}
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.`);
}
}

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="#{[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
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/plage_ouvertures/calendar.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
data-agent-id="#{@agent.id}"
data-organisation-id="#{@current_organisation.id}"
data-display-saturdays="#{current_agent.display_saturdays}"
data-event-sources-json="#{[admin_agenda_plage_ouvertures_path(agent_id: @agent, organisation_id: current_organisation.id), OffDays.to_full_calendar_array].to_json}"
data-event-sources-json="#{[admin_api_agenda_plage_ouvertures_path(agent_id: @agent, organisation_id: current_organisation.id), OffDays.to_full_calendar_array].to_json}"
]
7 changes: 7 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,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]
Expand Down
10 changes: 0 additions & 10 deletions config/routes/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,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

Expand Down
95 changes: 0 additions & 95 deletions spec/controllers/admin/agenda/rdvs_controller_spec.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.describe Admin::Agenda::AbsencesController, type: :controller do
RSpec.describe Admin::Api::Agenda::AbsencesController, type: :controller do
describe "GET index" do
context "with a signed in agent" do
let(:organisation) { create(:organisation) }
Expand Down Expand Up @@ -56,5 +56,12 @@
end
end
end

context "when agent is not login" do
it "returns unauthorized" do
get :index, params: { agent_id: 1, organisation_id: 1, start: Date.new(2019, 8, 12), end: Date.new(2019, 8, 19), format: :json }
expect(response).to be_unauthorized
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.describe Admin::Agenda::PlageOuverturesController, type: :controller do
RSpec.describe Admin::Api::Agenda::PlageOuverturesController, type: :controller do
describe "GET index" do
context "with a signed in agent" do
let(:organisation) { create(:organisation) }
Expand Down Expand Up @@ -66,5 +66,12 @@
end
end
end

context "when agent is not login" do
it "returns unauthorized" do
get :index, params: { agent_id: 1, organisation_id: 1, start: Date.new(2019, 8, 12), end: Date.new(2019, 8, 19), format: :json }
expect(response).to be_unauthorized
end
end
end
end
104 changes: 104 additions & 0 deletions spec/controllers/admin/api/agenda/rdvs_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
RSpec.describe Admin::Api::Agenda::RdvsController, type: :controller do
render_views

# Semaine du lundi 8 avril 2024 au vendredi 12 avril 2024.
# On note que FullCalendar utilise des dates naïves (sans timezone).
let(:fullcalendar_time_range_params) do
{
start: "2024-04-08T00:00:00",
end: "2024-04-13T00:00:00", # FullCalendar utilise cette valeur pour indiquer "jusqu'au vendredi 12 inclus"
}
end
let(:aujourdhui_lundi_15h) { Time.zone.parse("2024-04-08 15:00:00") }
let(:mercredi_15h) { Time.zone.parse("2024-04-10 15:00:00") }

let(:organisation) { create(:organisation) }

context "with a signed in agent" do
before { sign_in current_agent }

describe "displaying RDVs across organisations" do
let(:other_organisation) { create(:organisation) }
let(:current_agent) { create(:agent, admin_role_in_organisations: [organisation, other_organisation]) }

it "returns rdvs of given agent across organisations" do
travel_to(aujourdhui_lundi_15h)
given_agent = create(:agent, basic_role_in_organisations: [organisation], service: current_agent.services.first)
create(:rdv, agents: [current_agent])
rdv = create(:rdv, agents: [given_agent], organisation: organisation, starts_at: mercredi_15h)
rdv_from_other_organisation = create(:rdv, agents: [given_agent], organisation: other_organisation, starts_at: mercredi_15h)
get :index, params: fullcalendar_time_range_params.merge(agent_id: given_agent.id, organisation_id: organisation.id, format: :json)
expect(response).to be_successful

returns_rdvs = response.parsed_body
expect(returns_rdvs.pluck("id")).to contain_exactly(rdv.id, rdv_from_other_organisation.id)

# Les RDVs des autres orgas sont affichés en gris
expect(returns_rdvs.find { _1["id"] == rdv_from_other_organisation.id }["backgroundColor"]).to eq("#757575")
end
end

describe "respecting the time range" do
let(:samedi_15h) { Time.zone.parse("2024-04-13 15:00:00") }
let(:mardi_en_huit_15h) { Time.zone.parse("2024-04-16 15:00:00") }
let(:vendredi_dernier_15h) { Time.zone.parse("2024-04-05 15:00:00") }

let(:current_agent) { create(:agent, basic_role_in_organisations: [organisation]) }

it "returns rdvs of given agent from start to end" do
travel_to(aujourdhui_lundi_15h)
create(:rdv, agents: [current_agent], organisation: organisation, starts_at: vendredi_dernier_15h)
rdv = create(:rdv, agents: [current_agent], organisation: organisation, starts_at: mercredi_15h)
create(:rdv, agents: [current_agent], organisation: organisation, starts_at: samedi_15h)
create(:rdv, agents: [current_agent], organisation: organisation, starts_at: mardi_en_huit_15h)

get :index, params: fullcalendar_time_range_params.merge(agent_id: current_agent.id, organisation_id: organisation.id, format: :json)
expect(response.parsed_body.pluck("id")).to eq([rdv.id])
end
end

describe "showing RDVs without details" do
describe "showing an agent's RDVs for a service in which I am not" do
let(:current_agent) { create(:agent, basic_role_in_organisations: [organisation]) }

it "does not show any info about the RDV and does not provide a link" do
other_service = create(:service)
given_agent = create(:agent, basic_role_in_organisations: [organisation], service: current_agent.services.first)
rdv_of_another_service = create(:rdv, agents: [given_agent], organisation: organisation, starts_at: mercredi_15h, motif: create(:motif, service: other_service))
get :index, params: fullcalendar_time_range_params.merge(agent_id: given_agent.id, organisation_id: organisation.id, format: :json)
expect(response.parsed_body.size).to eq(1)
expect(response.parsed_body[0].keys).to eq(%w[start end title textColor backgroundColor extendedProps])
expect(response.parsed_body[0]["title"]).to eq("Occupé⋅e (en RDV)")
expect(Time.zone.parse(response.parsed_body[0]["start"])).to eq(rdv_of_another_service.starts_at)
expect(Time.zone.parse(response.parsed_body[0]["end"])).to eq(rdv_of_another_service.ends_at)
expect(response.parsed_body[0]["extendedProps"]["unauthorizedRdvExplanation"]).to include("Vous n'avez pas accès à ce RDV")
end
end

describe "showing an agent's RDVs for an organisation where I don't belongs" do
let(:current_agent) { create(:agent, basic_role_in_organisations: [organisation]) }

it "does not show any info about the RDV and does not provide a link" do
other_org = create(:organisation)
given_agent = create(:agent, basic_role_in_organisations: [organisation, other_org], service: current_agent.services.first)
motif_of_other_org = create(:motif, organisation: other_org, service: current_agent.services.first)
rdv_of_another_org_same_service = create(:rdv, agents: [given_agent], organisation: other_org, starts_at: aujourdhui_lundi_15h, motif: motif_of_other_org)
get :index, params: fullcalendar_time_range_params.merge(agent_id: given_agent.id, organisation_id: organisation.id, format: :json)
expect(response.parsed_body.size).to eq(1)
expect(response.parsed_body[0].keys).to eq(%w[start end title textColor backgroundColor extendedProps])
expect(response.parsed_body[0]["title"]).to eq("Occupé⋅e (en RDV)")
expect(Time.zone.parse(response.parsed_body[0]["start"])).to eq(rdv_of_another_org_same_service.starts_at)
expect(Time.zone.parse(response.parsed_body[0]["end"])).to eq(rdv_of_another_org_same_service.ends_at)
expect(response.parsed_body[0]["extendedProps"]["unauthorizedRdvExplanation"]).to include("Vous n'avez pas accès à ce RDV")
end
end
end
end

context "when agent is not login" do
it "returns unauthorized" do
get :index, params: { agent_id: 1, organisation_id: 1, start: "2019-08-12", end: "2019-08-19", format: :json }
expect(response).to be_unauthorized
end
end
end

0 comments on commit 2340c2a

Please sign in to comment.