chore: Ensure template_sync timestamp is updated (#7265)

Fix for the cases where there are multiple channels with invalid provider config, which results in the templates sync scheduler failing to schedule jobs for valid channels.

fixes: https://linear.app/chatwoot/issue/CW-2032/bug-whatsapp-template-sync-failing-in-cloud
This commit is contained in:
Sojan Jose
2023-06-07 17:18:24 +05:30
committed by GitHub
parent 39eaed7766
commit 7e89e3a34a
3 changed files with 12 additions and 4 deletions

View File

@@ -50,6 +50,12 @@ class Channel::Whatsapp < ApplicationRecord
true true
end end
def mark_message_templates_updated
# rubocop:disable Rails/SkipsModelValidations
update_column(:message_templates_last_updated, Time.zone.now)
# rubocop:enable Rails/SkipsModelValidations
end
delegate :send_message, to: :provider_service delegate :send_message, to: :provider_service
delegate :send_template, to: :provider_service delegate :send_template, to: :provider_service
delegate :sync_templates, to: :provider_service delegate :sync_templates, to: :provider_service

View File

@@ -22,9 +22,10 @@ class Whatsapp::Providers::Whatsapp360DialogService < Whatsapp::Providers::BaseS
end end
def sync_templates def sync_templates
# ensuring that channels with wrong provider config wouldn't keep trying to sync templates
whatsapp_channel.mark_message_templates_updated
response = HTTParty.get("#{api_base_path}/configs/templates", headers: api_headers) response = HTTParty.get("#{api_base_path}/configs/templates", headers: api_headers)
whatsapp_channel[:message_templates] = response['waba_templates'] if response.success? whatsapp_channel.update(message_templates: response['waba_templates'], message_templates_last_updated: Time.now.utc) if response.success?
whatsapp_channel.update(message_templates_last_updated: Time.now.utc)
end end
def validate_provider_config? def validate_provider_config?

View File

@@ -23,9 +23,10 @@ class Whatsapp::Providers::WhatsappCloudService < Whatsapp::Providers::BaseServi
end end
def sync_templates def sync_templates
# ensuring that channels with wrong provider config wouldn't keep trying to sync templates
whatsapp_channel.mark_message_templates_updated
templates = fetch_whatsapp_templates("#{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']}")
whatsapp_channel[:message_templates] = templates if templates.present? whatsapp_channel.update(message_templates: templates, message_templates_last_updated: Time.now.utc) if templates.present?
whatsapp_channel.update(message_templates_last_updated: Time.now.utc)
end end
def fetch_whatsapp_templates(url) def fetch_whatsapp_templates(url)