send dinum siret when siret service same as siret etablissement requested

This commit is contained in:
Christophe Robillard 2023-07-26 19:27:08 +02:00
parent 9571981856
commit 2963fdbfa3
2 changed files with 25 additions and 11 deletions

View file

@ -91,14 +91,16 @@ class APIEntreprise::API
private
def recipient
@procedure&.service && @procedure.service.siret.presence || ENV.fetch('API_ENTREPRISE_DEFAULT_SIRET')
def recipient_for(siret_or_siren)
service_siret = @procedure&.service && @procedure.service.siret.presence
return service_siret if service_siret && !service_siret.starts_with?(siret_or_siren)
ENV.fetch('API_ENTREPRISE_DEFAULT_SIRET')
end
def call_with_siret(resource_name, siret_or_siren, user_id: nil)
url = make_url(resource_name, siret_or_siren)
params = build_params(user_id)
params = build_params(user_id, siret_or_siren)
call(url, params)
end
@ -144,8 +146,8 @@ class APIEntreprise::API
[API_ENTREPRISE_URL, format(resource_name, id: siret_or_siren)].compact.join("/")
end
def build_params(user_id)
params = base_params
def build_params(user_id, siret_or_siren)
params = base_params(siret_or_siren)
params[:object] = if api_object.present?
api_object
@ -158,10 +160,10 @@ class APIEntreprise::API
params
end
def base_params
def base_params(siret_or_siren)
{
context: APPLICATION_NAME,
recipient: recipient,
recipient: recipient_for(siret_or_siren),
non_diffusables: true
}
end

View file

@ -95,10 +95,22 @@ describe APIEntreprise::API do
end
context 'with a service with siret' do
let(:procedure) { create(:procedure, :with_service) }
it 'send default recipient' do
subject
expect(WebMock).to have_requested(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/unites_legales\/#{siren}/).with(query: hash_including({ recipient: procedure.service.siret }))
context 'with a siren entreprise not equivalent to siret service' do
let(:procedure) { create(:procedure, :with_service) }
it 'send default recipient' do
subject
expect(WebMock).to have_requested(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/unites_legales\/#{siren}/).with(query: hash_including({ recipient: procedure.service.siret }))
end
end
context 'with a siren entreprise equivalent to siret service' do
let(:procedure) { create(:procedure, :with_service) }
let(:siren) { procedure.service.siret[0..8] }
let(:dinum_siret) { "13002526500013" }
it 'send default recipient' do
subject
expect(WebMock).to have_requested(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/insee\/sirene\/unites_legales\/#{siren}/).with(query: hash_including({ recipient: dinum_siret }))
end
end
end
end