From b03f097a3d6a8016c2bf85fca997be01b0708584 Mon Sep 17 00:00:00 2001 From: Matthew Rider Date: Thu, 10 Oct 2024 21:00:25 +0200 Subject: [PATCH] Improve template creation --- .../whats_app_adapter/create_templates.rb | 6 ++--- .../create_welcome_message_template_job.rb | 23 +++++-------------- .../template_fetcher_service.rb | 21 ++++++----------- 3 files changed, 15 insertions(+), 35 deletions(-) diff --git a/app/jobs/whats_app_adapter/create_templates.rb b/app/jobs/whats_app_adapter/create_templates.rb index 854f70c8b..4e79789b0 100644 --- a/app/jobs/whats_app_adapter/create_templates.rb +++ b/app/jobs/whats_app_adapter/create_templates.rb @@ -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| diff --git a/app/jobs/whats_app_adapter/three_sixty_dialog/create_welcome_message_template_job.rb b/app/jobs/whats_app_adapter/three_sixty_dialog/create_welcome_message_template_job.rb index bcba52cce..fe9cd74f1 100644 --- a/app/jobs/whats_app_adapter/three_sixty_dialog/create_welcome_message_template_job.rb +++ b/app/jobs/whats_app_adapter/three_sixty_dialog/create_welcome_message_template_job.rb @@ -7,15 +7,10 @@ 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 @@ -23,7 +18,7 @@ def perform(organization_id:) end end - attr_reader :organization, :token, :base_uri, :partner_id, :waba_account_id + attr_reader :organization, :base_uri private @@ -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 diff --git a/app/services/whats_app_adapter/three_sixty_dialog/template_fetcher_service.rb b/app/services/whats_app_adapter/three_sixty_dialog/template_fetcher_service.rb index 197eb528a..4ff812458 100644 --- a/app/services/whats_app_adapter/three_sixty_dialog/template_fetcher_service.rb +++ b/app/services/whats_app_adapter/three_sixty_dialog/template_fetcher_service.rb @@ -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)