Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve template creation #2062

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading