Skip to content

Commit

Permalink
Add GET api/rdvinsertion/users/:user_id/referent_assignations endpo…
Browse files Browse the repository at this point in the history
…int (#4806)

* Add Api::Rdvinsertion::ReferentAssignationsController#index endpoint

* Remove subquery
  • Loading branch information
aminedhobb authored Nov 14, 2024
1 parent eb8329b commit 3ced104
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
class Api::Rdvinsertion::ReferentAssignationsController < Api::Rdvinsertion::AgentAuthBaseController
before_action :set_user, :set_agents, only: %i[create_many]
before_action :set_user, only: %i[create_many index]
before_action :set_agents, only: %i[create_many]

def index
referent_assignations = ReferentAssignation
.where(user: @user)
.joins(agent: :organisations)
.where(organisations: { verticale: "rdv_insertion" })
.distinct

render_collection referent_assignations
end

def create_many
@agents.each { |agent| ReferentAssignation.find_or_create_by!(user: @user, agent: agent) }
Expand Down
4 changes: 3 additions & 1 deletion config/routes/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
resource :referent_assignations, only: [] do
post :create_many, on: :collection
end
resources :users, only: %i[show]
resources :users, only: %i[show] do
resources :referent_assignations, only: %i[index]
end
resources :motif_categories, only: %i[create]
resources :motif_category_territories, only: %i[create]
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,26 @@
end
end
end

describe "GET /api/rdvinsertion/users/:id/referent_assignations" do
let(:agent) { create(:agent, basic_role_in_organisations: [organisation_rdv_insertion]) }
let(:user) { create(:user, organisations: [organisation_rdv_insertion], referent_agents: [agent, other_agent]) }
let(:other_agent) { create(:agent, basic_role_in_organisations: [organisation_rdv_solidarites]) }
let(:organisation_rdv_insertion) { create(:organisation, verticale: "rdv_insertion") }
let(:organisation_rdv_solidarites) { create(:organisation, verticale: "rdv_solidarites") }
let(:shared_secret) { "S3cr3T" }
let(:auth_headers) { api_auth_headers_with_shared_secret(agent, shared_secret) }

before do
allow(ENV).to receive(:fetch).with("SHARED_SECRET_FOR_AGENTS_AUTH").and_return(shared_secret)
end

it "returns the referent assignations" do
get api_rdvinsertion_user_referent_assignations_path(user.id), headers: auth_headers
expect(response.status).to eq(200)
response_referent_assignations = response.parsed_body["referent_assignations"]
response_referent_assignations_agent_ids = response_referent_assignations.map { |referent_assignation| referent_assignation.dig("agent", "id") }
expect(response_referent_assignations_agent_ids).to contain_exactly(agent.id)
end
end
end

0 comments on commit 3ced104

Please sign in to comment.