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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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:create': `AUDIT_LOGS.ACCOUNT_USER.ADD`,
'accountuser:update:self': `AUDIT_LOGS.ACCOUNT_USER.EDIT.SELF`, 'accountuser:update:self': `AUDIT_LOGS.ACCOUNT_USER.EDIT.SELF`,
'accountuser:update:other': `AUDIT_LOGS.ACCOUNT_USER.EDIT.OTHER`, '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:create': `AUDIT_LOGS.INBOX_MEMBER.ADD`,
'inboxmember:destroy': `AUDIT_LOGS.INBOX_MEMBER.REMOVE`, 'inboxmember:destroy': `AUDIT_LOGS.INBOX_MEMBER.REMOVE`,
'teammember:create': `AUDIT_LOGS.TEAM_MEMBER.ADD`, 'teammember:create': `AUDIT_LOGS.TEAM_MEMBER.ADD`,
@ -89,9 +90,9 @@ function handleAccountUserCreate(auditLogItem, translationPayload, agentList) {
} }
function handleAccountUserUpdate(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( translationPayload.user = getAgentName(
auditLogItem.auditable.user_id, auditLogItem.auditable?.user_id,
agentList agentList
); );
} }
@ -187,16 +188,25 @@ export function generateTranslationPayload(auditLogItem, agentList) {
return translationPayload; 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 => { export const generateLogActionKey = auditLogItem => {
const auditableType = auditLogItem.auditable_type.toLowerCase(); const auditableType = auditLogItem.auditable_type.toLowerCase();
const action = auditLogItem.action.toLowerCase(); const action = auditLogItem.action.toLowerCase();
let logActionKey = `${auditableType}:${action}`; let logActionKey = `${auditableType}:${action}`;
if (auditableType === 'accountuser' && action === 'update') { if (auditableType === 'accountuser' && action === 'update') {
logActionKey += logActionKey += getAccountUserUpdateSuffix(auditLogItem);
auditLogItem.user_id === auditLogItem.auditable.user_id
? ':self'
: ':other';
} }
return translationKeys[logActionKey] || ''; return translationKeys[logActionKey] || '';

View File

@ -178,5 +178,17 @@ describe('Helper functions', () => {
const logActionKey = generateLogActionKey(auditLogItem); const logActionKey = generateLogActionKey(auditLogItem);
expect(logActionKey).toEqual('AUDIT_LOGS.ACCOUNT_USER.EDIT.OTHER'); 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.", "404": "There are no Audit Logs available in this account.",
"TITLE": "Manage Audit Logs", "TITLE": "Manage Audit Logs",
"DESC": "Audit Logs are trails for events and actions in a Chatwoot System.", "DESC": "Audit Logs are trails for events and actions in a Chatwoot System.",
"TABLE_HEADER": [ "TABLE_HEADER": ["Activity", "Time", "IP Address"]
"Activity",
"Time",
"IP Address"
]
}, },
"API": { "API": {
"SUCCESS_MESSAGE": "AuditLogs retrieved successfully", "SUCCESS_MESSAGE": "AuditLogs retrieved successfully",
@ -31,7 +27,8 @@
"ADD": "%{agentName} invited %{invitee} to the account as an %{role}", "ADD": "%{agentName} invited %{invitee} to the account as an %{role}",
"EDIT": { "EDIT": {
"SELF": "%{agentName} changed their %{attributes} to %{values}", "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": { "INBOX": {