diff --git a/server-plugins/notification-resources/package.json b/server-plugins/notification-resources/package.json index 0c68a0c66b..9e2dd97a8b 100644 --- a/server-plugins/notification-resources/package.json +++ b/server-plugins/notification-resources/package.json @@ -39,6 +39,7 @@ }, "dependencies": { "@hcengineering/activity": "^0.6.0", + "@hcengineering/analytics": "^0.6.0", "@hcengineering/core": "^0.6.32", "@hcengineering/platform": "^0.6.11", "@hcengineering/server-core": "^0.6.1", diff --git a/server-plugins/notification-resources/src/index.ts b/server-plugins/notification-resources/src/index.ts index 47388689e6..577d09b27e 100644 --- a/server-plugins/notification-resources/src/index.ts +++ b/server-plugins/notification-resources/src/index.ts @@ -81,6 +81,7 @@ import { workbenchId } from '@hcengineering/workbench' import webpush, { WebPushError } from 'web-push' import { encodeObjectURI } from '@hcengineering/view' import serverView from '@hcengineering/server-view' +import { Analytics } from '@hcengineering/analytics' import { Content, NotifyResult, NotifyParams } from './types' import { @@ -463,8 +464,9 @@ async function activityInboxNotificationToText (doc: Data>, hierarchy: Hierarchy) return hierarchy.classHierarchyMixin(_class, serverNotification.mixin.TextPresenter) } +async function getSenderName ( + accountId: Ref, + control: TriggerControl, + cache: Map, Doc> +): Promise { + if (accountId === core.account.System) { + return await translate(core.string.System, {}) + } + + const account = await getPersonAccountById(accountId, control) + + if (account === undefined) { + console.error('Cannot find person account: ', accountId) + Analytics.handleError(new Error(`Cannot find person account ${accountId}`)) + return '' + } + + const person = + (cache.get(accountId) as Person) ?? (await control.findAll(contact.class.Person, { _id: account.person }))[0] + + if (person === undefined) { + console.error('Cannot find person', { accountId: account._id, person: account.person }) + Analytics.handleError(new Error(`Cannot find person ${account.person}`)) + + return '' + } + + return formatName(person.name, control.branding?.lastNameFirst) +} + async function getFallbackNotificationFullfillment ( object: Doc, originTx: TxCUD, @@ -316,23 +348,8 @@ async function getFallbackNotificationFullfillment ( } const tx = TxProcessor.extractTx(originTx) - const account = control.modelDb.getObject(tx.modifiedBy) as PersonAccount - if (account !== undefined) { - const person = - (cache.get(account.person) as Person) ?? (await control.findAll(contact.class.Person, { _id: account.person }))[0] - if (person !== undefined) { - intlParams.senderName = formatName(person.name, control.branding?.lastNameFirst) - cache.set(person._id, person) - } else { - console.error('Cannot find person: ', { accountId: account._id, person: account.person }) - } - } else if (tx.modifiedBy === core.account.System) { - intlParams.senderName = await translate(core.string.System, {}) - } else { - console.error('Cannot find person account by _id: ', tx.modifiedBy) - } - intlParams.senderName = intlParams.senderName ?? '' + intlParams.senderName = await getSenderName(tx.modifiedBy as Ref, control, cache) if (tx._class === core.class.TxUpdateDoc) { const updateTx = tx as TxUpdateDoc