Skip to content

Commit

Permalink
Improve template creation
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwr18 committed Oct 10, 2024
1 parent 078227b commit b03f097
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 35 deletions.
6 changes: 2 additions & 4 deletions app/jobs/whats_app_adapter/create_templates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ def perform(organization_id:, token:)
@token = token
@waba_account_id = organization.three_sixty_dialog_client_waba_account_id
@waba_account_id = fetch_client_info if waba_account_id.blank? || organization.three_sixty_dialog_whats_app_template_namespace.blank?
existing_templates = WhatsAppAdapter::ThreeSixtyDialog::TemplateFetcherService.new(
waba_account_id: waba_account_id,
token: token
).call
existing_templates = WhatsAppAdapter::ThreeSixtyDialog::TemplateFetcherService.new(organization_id: organization.id).call

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|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,18 @@ module ThreeSixtyDialog
class CreateWelcomeMessageTemplateJob < ApplicationJob
def perform(organization_id:)
@organization = Organization.find_by(id: organization_id)
@token = WhatsAppAdapter::ThreeSixtyDialog::TokenFetcherService.call

@base_uri = ENV.fetch('THREE_SIXTY_DIALOG_PARTNER_REST_API_ENDPOINT', 'https://stoplight.io/mocks/360dialog/360dialog-partner-api/24588693')
@partner_id = ENV.fetch('THREE_SIXTY_DIALOG_PARTNER_ID', nil)
@waba_account_id = organization.three_sixty_dialog_client_waba_account_id
existing_templates = WhatsAppAdapter::ThreeSixtyDialog::TemplateFetcherService.new(
waba_account_id: waba_account_id,
token: token
).call
@base_uri = ENV.fetch('THREE_SIXTY_DIALOG_WHATS_APP_REST_API_ENDPOINT', 'https://stoplight.io/mocks/360dialog/360dialog-partner-api/24588693')
existing_templates = WhatsAppAdapter::ThreeSixtyDialog::TemplateFetcherService.new(organization_id: organization.id).call

if "welcome_message_#{organization.project_name.parameterize.underscore}".in?(existing_templates)
notify_admin_to_update_existing_template
else
create_welcome_message_template
end
end

attr_reader :organization, :token, :base_uri, :partner_id, :waba_account_id
attr_reader :organization, :base_uri

private

Expand All @@ -32,14 +27,8 @@ def notify_admin_to_update_existing_template
end

def create_welcome_message_template
url = URI.parse(
"#{base_uri}/partners/#{partner_id}/waba_accounts/#{waba_account_id}/waba_templates"
)
headers = {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: "Bearer #{token}"
}
url = URI.parse("#{base_uri}/v1/configs/templates")
headers = { 'D360-API-KEY' => organization.three_sixty_dialog_client_api_key, 'Content-Type' => 'application/json' }

request = Net::HTTP::Post.new(url.to_s, headers)
payload = welcome_message_template_payload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,17 @@
module WhatsAppAdapter
module ThreeSixtyDialog
class TemplateFetcherService
def initialize(waba_account_id:, token:)
@base_uri = ENV.fetch('THREE_SIXTY_DIALOG_PARTNER_REST_API_ENDPOINT', 'https://stoplight.io/mocks/360dialog/360dialog-partner-api/24588693')
@partner_id = ENV.fetch('THREE_SIXTY_DIALOG_PARTNER_ID', nil)
@waba_account_id = waba_account_id
@token = token
def initialize(organization_id:)
@organization = Organization.find(organization_id)
@base_uri = ENV.fetch('THREE_SIXTY_DIALOG_WHATS_APP_REST_API_ENDPOINT', 'https://stoplight.io/mocks/360dialog/360dialog-partner-api/24588693')
end

attr_reader :base_uri, :partner_id, :waba_account_id, :token
attr_reader :base_uri, :organization

def call
url = URI.parse(
"#{base_uri}/partners/#{partner_id}/waba_accounts/#{waba_account_id}/waba_templates"
)
headers = {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: "Bearer #{token}"
}
url = URI.parse("#{base_uri}/v1/configs/templates")
headers = { 'D360-API-KEY' => organization.three_sixty_dialog_client_api_key, 'Content-Type' => 'application/json' }

request = Net::HTTP::Get.new(url.to_s, headers)
response = Net::HTTP.start(url.host, url.port, use_ssl: true) do |http|
http.request(request)
Expand Down

0 comments on commit b03f097

Please sign in to comment.