Skip to content

Commit

Permalink
Find or create plus clair côté hubee, séquençage hubee > fqf dans un …
Browse files Browse the repository at this point in the history
…organizer
  • Loading branch information
jbfeldis committed Nov 4, 2024
1 parent f13bda3 commit 1eb51ab
Show file tree
Hide file tree
Showing 20 changed files with 403 additions and 277 deletions.
5 changes: 1 addition & 4 deletions app/clients/formulaire_qf_api_client.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
class FormulaireQFAPIClient
def create_collectivity(authorization_request)
organization = authorization_request.organization
editor_name = authorization_request.extra_infos.dig('service_provider', 'id')

def create_collectivity(organization:, editor_name: nil)
params = {
siret: organization.siret,
code_cog: organization.code_commune_etablissement,
Expand Down
106 changes: 63 additions & 43 deletions app/clients/hubee_api_client.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
class HubEEAPIClient < AbstractHubEEAPIClient
class HubEEAPIClient < AbstractHubEEAPIClient # rubocop:disable Metrics/ClassLength
class NotFound < StandardError; end
class AlreadyExists < StandardError; end

def find_or_create_organization(organization, email_demandeur = nil)
find_organization(organization)
rescue NotFound
create_organization(organization, email_demandeur)
end

def find_organization(organization)
http_connection.get("#{host}/referential/v1/organizations/SI-#{organization.siret}-#{organization.code_commune_etablissement}").body
rescue Faraday::ResourceNotFound
Expand Down Expand Up @@ -30,32 +36,10 @@ def create_organization(organization, email)
raise
end

def create_subscription(authorization_request, organization_payload, process_code) # rubocop:disable Metrics/AbcSize
http_connection.post(
"#{host}/referential/v1/subscriptions",
{
datapassId: authorization_request.external_id.to_i,
notificationFrequency: 'Aucune',
processCode: process_code,
subscriber: {
type: 'SI',
companyRegister: organization_payload['companyRegister'],
branchCode: organization_payload['branchCode']
},
email: authorization_request.demandeur.email,
status: 'Inactif',
localAdministrator: {
email: authorization_request.demandeur.email
},
validateDateTime: DateTime.now.iso8601,
updateDateTime: DateTime.now.iso8601
}.to_json,
'Content-Type' => 'application/json'
)
rescue Faraday::BadRequestError => e
raise AlreadyExists if already_exists_error?(e)

raise
def create_subscription(authorization_request, organization_payload, process_code, editor_payload = {})
subscription_payload = find_or_create_inactive_subscription(authorization_request, organization_payload, process_code)
activate_subscription(authorization_request, subscription_payload, editor_payload)
subscription_payload
end

def find_subscription(_authorization_request, organization_payload, process_code)
Expand All @@ -67,7 +51,28 @@ def find_subscription(_authorization_request, organization_payload, process_code
request.body.first
end

def update_subscription(authorization_request, subscription_payload, editor_payload = {}) # rubocop:disable Metrics/AbcSize
protected

def host
Rails.application.credentials.hubee_api_url
end

def already_exists_error?(faraday_error)
faraday_error.response[:body]['errors'].any? do |error|
error['message'].include?('already exists')
end
end

def http_connection(&block)
super do |conn|
conn.request :authorization, 'Bearer', -> { HubEEAPIAuthentication.new.access_token }
yield(conn) if block
end
end

private

def activate_subscription(authorization_request, subscription_payload, editor_payload = {})
subscription_id = authorization_request.extra_infos['hubee_subscription_id']
return if subscription_id.blank?

Expand All @@ -80,7 +85,7 @@ def update_subscription(authorization_request, subscription_payload, editor_payl

payload.delete('id')
payload.delete('creationDateTime')
payload.merge!(editor_payload.with_indifferent_access) if editor_payload.present?
payload.merge!(editor_payload.with_indifferent_access)

http_connection.put(
"#{host}/referential/v1/subscriptions/#{subscription_id}",
Expand All @@ -89,22 +94,37 @@ def update_subscription(authorization_request, subscription_payload, editor_payl
)
end

protected

def host
Rails.application.credentials.hubee_api_url
end
def create_inactive_subscription(authorization_request, organization_payload, process_code) # rubocop:disable Metrics/AbcSize
http_connection.post(
"#{host}/referential/v1/subscriptions",
{
datapassId: authorization_request.external_id.to_i,
notificationFrequency: 'Aucune',
processCode: process_code,
subscriber: {
type: 'SI',
companyRegister: organization_payload['companyRegister'],
branchCode: organization_payload['branchCode']
},
email: authorization_request.demandeur.email,
status: 'Inactif',
localAdministrator: {
email: authorization_request.demandeur.email
},
validateDateTime: DateTime.now.iso8601,
updateDateTime: DateTime.now.iso8601
}.to_json,
'Content-Type' => 'application/json'
)
rescue Faraday::BadRequestError => e
raise AlreadyExists if already_exists_error?(e)

def already_exists_error?(faraday_error)
faraday_error.response[:body]['errors'].any? do |error|
error['message'].include?('already exists')
end
raise
end

def http_connection(&block)
super do |conn|
conn.request :authorization, 'Bearer', -> { HubEEAPIAuthentication.new.access_token }
yield(conn) if block
end
def find_or_create_inactive_subscription(authorization_request, organization_payload, process_code)
create_inactive_subscription(authorization_request, organization_payload, process_code)
rescue HubEEAPIClient::AlreadyExists
find_subscription(authorization_request, organization_payload, process_code)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class DatapassWebhook::APIParticulier::CreateFormulaireQFCollectivity < ApplicationInteractor
delegate :authorization_request, to: :context
delegate :organization, to: :authorization_request, private: true

def call
FormulaireQFAPIClient.new.create_collectivity(organization:, editor_name:)
end

private

def editor_name
authorization_request.extra_infos.dig('service_provider', 'id')
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class DatapassWebhook::APIParticulier::CreateHubEEOrganization < ApplicationInteractor
delegate :authorization_request, :hubee_organization_payload, to: :context

def call
context.hubee_organization_payload = find_or_create_organization_on_hubee
save_hubee_organization_id_to_authorization_request
end

private

def build_hubee_organization_id
"SI-#{hubee_organization_payload['companyRegister']}-#{hubee_organization_payload['branchCode']}"
end

def find_or_create_organization_on_hubee
hubee_api_client.find_or_create_organization(authorization_request.organization, authorization_request.demandeur.email)
end

def hubee_api_client
@hubee_api_client ||= HubEEAPIClient.new
end

def save_hubee_organization_id_to_authorization_request
authorization_request.extra_infos['hubee_organization_id'] = build_hubee_organization_id
authorization_request.save!
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class DatapassWebhook::APIParticulier::CreateHubEESubscription < ApplicationInteractor
delegate :authorization_request, :hubee_organization_payload, :hubee_subscription_payload, to: :context

def call
context.hubee_subscription_payload = create_subscription_on_hubee
save_hubee_subscription_id_to_authorization_request
end

private

def create_subscription_on_hubee
hubee_api_client.create_subscription(authorization_request, hubee_organization_payload, process_code, editor_payload)
end

def editor_organization
@editor_organization ||= Organization.new(service_provider['siret'])
end

def editor_payload
return {} unless editor_subscription?

{
delegationActor: {
branchCode: editor_organization.code_commune_etablissement,
companyRegister: editor_organization.siret,
type: 'EDT'
},
accessMode: 'API'
}
end

def editor_subscription?
service_provider['type'] == 'editor'
end

def hubee_api_client
@hubee_api_client ||= HubEEAPIClient.new
end

def process_code
'FormulaireQF'
end

def save_hubee_subscription_id_to_authorization_request
authorization_request.extra_infos['hubee_subscription_id'] = hubee_subscription_payload['id']
authorization_request.save!
end

def service_provider
@service_provider ||= Hash(authorization_request.extra_infos['service_provider'])
end
end

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class DatapassWebhook::ScheduleCreateFormulaireQFResourcesJob < ApplicationInteractor
def call
return unless context.event == 'approve'
return unless context.modalities.include?('formulaire_qf')

CreateFormulaireQFResourcesJob.perform_later(context.authorization_request.id)
end
end
6 changes: 0 additions & 6 deletions app/jobs/create_formulaire_qf_collectivity_job.rb

This file was deleted.

64 changes: 0 additions & 64 deletions app/jobs/create_formulaire_qf_hubee_subscription_job.rb

This file was deleted.

7 changes: 7 additions & 0 deletions app/jobs/create_formulaire_qf_resources_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class CreateFormulaireQFResourcesJob < ApplicationJob
def perform(authorization_request_id)
authorization_request = AuthorizationRequest.find(authorization_request_id)

DatapassWebhook::CreateFormulaireQFResources.call(authorization_request:)
end
end
2 changes: 1 addition & 1 deletion app/organizers/datapass_webhook/api_particulier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class APIParticulier < ApplicationOrganizer
::DatapassWebhook::ReopenAuthorizationRequest,
::DatapassWebhook::RevokeCurrentToken,
::DatapassWebhook::UpdateMailjetContacts,
::DatapassWebhook::ScheduleCreateFormulaireQFHubEESubscriptionJob,
::DatapassWebhook::ScheduleCreateFormulaireQFResourcesJob,
::DatapassWebhook::ExtractMailjetVariables,
::DatapassWebhook::ScheduleAuthorizationRequestEmails,
::DatapassWebhook::APIParticulier::NotifyReporters
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class DatapassWebhook::CreateFormulaireQFResources < ApplicationOrganizer
organize DatapassWebhook::APIParticulier::CreateHubEEOrganization,
DatapassWebhook::APIParticulier::CreateHubEESubscription,
DatapassWebhook::APIParticulier::CreateFormulaireQFCollectivity
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'rails_helper'

RSpec.describe DatapassWebhook::APIParticulier::CreateFormulaireQFCollectivity, type: :interactor do
subject(:interactor) { described_class.call(**params) }

let(:authorization_request) { create(:authorization_request) }
let(:formulaire_qf_api_client) { instance_double(FormulaireQFAPIClient) }
let(:params) { { authorization_request: } }
let(:organization) { authorization_request.organization }
let(:editor_name) { 'SUPEREDITOR' }

before do
allow(FormulaireQFAPIClient).to receive(:new).and_return(formulaire_qf_api_client)
allow(formulaire_qf_api_client).to receive(:create_collectivity)

authorization_request.extra_infos['service_provider'] = { 'id' => editor_name }
end

it 'creates a collectivity on FormulaireQF' do
expect(formulaire_qf_api_client).to receive(:create_collectivity).with(organization:, editor_name:)
interactor
end

it { is_expected.to be_a_success }
end
Loading

0 comments on commit 1eb51ab

Please sign in to comment.