fix: setup webhook for create and update should be done after db commit (#12176)
## Reference https://github.com/chatwoot/chatwoot/pull/12149#issuecomment-3178108388 ## Description setup_webhook was done before the save, and hence the meta webhook validation might fail because of a race condition where the facebook validation is done before we saving the entry to the database. ## Type of change Please delete options that are not relevant. - [ ] Bug fix (non-breaking change which fixes an issue) ## How Has This Been Tested? - New inbox creation, webhook validation - Existing inbox update, webhook validation - <img width="614" height="674" alt="image" src="https://github.com/user-attachments/assets/be223945-deed-475a-82e5-3ae9c54a13fa" /> ## Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my code - [ ] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules --------- Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
This commit is contained in:
committed by
GitHub
parent
a88fef2e1d
commit
6b42ff8d39
@@ -32,7 +32,6 @@ class Channel::Whatsapp < ApplicationRecord
|
||||
validates :phone_number, presence: true, uniqueness: true
|
||||
validate :validate_provider_config
|
||||
|
||||
before_save :setup_webhooks
|
||||
after_create :sync_templates
|
||||
before_destroy :teardown_webhooks
|
||||
|
||||
@@ -60,6 +59,13 @@ class Channel::Whatsapp < ApplicationRecord
|
||||
delegate :media_url, to: :provider_service
|
||||
delegate :api_headers, to: :provider_service
|
||||
|
||||
def setup_webhooks
|
||||
perform_webhook_setup
|
||||
rescue StandardError => e
|
||||
Rails.logger.error "[WHATSAPP] Webhook setup failed: #{e.message}"
|
||||
prompt_reauthorization!
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def ensure_webhook_verify_token
|
||||
@@ -70,34 +76,6 @@ class Channel::Whatsapp < ApplicationRecord
|
||||
errors.add(:provider_config, 'Invalid Credentials') unless provider_service.validate_provider_config?
|
||||
end
|
||||
|
||||
def setup_webhooks
|
||||
return unless should_setup_webhooks?
|
||||
|
||||
perform_webhook_setup
|
||||
rescue StandardError => e
|
||||
handle_webhook_setup_error(e)
|
||||
end
|
||||
|
||||
def provider_config_changed?
|
||||
will_save_change_to_provider_config?
|
||||
end
|
||||
|
||||
def should_setup_webhooks?
|
||||
whatsapp_cloud_provider? && embedded_signup_source? && webhook_config_present? && provider_config_changed?
|
||||
end
|
||||
|
||||
def whatsapp_cloud_provider?
|
||||
provider == 'whatsapp_cloud'
|
||||
end
|
||||
|
||||
def embedded_signup_source?
|
||||
provider_config['source'] == 'embedded_signup'
|
||||
end
|
||||
|
||||
def webhook_config_present?
|
||||
provider_config['business_account_id'].present? && provider_config['api_key'].present?
|
||||
end
|
||||
|
||||
def perform_webhook_setup
|
||||
business_account_id = provider_config['business_account_id']
|
||||
api_key = provider_config['api_key']
|
||||
@@ -105,12 +83,6 @@ class Channel::Whatsapp < ApplicationRecord
|
||||
Whatsapp::WebhookSetupService.new(self, business_account_id, api_key).perform
|
||||
end
|
||||
|
||||
def handle_webhook_setup_error(error)
|
||||
Rails.logger.error "[WHATSAPP] Webhook setup failed: #{error.message}"
|
||||
# Don't raise the error to prevent channel creation from failing
|
||||
# Webhooks can be retried later
|
||||
end
|
||||
|
||||
def teardown_webhooks
|
||||
Whatsapp::WebhookTeardownService.new(self).perform
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user