Skip to content

Commit

Permalink
Making ScheduleAuthorizationRequestJob work for Particulier
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuelfaure committed Aug 16, 2023
1 parent b61be83 commit bca0292
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
class DatapassWebhook::ScheduleAuthorizationRequestEmails < ApplicationInteractor
def call
(datapass_webhooks_config_for_event[:emails] || []).each do |email_config|
schedule_email(
email_config.stringify_keys
)
schedule_email(email_config.stringify_keys)
end
end

Expand Down
10 changes: 10 additions & 0 deletions app/jobs/concerns/mailjet_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ def build_mailjet_error_context(mailjet_exception)
}
end

def from_name(api)
"API #{api.capitalize}"
end

def from_email(api)
mailer_klass = "API#{api.capitalize}Mailer".constantize

mailer_klass.default_params[:from]
end

private

def sanitize_message_payload(message)
Expand Down
8 changes: 4 additions & 4 deletions app/jobs/schedule_authorization_request_mailjet_email_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def perform(authorization_request_id, authorization_request_status, mailjet_attr
return if authorization_request_status_changed?(authorization_request_status)

deliver_mailjet_email(
build_message(mailjet_attributes.stringify_keys)
build_message(mailjet_attributes.stringify_keys, @authorization_request.api)
)
rescue Mailjet::ApiError => e
affect_mailjet_context_for_sentry(e)
Expand All @@ -21,10 +21,10 @@ def perform(authorization_request_id, authorization_request_status, mailjet_attr

private

def build_message(mailjet_attributes)
def build_message(mailjet_attributes, api)
{
from_name: 'API Entreprise',
from_email: APIEntrepriseMailer.default_params[:from],
from_name: from_name(api),
from_email: from_email(api),
to: build_recipient_attributes(mailjet_attributes['to']),
cc: build_recipient_attributes(mailjet_attributes['cc']),
vars: mailjet_attributes['vars'],
Expand Down
27 changes: 23 additions & 4 deletions spec/jobs/schedule_authorization_request_mailjet_email_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
it 'calls Mailjet client with valid params' do
expect(Mailjet::Send).to receive(:create).with(
{
from_name: anything,
from_email: anything,
from_name: 'API Entreprise',
from_email: APIEntrepriseMailer.default_params[:from],
to: "#{to_user.full_name} <#{to_user.email}>",
vars: mailjet_template_vars,
'Mj-TemplateLanguage' => true,
Expand All @@ -83,8 +83,8 @@
it 'calls Mailjet client with valid params' do
expect(Mailjet::Send).to receive(:create).with(
{
from_name: anything,
from_email: anything,
from_name: 'API Entreprise',
from_email: APIEntrepriseMailer.default_params[:from],
to: "#{to_user.full_name} <#{to_user.email}>",
vars: mailjet_template_vars,
'Mj-TemplateLanguage' => true,
Expand Down Expand Up @@ -191,5 +191,24 @@
end
end
end

describe 'with api Particulier' do
let(:authorization_request) { create(:authorization_request, status: authorization_request_status, api: 'particulier') }

it 'calls Mailjet client with valid params' do
expect(Mailjet::Send).to receive(:create).with(
{
from_name: 'API Particulier',
from_email: APIParticulierMailer.default_params[:from],
to: "#{to_user.full_name} <#{to_user.email}>",
vars: mailjet_template_vars,
'Mj-TemplateLanguage' => true,
'Mj-TemplateID' => mailjet_template_id
}.stringify_keys
)

subject
end
end
end
end

0 comments on commit bca0292

Please sign in to comment.