2021-09-17 11:44:22 +02:00
|
|
|
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] }
|
2021-09-28 15:12:10 +02:00
|
|
|
.compact
|
2021-09-17 11:44:22 +02:00
|
|
|
.map { |provider, scope| extract_sources(provider, scope) }
|
|
|
|
.reduce({}) { |acc, el| acc.deep_merge(el) }
|
|
|
|
end
|
|
|
|
|
2021-09-17 12:21:38 +02:00
|
|
|
# Remove sources not available for the procedure
|
|
|
|
def sanitize(requested_sources)
|
|
|
|
requested_sources_a = h_to_a(requested_sources)
|
|
|
|
available_sources_a = h_to_a(available_sources)
|
|
|
|
|
|
|
|
filtered_sources_a = requested_sources_a.intersection(available_sources_a)
|
|
|
|
|
|
|
|
a_to_h(filtered_sources_a)
|
|
|
|
end
|
|
|
|
|
2021-09-17 11:44:22 +02:00
|
|
|
private
|
|
|
|
|
2021-09-17 12:21:38 +02:00
|
|
|
# { 'cnaf' => { 'scope' => ['a', 'b'] }} => [['cnaf', 'scope', 'a'], ['cnaf', 'scope', 'b']]
|
|
|
|
def h_to_a(h)
|
|
|
|
h.reduce([]) { |acc, (provider, scopes)| scopes.each { |scope, values| values.each { |s, _| acc << [provider, scope, s] } }; acc }
|
|
|
|
end
|
|
|
|
|
|
|
|
# [['cnaf', 'scope', 'a'], ['cnaf', 'scope', 'b']] => { 'cnaf' => { 'scope' => ['a', 'b'] }}
|
|
|
|
def a_to_h(a)
|
|
|
|
h = Hash.new { |h, k| h[k] = Hash.new { |h2, k2| h2[k2] = [] } }
|
|
|
|
|
|
|
|
a.reduce(h) { |acc, (provider, scope, source)| h[provider][scope] << source; acc }
|
|
|
|
end
|
|
|
|
|
2021-09-17 11:44:22 +02:00
|
|
|
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' => {
|
2021-09-21 11:01:04 +02:00
|
|
|
'allocataires' => ['nomPrenom', 'dateDeNaissance', 'sexe'],
|
|
|
|
'enfants' => ['nomPrenom', 'dateDeNaissance', 'sexe'],
|
|
|
|
'adresse' => ['identite', 'complementIdentite', 'complementIdentiteGeo', 'numeroRue', 'lieuDit', 'codePostalVille', 'pays'],
|
|
|
|
'quotient_familial' => ['quotientFamilial', 'annee', 'mois']
|
2021-09-17 11:44:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|