From b03a8398099da449434a5c40493c37607a568c40 Mon Sep 17 00:00:00 2001 From: Sivin Varghese <64252451+iamsivin@users.noreply.github.com> Date: Wed, 7 Aug 2024 14:42:01 +0530 Subject: [PATCH] fix: TypeError: Cannot read properties of null (reading 'user_id') (#9884) --- .../dashboard/helper/auditlogHelper.js | 22 ++++++++++++++----- .../helper/specs/auditlogHelper.spec.js | 12 ++++++++++ .../dashboard/i18n/locale/en/auditLogs.json | 9 +++----- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/app/javascript/dashboard/helper/auditlogHelper.js b/app/javascript/dashboard/helper/auditlogHelper.js index 0812ba7f6..6f5a4dede 100644 --- a/app/javascript/dashboard/helper/auditlogHelper.js +++ b/app/javascript/dashboard/helper/auditlogHelper.js @@ -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] || ''; diff --git a/app/javascript/dashboard/helper/specs/auditlogHelper.spec.js b/app/javascript/dashboard/helper/specs/auditlogHelper.spec.js index 8c2486bab..0ec4f3cdf 100644 --- a/app/javascript/dashboard/helper/specs/auditlogHelper.spec.js +++ b/app/javascript/dashboard/helper/specs/auditlogHelper.spec.js @@ -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'); + }); }); }); diff --git a/app/javascript/dashboard/i18n/locale/en/auditLogs.json b/app/javascript/dashboard/i18n/locale/en/auditLogs.json index e51579d29..263f78896 100644 --- a/app/javascript/dashboard/i18n/locale/en/auditLogs.json +++ b/app/javascript/dashboard/i18n/locale/en/auditLogs.json @@ -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": {