-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix create templates job failing on first run (#2061)
Fixes #2044 The first time the job is run the three_sixty_dialog_client_waba_account_id is nil so it calls the fetch_client_info method. This method successfully updates the organization, but as it's the last line of the method it saves the return of the update `true` to the local variable that is used in the url to create the templates. This results in an error. Subsequent times it's ran it does not error because it uses the value of the organization's attribute, which is the correct value.
- Loading branch information
Showing
20 changed files
with
346 additions
and
391 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
70 changes: 70 additions & 0 deletions
70
app/jobs/whats_app_adapter/three_sixty_dialog/create_templates_job.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,70 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'net/http' | ||
|
||
module WhatsAppAdapter | ||
module ThreeSixtyDialog | ||
class CreateTemplatesJob < ApplicationJob | ||
def perform(organization_id:) | ||
organization = Organization.find(organization_id) | ||
|
||
existing_templates = WhatsAppAdapter::ThreeSixtyDialog::TemplateFetcherService.call(organization_id: organization.id) | ||
|
||
templates_to_create_array = whats_app_templates.keys.difference(existing_templates) | ||
templates_to_create = whats_app_templates.select { |key, _value| key.in?(templates_to_create_array) } | ||
templates_to_create.each do |key, value| | ||
WhatsAppAdapter::ThreeSixtyDialog::TemplateCreatorService.call(organization_id: organization.id, | ||
payload: new_request_template_payload(key, value)) | ||
end | ||
end | ||
|
||
private | ||
|
||
# rubocop:disable Style/FormatStringToken | ||
def whats_app_templates | ||
I18n.t('.')[:adapter][:whats_app][:request_template].transform_values do |value| | ||
value.gsub('%{first_name}', '{{1}}').gsub('%{request_title}', '{{2}}') | ||
end | ||
end | ||
# rubocop:enable Style/FormatStringToken | ||
|
||
# rubocop:disable Metrics/MethodLength | ||
def new_request_template_payload(template_name, template_text) | ||
{ | ||
name: template_name, | ||
category: 'MARKETING', | ||
components: [ | ||
{ | ||
type: 'BODY', | ||
text: template_text, | ||
example: { | ||
body_text: [ | ||
[ | ||
'Jakob', | ||
'Familie und Freizeit' | ||
] | ||
] | ||
} | ||
}, | ||
{ | ||
type: 'BUTTONS', | ||
buttons: [ | ||
{ | ||
type: 'QUICK_REPLY', | ||
text: 'Antworten' | ||
}, | ||
{ | ||
type: 'QUICK_REPLY', | ||
text: 'Mehr Infos' | ||
} | ||
] | ||
} | ||
], | ||
language: 'de', | ||
allow_category_change: true | ||
} | ||
end | ||
# rubocop:enable Metrics/MethodLength | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApplicationService | ||
def self.call(...) | ||
new(...).call | ||
end | ||
end |
Oops, something went wrong.