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>
This commit is contained in:
@@ -30,6 +30,10 @@ 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`,
|
||||
'inboxmember:create': `AUDIT_LOGS.INBOX_MEMBER.ADD`,
|
||||
'inboxmember:destroy': `AUDIT_LOGS.INBOX_MEMBER.REMOVE`,
|
||||
'teammember:create': `AUDIT_LOGS.TEAM_MEMBER.ADD`,
|
||||
'teammember:destroy': `AUDIT_LOGS.TEAM_MEMBER.REMOVE`,
|
||||
'account:update': `AUDIT_LOGS.ACCOUNT.EDIT`,
|
||||
};
|
||||
|
||||
@@ -102,6 +106,58 @@ function handleAccountUserUpdate(auditLogItem, translationPayload, agentList) {
|
||||
return translationPayload;
|
||||
}
|
||||
|
||||
function setUserInPayload(auditLogItem, translationPayload, agentList) {
|
||||
const userIdChange = auditLogItem.audited_changes.user_id;
|
||||
if (userIdChange && userIdChange !== undefined) {
|
||||
translationPayload.user = getAgentName(userIdChange, agentList);
|
||||
}
|
||||
return translationPayload;
|
||||
}
|
||||
|
||||
function setTeamIdInPayload(auditLogItem, translationPayload) {
|
||||
if (auditLogItem.audited_changes.team_id) {
|
||||
translationPayload.team_id = auditLogItem.audited_changes.team_id;
|
||||
}
|
||||
return translationPayload;
|
||||
}
|
||||
|
||||
function setInboxIdInPayload(auditLogItem, translationPayload) {
|
||||
if (auditLogItem.audited_changes.inbox_id) {
|
||||
translationPayload.inbox_id = auditLogItem.audited_changes.inbox_id;
|
||||
}
|
||||
return translationPayload;
|
||||
}
|
||||
|
||||
function handleInboxTeamMember(auditLogItem, translationPayload, agentList) {
|
||||
if (auditLogItem.audited_changes) {
|
||||
translationPayload = setUserInPayload(
|
||||
auditLogItem,
|
||||
translationPayload,
|
||||
agentList
|
||||
);
|
||||
translationPayload = setTeamIdInPayload(auditLogItem, translationPayload);
|
||||
translationPayload = setInboxIdInPayload(auditLogItem, translationPayload);
|
||||
}
|
||||
return translationPayload;
|
||||
}
|
||||
|
||||
function handleAccountUser(
|
||||
auditLogItem,
|
||||
translationPayload,
|
||||
agentList,
|
||||
action
|
||||
) {
|
||||
if (action === 'create') {
|
||||
return handleAccountUserCreate(auditLogItem, translationPayload, agentList);
|
||||
}
|
||||
|
||||
if (action === 'update') {
|
||||
return handleAccountUserUpdate(auditLogItem, translationPayload, agentList);
|
||||
}
|
||||
|
||||
return translationPayload;
|
||||
}
|
||||
|
||||
export function generateTranslationPayload(auditLogItem, agentList) {
|
||||
let translationPayload = {
|
||||
agentName: getAgentName(auditLogItem.user_id, agentList),
|
||||
@@ -112,21 +168,20 @@ export function generateTranslationPayload(auditLogItem, agentList) {
|
||||
const action = auditLogItem.action.toLowerCase();
|
||||
|
||||
if (auditableType === 'accountuser') {
|
||||
if (action === 'create') {
|
||||
translationPayload = handleAccountUserCreate(
|
||||
auditLogItem,
|
||||
translationPayload,
|
||||
agentList
|
||||
);
|
||||
}
|
||||
translationPayload = handleAccountUser(
|
||||
auditLogItem,
|
||||
translationPayload,
|
||||
agentList,
|
||||
action
|
||||
);
|
||||
}
|
||||
|
||||
if (action === 'update') {
|
||||
translationPayload = handleAccountUserUpdate(
|
||||
auditLogItem,
|
||||
translationPayload,
|
||||
agentList
|
||||
);
|
||||
}
|
||||
if (auditableType === 'inboxmember' || auditableType === 'teammember') {
|
||||
translationPayload = handleInboxTeamMember(
|
||||
auditLogItem,
|
||||
translationPayload,
|
||||
agentList
|
||||
);
|
||||
}
|
||||
|
||||
return translationPayload;
|
||||
|
||||
@@ -98,6 +98,48 @@ describe('Helper functions', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle InboxMember or TeamMember', () => {
|
||||
const auditLogItemInboxMember = {
|
||||
auditable_type: 'InboxMember',
|
||||
action: 'create',
|
||||
audited_changes: {
|
||||
user_id: 2,
|
||||
},
|
||||
user_id: 1,
|
||||
auditable_id: 789,
|
||||
};
|
||||
|
||||
const payloadInboxMember = generateTranslationPayload(
|
||||
auditLogItemInboxMember,
|
||||
agentList
|
||||
);
|
||||
expect(payloadInboxMember).toEqual({
|
||||
agentName: 'Agent 1',
|
||||
id: 789,
|
||||
user: 'Agent 2',
|
||||
});
|
||||
|
||||
const auditLogItemTeamMember = {
|
||||
auditable_type: 'TeamMember',
|
||||
action: 'create',
|
||||
audited_changes: {
|
||||
user_id: 3,
|
||||
},
|
||||
user_id: 1,
|
||||
auditable_id: 789,
|
||||
};
|
||||
|
||||
const payloadTeamMember = generateTranslationPayload(
|
||||
auditLogItemTeamMember,
|
||||
agentList
|
||||
);
|
||||
expect(payloadTeamMember).toEqual({
|
||||
agentName: 'Agent 1',
|
||||
id: 789,
|
||||
user: 'Agent 3',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle generic case like Team create', () => {
|
||||
const auditLogItem = {
|
||||
auditable_type: 'Team',
|
||||
|
||||
@@ -56,8 +56,16 @@
|
||||
"EDIT": "%{agentName} updated a macro (#%{id})",
|
||||
"DELETE": "%{agentName} deleted a macro (#%{id})"
|
||||
},
|
||||
"INBOX_MEMBER": {
|
||||
"ADD": "%{agentName} added %{user} to the inbox(#%{inbox_id})",
|
||||
"REMOVE": "%{agentName} removed %{user} from the inbox(#%{inbox_id})"
|
||||
},
|
||||
"TEAM_MEMBER": {
|
||||
"ADD": "%{agentName} added %{user} to the team(#%{team_id})",
|
||||
"REMOVE": "%{agentName} removed %{user} from the team(#%{team_id})"
|
||||
},
|
||||
"ACCOUNT": {
|
||||
"EDIT": "%{agentName} updated the account configuration (#%{id})"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user