Skip to content

Commit

Permalink
Merge pull request #9365 from demarches-simplifiees/fix-siret
Browse files Browse the repository at this point in the history
envoie le siret par défaut si le siret du service est identique au siret de l'établissement demandé
  • Loading branch information
krichtof authored Jul 27, 2023
2 parents fed6b8c + 2963fdb commit c315127
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
16 changes: 9 additions & 7 deletions app/lib/api_entreprise/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,16 @@ def current_status

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
Expand Down Expand Up @@ -144,8 +146,8 @@ def make_url(resource_name, siret_or_siren = nil)
[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
Expand All @@ -158,10 +160,10 @@ def build_params(user_id)
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
Expand Down
20 changes: 16 additions & 4 deletions spec/lib/api_entreprise/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,22 @@
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
Expand Down

0 comments on commit c315127

Please sign in to comment.