Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refacto : on sépare les API internes full calendar dans un autre namespace #4805

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Admin::Agenda::AbsencesController < Admin::Agenda::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 Admin::Agenda::PlageOuverturesController < Admin::Agenda::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 Admin::Agenda::RdvsController < Admin::Agenda::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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Admin::Agenda::BaseController < ApplicationController
class Admin::Api::BaseController < ApplicationController
include Admin::AuthenticatedControllerConcern

respond_to :json
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="#{[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}"
]
13 changes: 7 additions & 6 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 Expand Up @@ -172,12 +179,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
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ici j’ai juste rajouté ce contexte pour pouvoir rajouter le cas où l’agent n’est pas connecté

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 }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

J'apprécie beaucoup que cette spec ne reprenne pas tous les let de l'enfer de celle d'au-dessus, bravo ! 👏

expect(response).to be_unauthorized
end
end
end
Loading