fix: flatten template array for whatsapp templates (#6880)
- Refactor the WhatsApp template sync job - Fix the issue when fetching the next set of templates
This commit is contained in:
@@ -23,13 +23,24 @@ class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseServi
|
|||||||
end
|
end
|
||||||
|
|
||||||
def sync_templates
|
def sync_templates
|
||||||
response = HTTParty.get("#{business_account_path}/message_templates?access_token=#{whatsapp_channel.provider_config['api_key']}")
|
templates = fetch_whatsapp_templates("#{business_account_path}/message_templates?access_token=#{whatsapp_channel.provider_config['api_key']}")
|
||||||
|
|
||||||
return unless response.success?
|
whatsapp_channel.update(message_templates: templates, message_templates_last_updated: Time.now.utc) if templates.present?
|
||||||
|
end
|
||||||
|
|
||||||
@templates = response['data']
|
def fetch_whatsapp_templates(url)
|
||||||
fetch_next_templates(response)
|
response = HTTParty.get(url)
|
||||||
save_templates
|
return [] unless response.success?
|
||||||
|
|
||||||
|
next_url = next_url(response)
|
||||||
|
|
||||||
|
return response['data'] + fetch_whatsapp_templates(next_url) if next_url.present?
|
||||||
|
|
||||||
|
response['data']
|
||||||
|
end
|
||||||
|
|
||||||
|
def next_url(response)
|
||||||
|
response['paging'] ? response['paging']['next'] : ''
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_provider_config?
|
def validate_provider_config?
|
||||||
@@ -45,25 +56,6 @@ class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseServi
|
|||||||
"#{api_base_path}/v13.0/#{media_id}"
|
"#{api_base_path}/v13.0/#{media_id}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def fetch_next_templates(response)
|
|
||||||
loop do
|
|
||||||
break unless response['paging'] && response['paging']['next']
|
|
||||||
|
|
||||||
response = HTTParty.get(response['paging']['next'])
|
|
||||||
push_templates(response)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def push_templates(response)
|
|
||||||
@templates << response['data'] if response.success? && response['data'].any?
|
|
||||||
end
|
|
||||||
|
|
||||||
def save_templates
|
|
||||||
whatsapp_channel.update(message_templates: @templates, message_templates_last_updated: Time.now.utc)
|
|
||||||
end
|
|
||||||
|
|
||||||
def api_base_path
|
def api_base_path
|
||||||
ENV.fetch('WHATSAPP_CLOUD_BASE_URL', 'https://graph.facebook.com')
|
ENV.fetch('WHATSAPP_CLOUD_BASE_URL', 'https://graph.facebook.com')
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -120,12 +120,17 @@ describe Whatsapp::Providers::WhatsappCloudService do
|
|||||||
{ status: 200, headers: response_headers,
|
{ status: 200, headers: response_headers,
|
||||||
body: { data: [
|
body: { data: [
|
||||||
{ id: '123456789', name: 'next_template' }
|
{ id: '123456789', name: 'next_template' }
|
||||||
|
], paging: { next: 'https://graph.facebook.com/v14.0/123456789/message_templates?access_token=test_key' } }.to_json },
|
||||||
|
{ status: 200, headers: response_headers,
|
||||||
|
body: { data: [
|
||||||
|
{ id: '123456789', name: 'last_template' }
|
||||||
], paging: { prev: 'https://graph.facebook.com/v14.0/123456789/message_templates?access_token=test_key' } }.to_json }
|
], paging: { prev: 'https://graph.facebook.com/v14.0/123456789/message_templates?access_token=test_key' } }.to_json }
|
||||||
)
|
)
|
||||||
|
|
||||||
expect(subject.sync_templates).to be(true)
|
expect(subject.sync_templates).to be(true)
|
||||||
expect(whatsapp_channel.reload.message_templates.first).to eq({ id: '123456789', name: 'test_template' }.stringify_keys)
|
expect(whatsapp_channel.reload.message_templates.first).to eq({ id: '123456789', name: 'test_template' }.stringify_keys)
|
||||||
expect(whatsapp_channel.reload.message_templates.last).to eq([{ id: '123456789', name: 'next_template' }.stringify_keys])
|
expect(whatsapp_channel.reload.message_templates.second).to eq({ id: '123456789', name: 'next_template' }.stringify_keys)
|
||||||
|
expect(whatsapp_channel.reload.message_templates.last).to eq({ id: '123456789', name: 'last_template' }.stringify_keys)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user