Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: auditlog for team and inbox member updates (#7516)
- adds an audit log when an agent is added or removed from a team - adds an audit log when an agent is added or removed from an inbox Co-authored-by: Sojan Jose <sojan@pepalo.com>
- Loading branch information
1 parent
2df8327
commit 7b8a3fc
Showing
9 changed files
with
249 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module Enterprise::Audit::InboxMember | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
after_commit :create_audit_log_entry_on_create, on: :create | ||
after_commit :create_audit_log_entry_on_delete, on: :destroy | ||
end | ||
|
||
private | ||
|
||
def create_audit_log_entry_on_create | ||
create_audit_log_entry('create') | ||
end | ||
|
||
def create_audit_log_entry_on_delete | ||
create_audit_log_entry('destroy') | ||
end | ||
|
||
def create_audit_log_entry(action) | ||
return if inbox.blank? | ||
|
||
Enterprise::AuditLog.create( | ||
auditable_id: id, | ||
auditable_type: 'InboxMember', | ||
action: action, | ||
associated_id: inbox&.account_id, | ||
audited_changes: attributes.except('updated_at', 'created_at'), | ||
associated_type: 'Account' | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module Enterprise::Audit::TeamMember | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
after_commit :create_audit_log_entry_on_create, on: :create | ||
after_commit :create_audit_log_entry_on_delete, on: :destroy | ||
end | ||
|
||
private | ||
|
||
def create_audit_log_entry_on_create | ||
create_audit_log_entry('create') | ||
end | ||
|
||
def create_audit_log_entry_on_delete | ||
create_audit_log_entry('destroy') | ||
end | ||
|
||
def create_audit_log_entry(action) | ||
return if team.blank? | ||
|
||
Enterprise::AuditLog.create( | ||
auditable_id: id, | ||
auditable_type: 'TeamMember', | ||
action: action, | ||
associated_id: team&.account_id, | ||
audited_changes: attributes.except('updated_at', 'created_at'), | ||
associated_type: 'Account' | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe InboxMember, type: :model do | ||
let(:user) { create(:user) } | ||
let(:inbox) { create(:inbox) } | ||
let!(:inbox_member) { create(:inbox_member, inbox: inbox, user: user) } | ||
|
||
describe 'audit log' do | ||
context 'when inbox member is created' do | ||
it 'has associated audit log created' do | ||
expect(Audited::Audit.where(auditable: inbox_member, action: 'create').count).to eq(1) | ||
end | ||
|
||
it 'has user_id in audited_changes matching user.id' do | ||
audit_log = Audited::Audit.find_by(auditable: inbox_member, action: 'create') | ||
expect(audit_log.audited_changes['user_id']).to eq(user.id) | ||
end | ||
end | ||
|
||
context 'when inbox member is destroyed' do | ||
it 'has associated audit log created' do | ||
inbox_member.destroy | ||
audit_log = Audited::Audit.find_by(auditable: inbox_member, action: 'destroy') | ||
expect(audit_log).to be_present | ||
expect(audit_log.audited_changes['inbox_id']).to eq(inbox.id) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe TeamMember, type: :model do | ||
let(:user) { create(:user) } | ||
let(:team) { create(:team) } | ||
let!(:team_member) { create(:team_member, user: user, team: team) } | ||
|
||
describe 'audit log' do | ||
context 'when team member is created' do | ||
it 'has associated audit log created' do | ||
expect(Audited::Audit.where(auditable: team_member, action: 'create').count).to eq(1) | ||
end | ||
|
||
it 'has user_id in audited_changes matching user.id' do | ||
audit_log = Audited::Audit.find_by(auditable: team_member, action: 'create') | ||
expect(audit_log.audited_changes['user_id']).to eq(user.id) | ||
end | ||
end | ||
|
||
context 'when team member is destroyed' do | ||
it 'has associated audit log created' do | ||
team_member.destroy | ||
audit_log = Audited::Audit.find_by(auditable: team_member, action: 'destroy') | ||
expect(audit_log).to be_present | ||
expect(audit_log.audited_changes['team_id']).to eq(team.id) | ||
end | ||
end | ||
end | ||
end |