feat: add auditlogs for account model (#7511)

This commit is contained in:
Vishnu Narayanan
2023-07-19 23:52:34 +05:30
committed by GitHub
parent 1d718b92b7
commit ea825d49da
6 changed files with 21 additions and 6 deletions

View File

@@ -30,6 +30,7 @@ const translationKeys = {
'accountuser:create': `AUDIT_LOGS.ACCOUNT_USER.ADD`,
'accountuser:update:self': `AUDIT_LOGS.ACCOUNT_USER.EDIT.SELF`,
'accountuser:update:other': `AUDIT_LOGS.ACCOUNT_USER.EDIT.OTHER`,
'account:update': `AUDIT_LOGS.ACCOUNT.EDIT`,
};
function extractAttrChange(attrChange) {

View File

@@ -55,6 +55,9 @@
"ADD": "%{agentName} created a new macro (#%{id})",
"EDIT": "%{agentName} updated a macro (#%{id})",
"DELETE": "%{agentName} deleted a macro (#%{id})"
},
"ACCOUNT": {
"EDIT": "%{agentName} updated the account configuration (#%{id})"
}
}
}

View File

@@ -2,6 +2,7 @@ module Enterprise::Audit::Account
extend ActiveSupport::Concern
included do
audited except: :updated_at, on: [:update]
has_associated_audits
end
end

View File

@@ -5,7 +5,11 @@ class Enterprise::AuditLog < Audited::Audit
def log_additional_information
# rubocop:disable Rails/SkipsModelValidations
update_columns(username: user&.email)
if auditable_type == 'Account' && auditable_id.present?
update_columns(associated_type: auditable_type, associated_id: auditable_id, username: user&.email)
else
update_columns(username: user&.email)
end
# rubocop:enable Rails/SkipsModelValidations
end
end

View File

@@ -47,13 +47,14 @@ RSpec.describe 'Enterprise Audit API', type: :request do
expect(response).to have_http_status(:success)
json_response = JSON.parse(response.body)
expect(json_response['audit_logs'][0]['auditable_type']).to eql('Inbox')
expect(json_response['audit_logs'][0]['action']).to eql('create')
expect(json_response['audit_logs'][0]['audited_changes']['name']).to eql(inbox.name)
expect(json_response['audit_logs'][0]['associated_id']).to eql(account.id)
expect(json_response['audit_logs'][1]['auditable_type']).to eql('Inbox')
expect(json_response['audit_logs'][1]['action']).to eql('create')
expect(json_response['audit_logs'][1]['audited_changes']['name']).to eql(inbox.name)
expect(json_response['audit_logs'][1]['associated_id']).to eql(account.id)
expect(json_response['current_page']).to be(1)
# contains audit log for account user as well
expect(json_response['total_entries']).to be(2)
# contains audit logs for account update(enable audit logs)
expect(json_response['total_entries']).to be(3)
end
end
end

View File

@@ -33,6 +33,11 @@ RSpec.describe Account do
# checking whether associated_audits method is present
expect(account.associated_audits.present?).to be false
end
it 'creates audit logs when account is updated' do
account.update(name: 'New Name')
expect(Audited::Audit.where(auditable_type: 'Account', action: 'update').count).to eq 1
end
end
it 'returns max limits from global config when enterprise version' do