Skip to content

Commit

Permalink
feat(webhooks): Retrieve user organisations when creating it from rdv…
Browse files Browse the repository at this point in the history
…sp (#2369)

* feat(webhooks): Retrieve user organisations when creating it from rdvsp

* Apply review suggestions

* move method from concern

* apply new rdv_solidarites_session method to recent referent assignation script
  • Loading branch information
aminedhobb authored Oct 7, 2024
1 parent b9f724d commit eccd042
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 27 deletions.
8 changes: 8 additions & 0 deletions app/clients/rdv_solidarites_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ def create_user(request_body)
)
end

def get_user(user_id)
Faraday.get(
"#{@url}/api/rdvinsertion/users/#{user_id}",
{},
request_headers
)
end

def create_user_profiles(user_id, organisation_ids)
Faraday.post(
"#{@url}/api/rdvinsertion/user_profiles/create_many",
Expand Down
17 changes: 13 additions & 4 deletions app/jobs/inbound_webhooks/rdv_solidarites/process_rdv_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,34 @@ def rdv_solidarites_user_ids
rdv_solidarites_rdv.user_ids
end

# rubocop:disable Metrics/AbcSize
def find_or_create_users
existing_users = User.where(rdv_solidarites_user_id: rdv_solidarites_user_ids).to_a

new_rdv_solidarites_users = rdv_solidarites_rdv.users.reject do |user|
user.id.in?(existing_users.map(&:rdv_solidarites_user_id))
end

new_users = new_rdv_solidarites_users.map do |user|
new_users = new_rdv_solidarites_users.map do |rdv_solidarites_user|
User.create!(
rdv_solidarites_user_id: user.id,
organisations: [organisation],
rdv_solidarites_user_id: rdv_solidarites_user.id,
created_through: "rdv_solidarites_webhook",
created_from_structure: organisation,
**user.attributes.slice(*User::SHARED_ATTRIBUTES_WITH_RDV_SOLIDARITES).compact_blank
organisation_ids: retrieve_user_organisation_ids(rdv_solidarites_user.id),
**rdv_solidarites_user.attributes.slice(*User::SHARED_ATTRIBUTES_WITH_RDV_SOLIDARITES).compact_blank
)
end

@users = existing_users + new_users
end
# rubocop:enable Metrics/AbcSize

def retrieve_user_organisation_ids(rdv_solidarites_user_id)
rdv_solidarites_organisation_ids = agents.first.with_rdv_solidarites_session do
call_service!(RdvSolidaritesApi::RetrieveUser, rdv_solidarites_user_id:).user.organisation_ids
end
Organisation.where(rdv_solidarites_organisation_id: rdv_solidarites_organisation_ids).ids
end

def participations_attributes_destroyed
return [] if rdv.nil?
Expand Down
8 changes: 8 additions & 0 deletions app/models/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ def department_organisations(department_id)
organisations.select { |organisation| organisation.department_id == department_id }
end

def with_rdv_solidarites_session(&)
# This ensure Current.rdv_solidarites_client would call the agent rdv_solidarites_client
Current.agent = self
yield
ensure
Current.agent = nil
end

private

# This is to make sure an agent can't be set as super_admin through an agent creation or update in the app.
Expand Down
10 changes: 10 additions & 0 deletions app/models/rdv_solidarites/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,15 @@ class User < Base
def deleted?
email&.ends_with?("@deleted.rdv-solidarites.fr")
end

def organisation_ids
@attributes[:user_profiles].map do |user_profile_attributes|
user_profile_attributes.dig(:organisation, :id)
end.compact
end

def user_profiles
@attributes[:user_profiles].to_a
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ module Invitations
class VerifyOrganisationCreneauxAvailability < BaseService
def initialize(organisation_id:)
@organisation = Organisation.find(organisation_id)
# On prend le premier agent de l'organisation pour les appels à l'API RDVSP
Current.agent = @organisation.agents.first
@invitations_params_without_creneau = []
@grouped_invitation_params_by_category = []
end

def call
process_invitations
process_invitations_params_without_creneau
result.grouped_invitation_params_by_category = @grouped_invitation_params_by_category
# On prend le premier agent de l'organisation pour les appels à l'API RDVSP
@organisation.agents.first.with_rdv_solidarites_session do
process_invitations
process_invitations_params_without_creneau
result.grouped_invitation_params_by_category = @grouped_invitation_params_by_category
end
end

private
Expand Down
19 changes: 19 additions & 0 deletions app/services/rdv_solidarites_api/retrieve_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module RdvSolidaritesApi
class RetrieveUser < Base
def initialize(rdv_solidarites_user_id:)
@rdv_solidarites_user_id = rdv_solidarites_user_id
end

def call
request!
result.user =
RdvSolidarites::User.new(rdv_solidarites_response_body["user"])
end

private

def rdv_solidarites_response
@rdv_solidarites_response ||= rdv_solidarites_client.get_user(@rdv_solidarites_user_id)
end
end
end
12 changes: 1 addition & 11 deletions lib/organisations/destroy_multiple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,11 @@ def display_organisation_info(organisation)
end

def organisation_exists_in_rdv_solidarites?(organisation)
with_faked_agent_auth(organisation) do
organisation.agents.first.with_rdv_solidarites_session do
RdvSolidaritesApi::RetrieveOrganisation
.call(rdv_solidarites_organisation_id: organisation.rdv_solidarites_organisation_id)
.success?
end
end

def with_faked_agent_auth(organisation)
return yield if Current.agent.present?

Current.agent = organisation.agents.first
result = yield
Current.agent = nil

result
end
end
end
9 changes: 3 additions & 6 deletions lib/users/transfer_referent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ def initialize(source_referent_id:, target_referent_id:)

def call
ReferentAssignation.where(agent: source_referent).find_each do |referent_assignation|
set_current_agent(referent_assignation)
assign_target_and_remove_source_referent(referent_assignation)
referent_assignation.agent.with_rdv_solidarites_session do
assign_target_and_remove_source_referent(referent_assignation)
end
end
end

private

def set_current_agent(referent_assignation)
Current.agent = referent_assignation.agent
end

def assign_target_and_remove_source_referent(referent_assignation)
assignation_service = Users::AssignReferent.call(user: referent_assignation.user, agent: target_referent)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,18 +223,32 @@
let!(:new_follow_up) do
build(:follow_up, motif_category: motif_category, user: new_user)
end
let!(:other_rdv_solidarites_organisation_id) { 22_424 }
let!(:other_user_organisation) do
create(:organisation, rdv_solidarites_organisation_id: other_rdv_solidarites_organisation_id)
end

before do
allow(User).to receive(:create!).and_return(new_user)
allow(FollowUp).to receive(:find_or_create_by!)
.with(user: new_user, motif_category: motif_category)
.and_return(new_follow_up)

allow(RdvSolidaritesApi::RetrieveUser).to receive(:call)
.with(rdv_solidarites_user_id: user_id1)
.and_return(
OpenStruct.new(
success?: true,
user: OpenStruct.new(organisation_ids: [rdv_solidarites_organisation_id,
other_rdv_solidarites_organisation_id])
)
)
end

it "creates the user" do
expect(User).to receive(:create!).with(
rdv_solidarites_user_id: user_id1,
organisations: [organisation],
organisation_ids: [organisation.id, other_user_organisation.id],
first_name: "James",
last_name: "Cameron",
address: "50 rue Victor Hugo 93500 Pantin",
Expand Down
53 changes: 53 additions & 0 deletions spec/services/rdv_solidarites_api/retrieve_user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
describe RdvSolidaritesApi::RetrieveUser, type: :service do
subject do
described_class.call(rdv_solidarites_user_id:)
end

let(:rdv_solidarites_client) { instance_double(RdvSolidaritesClient) }
let!(:rdv_solidarites_user_id) { 1717 }

describe "#call" do
let!(:user_attributes) do
{
"id" => 1717,
"first_name" => "Léonard",
"last_name" => "De Vinci"
}
end

before do
allow(Current).to receive(:rdv_solidarites_client).and_return(rdv_solidarites_client)
allow(rdv_solidarites_client).to receive(:get_user)
.with(rdv_solidarites_user_id)
.and_return(OpenStruct.new(success?: true, body: { "user" => user_attributes }.to_json))
end

context "when it succeeds" do
it("is a success") { is_a_success }

it "calls the rdv solidarites client" do
expect(rdv_solidarites_client).to receive(:get_user)
subject
end

it "returns the user" do
expect(subject.user.id).to eq(rdv_solidarites_user_id)
expect(subject.user.first_name).to eq("Léonard")
expect(subject.user.last_name).to eq("De Vinci")
end
end

context "when it fails" do
before do
allow(rdv_solidarites_client).to receive(:get_user)
.and_return(OpenStruct.new(success?: false, body: { error_messages: ["some error"] }.to_json))
end

it("is a failure") { is_a_failure }

it "returns the error" do
expect(subject.errors).to eq(["Erreur RDV-Solidarités: some error"])
end
end
end
end

0 comments on commit eccd042

Please sign in to comment.