fix: TypeError: Cannot read properties of null (reading 'user_id') (#9884)

This commit is contained in:
Sivin Varghese
2024-08-07 14:42:01 +05:30
committed by GitHub
parent 56e93d152d
commit b03a839809
3 changed files with 31 additions and 12 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`,
'accountuser:update:deleted': `AUDIT_LOGS.ACCOUNT_USER.EDIT.DELETED`,
'inboxmember:create': `AUDIT_LOGS.INBOX_MEMBER.ADD`,
'inboxmember:destroy': `AUDIT_LOGS.INBOX_MEMBER.REMOVE`,
'teammember:create': `AUDIT_LOGS.TEAM_MEMBER.ADD`,
@@ -89,9 +90,9 @@ function handleAccountUserCreate(auditLogItem, translationPayload, agentList) {
}
function handleAccountUserUpdate(auditLogItem, translationPayload, agentList) {
if (auditLogItem.user_id !== auditLogItem.auditable.user_id) {
if (auditLogItem.user_id !== auditLogItem.auditable?.user_id) {
translationPayload.user = getAgentName(
auditLogItem.auditable.user_id,
auditLogItem.auditable?.user_id,
agentList
);
}
@@ -187,16 +188,25 @@ export function generateTranslationPayload(auditLogItem, agentList) {
return translationPayload;
}
function getAccountUserUpdateSuffix(auditLogItem) {
if (auditLogItem.auditable === null) {
// If the user is deleted, we don't need to check if the user is the same as the auditLogItem.user_id
// Else we can use the deleted translation key
// It doesn't need the agent name
return ':deleted';
}
return auditLogItem.user_id === auditLogItem.auditable.user_id
? ':self'
: ':other';
}
export const generateLogActionKey = auditLogItem => {
const auditableType = auditLogItem.auditable_type.toLowerCase();
const action = auditLogItem.action.toLowerCase();
let logActionKey = `${auditableType}:${action}`;
if (auditableType === 'accountuser' && action === 'update') {
logActionKey +=
auditLogItem.user_id === auditLogItem.auditable.user_id
? ':self'
: ':other';
logActionKey += getAccountUserUpdateSuffix(auditLogItem);
}
return translationKeys[logActionKey] || '';

View File

@@ -178,5 +178,17 @@ describe('Helper functions', () => {
const logActionKey = generateLogActionKey(auditLogItem);
expect(logActionKey).toEqual('AUDIT_LOGS.ACCOUNT_USER.EDIT.OTHER');
});
it('should generate correct action key when updating a deleted user', () => {
const auditLogItem = {
auditable_type: 'AccountUser',
action: 'update',
user_id: 1,
auditable: null,
};
const logActionKey = generateLogActionKey(auditLogItem);
expect(logActionKey).toEqual('AUDIT_LOGS.ACCOUNT_USER.EDIT.DELETED');
});
});
});

View File

@@ -11,11 +11,7 @@
"404": "There are no Audit Logs available in this account.",
"TITLE": "Manage Audit Logs",
"DESC": "Audit Logs are trails for events and actions in a Chatwoot System.",
"TABLE_HEADER": [
"Activity",
"Time",
"IP Address"
]
"TABLE_HEADER": ["Activity", "Time", "IP Address"]
},
"API": {
"SUCCESS_MESSAGE": "AuditLogs retrieved successfully",
@@ -31,7 +27,8 @@
"ADD": "%{agentName} invited %{invitee} to the account as an %{role}",
"EDIT": {
"SELF": "%{agentName} changed their %{attributes} to %{values}",
"OTHER": "%{agentName} changed %{attributes} of %{user} to %{values}"
"OTHER": "%{agentName} changed %{attributes} of %{user} to %{values}",
"DELETED": "%{agentName} changed %{attributes} of a deleted user to %{values}"
}
},
"INBOX": {