From d731c972ad5620f425f2c92be9ca7c6b3201a61b Mon Sep 17 00:00:00 2001 From: Vishnu Narayanan Date: Wed, 10 Jan 2024 11:21:48 +0530 Subject: [PATCH] fix: skip auditlogs for whatsapp template sync (#8579) Skips audit logs for whatsapp_template sync Fixes: https://linear.app/chatwoot/issue/CW-2641/skip-whatsapp-template-updates-from-audit-logs --- .../app/models/enterprise/channelable.rb | 11 ++++ spec/enterprise/models/inbox_spec.rb | 52 +++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/enterprise/app/models/enterprise/channelable.rb b/enterprise/app/models/enterprise/channelable.rb index e46fdb2beb70..6fcae73d808e 100644 --- a/enterprise/app/models/enterprise/channelable.rb +++ b/enterprise/app/models/enterprise/channelable.rb @@ -21,6 +21,9 @@ def create_audit_log_entry return if audited_changes.blank? + # skip audit log creation if the only change is whatsapp channel template update + return if messaging_template_updates?(audited_changes) + Enterprise::AuditLog.create( auditable_id: auditable_id, auditable_type: auditable_type, @@ -30,5 +33,13 @@ def create_audit_log_entry audited_changes: audited_changes ) end + + def messaging_template_updates?(changes) + # if there is more than one key, return false + return false unless changes.keys.length == 1 + + # if the only key is message_templates_last_updated, return true + changes.key?('message_templates_last_updated') + end end end diff --git a/spec/enterprise/models/inbox_spec.rb b/spec/enterprise/models/inbox_spec.rb index 2cc7eeea9927..3e3e060d8397 100644 --- a/spec/enterprise/models/inbox_spec.rb +++ b/spec/enterprise/models/inbox_spec.rb @@ -97,4 +97,56 @@ end end end + + describe 'audit log with whatsapp channel' do + let(:channel) { create(:channel_whatsapp, provider: 'whatsapp_cloud', sync_templates: false, validate_provider_config: false) } + let(:inbox) { channel.inbox } + + before do + stub_request(:get, 'https://graph.facebook.com/v14.0//message_templates?access_token=test_key') + .with( + headers: { + 'Accept' => '*/*', + 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', + 'User-Agent' => 'Ruby' + } + ) + .to_return(status: 200, body: '', headers: {}) + end + + context 'when inbox is created' do + it 'has associated audit log created' do + expect(Audited::Audit.where(auditable_type: 'Inbox', action: 'create').count).to eq(1) + end + end + + context 'when inbox is updated' do + it 'has associated audit log created' do + inbox.update(name: 'Updated Inbox') + expect(Audited::Audit.where(auditable_type: 'Inbox', action: 'update').count).to eq(1) + end + end + + context 'when channel is updated' do + it 'has associated audit log created' do + previous_phone_number = inbox.channel.phone_number + new_phone_number = '1234567890' + inbox.channel.update(phone_number: new_phone_number) + + # check if channel update creates an audit log against inbox + expect(Audited::Audit.where(auditable_type: 'Inbox', action: 'update').count).to eq(1) + # Check for the specific phone_number update in the audit log + expect(Audited::Audit.where(auditable_type: 'Inbox', action: 'update', + audited_changes: { 'phone_number' => [previous_phone_number, new_phone_number] }).count).to eq(1) + end + end + + context 'when template sync runs' do + it 'has no associated audit log created' do + channel.sync_templates + # check if template sync does not create an audit log + expect(Audited::Audit.where(auditable_type: 'Inbox', action: 'update').count).to eq(0) + end + end + end end