add ApiParticulier::Service::Sources

This commit is contained in:
simon lehericey 2021-09-17 11:44:22 +02:00
parent b000590d81
commit 8327c6fd0c
2 changed files with 76 additions and 0 deletions

View file

@ -0,0 +1,42 @@
module APIParticulier
module Services
class SourcesService
def initialize(procedure)
@procedure = procedure
end
def available_sources
@procedure.api_particulier_scopes
.map { |provider_and_scope| raw_scopes[provider_and_scope] }
.map { |provider, scope| extract_sources(provider, scope) }
.reduce({}) { |acc, el| acc.deep_merge(el) }
end
private
def extract_sources(provider, scope)
{ provider => { scope => providers[provider][scope] } }
end
def raw_scopes
{
'cnaf_allocataires' => ['cnaf', 'allocataires'],
'cnaf_enfants' => ['cnaf', 'enfants'],
'cnaf_adresse' => ['cnaf', 'adresse'],
'cnaf_quotient_familial' => ['cnaf', 'quotient_familial']
}
end
def providers
{
'cnaf' => {
'allocataires' => ['noms_prenoms', 'date_de_naissance', 'sexe'],
'enfants' => ['noms_prenoms', 'date_de_naissance', 'sexe'],
'adresse' => ['identite', 'complement_d_identite', 'complement_d_identite_geo', 'numero_et_rue', 'lieu_dit', 'code_postal_et_ville', 'pays'],
'quotient_familial' => ['quotient_familial', 'annee', 'mois']
}
}
end
end
end
end

View file

@ -0,0 +1,34 @@
describe APIParticulier::Services::SourcesService do
describe "#sources" do
let(:service) { described_class.new(procedure) }
let(:procedure) { create(:procedure) }
let(:api_particulier_scopes) { [] }
let(:api_particulier_sources) { {} }
before do
procedure.update(api_particulier_scopes: api_particulier_scopes)
procedure.update(api_particulier_sources: api_particulier_sources)
end
subject { service.available_sources }
context 'when the procedure doesnt have any available scopes' do
it { is_expected.to eq({}) }
end
context 'when a procedure has a cnaf_allocataires and a cnaf_adresse scopes' do
let(:api_particulier_scopes) { ['cnaf_allocataires', 'cnaf_enfants'] }
let(:cnaf_allocataires_and_enfants) do
{
'cnaf' => {
'allocataires' => ['noms_prenoms', 'date_de_naissance', 'sexe'],
'enfants' => ['noms_prenoms', 'date_de_naissance', 'sexe']
}
}
end
it { is_expected.to match(cnaf_allocataires_and_enfants) }
end
end
end