From 8bd4590816cad2549d7bd29348fc1e507eb0ac19 Mon Sep 17 00:00:00 2001 From: Denis Bykhov Date: Sat, 2 Nov 2024 22:12:32 +0500 Subject: [PATCH] Remove notification subscriptions when an employee is deactivated (#7089) Signed-off-by: Denis Bykhov --- models/server-hr/src/index.ts | 9 ++++++ models/server-notification/package.json | 1 + models/server-notification/src/index.ts | 10 +++++++ server-plugins/hr/src/index.ts | 1 + .../notification-resources/src/index.ts | 29 ++++++++++++++++++- server-plugins/notification/src/index.ts | 3 +- 6 files changed, 51 insertions(+), 2 deletions(-) diff --git a/models/server-hr/src/index.ts b/models/server-hr/src/index.ts index 321f7b2796..da31dc0808 100644 --- a/models/server-hr/src/index.ts +++ b/models/server-hr/src/index.ts @@ -76,6 +76,15 @@ export function createModel (builder: Builder): void { } }) + builder.createDoc(serverCore.class.Trigger, core.space.Model, { + trigger: serverHr.trigger.OnEmployeeDeactivate, + isAsync: true, + txMatch: { + _class: core.class.TxMixin, + mixin: contact.mixin.Employee + } + }) + builder.createDoc(serverCore.class.Trigger, core.space.Model, { trigger: serverHr.trigger.OnPublicHolidayCreate, txMatch: { diff --git a/models/server-notification/package.json b/models/server-notification/package.json index 3858ba2045..d24744d78d 100644 --- a/models/server-notification/package.json +++ b/models/server-notification/package.json @@ -30,6 +30,7 @@ "dependencies": { "@hcengineering/activity": "^0.6.0", "@hcengineering/core": "^0.6.32", + "@hcengineering/contact": "^0.6.24", "@hcengineering/model": "^0.6.11", "@hcengineering/model-chunter": "^0.6.0", "@hcengineering/model-core": "^0.6.0", diff --git a/models/server-notification/src/index.ts b/models/server-notification/src/index.ts index 9fc5874823..c1fcc304a1 100644 --- a/models/server-notification/src/index.ts +++ b/models/server-notification/src/index.ts @@ -33,6 +33,7 @@ import serverNotification, { type TypeMatch, type TypeMatchFunc } from '@hcengineering/server-notification' +import contact from '@hcengineering/contact' export { serverNotificationId } from '@hcengineering/server-notification' @@ -90,4 +91,13 @@ export function createModel (builder: Builder): void { builder.createDoc(serverCore.class.Trigger, core.space.Model, { trigger: serverNotification.trigger.OnDocRemove }) + + builder.createDoc(serverCore.class.Trigger, core.space.Model, { + trigger: serverNotification.trigger.OnEmployeeDeactivate, + isAsync: true, + txMatch: { + _class: core.class.TxMixin, + mixin: contact.mixin.Employee + } + }) } diff --git a/server-plugins/hr/src/index.ts b/server-plugins/hr/src/index.ts index 8ee4a36dd4..fd158b2e9c 100644 --- a/server-plugins/hr/src/index.ts +++ b/server-plugins/hr/src/index.ts @@ -29,6 +29,7 @@ export const serverHrId = 'server-hr' as Plugin export default plugin(serverHrId, { trigger: { OnEmployee: '' as Resource, + OnEmployeeDeactivate: '' as Resource, OnDepartmentStaff: '' as Resource, OnDepartmentRemove: '' as Resource, OnRequestCreate: '' as Resource, diff --git a/server-plugins/notification-resources/src/index.ts b/server-plugins/notification-resources/src/index.ts index 9145bb9588..42b48c936e 100644 --- a/server-plugins/notification-resources/src/index.ts +++ b/server-plugins/notification-resources/src/index.ts @@ -17,6 +17,7 @@ import activity, { ActivityMessage, DocUpdateMessage } from '@hcengineering/activity' import chunter, { ChatMessage } from '@hcengineering/chunter' import contact, { + Employee, getAvatarProviderId, getGravatarUrl, Person, @@ -1908,6 +1909,31 @@ async function OnActivityMessageRemove (message: ActivityMessage, control: Trigg return res } +async function OnEmployeeDeactivate (tx: TxCUD, control: TriggerControl): Promise { + const actualTx = TxProcessor.extractTx(tx) + if (core.class.TxMixin !== actualTx._class) { + return [] + } + const ctx = actualTx as TxMixin + if (ctx.mixin !== contact.mixin.Employee || ctx.attributes.active !== false) { + return [] + } + + const targetAccount = control.modelDb.getAccountByPersonId(ctx.objectId) as PersonAccount[] + if (targetAccount.length === 0) return [] + + const res: Tx[] = [] + for (const acc of targetAccount) { + const subscriptions = await control.findAll(control.ctx, notification.class.PushSubscription, { + user: acc._id + }) + for (const sub of subscriptions) { + res.push(control.txFactory.createTxRemoveDoc(sub._class, sub.space, sub._id)) + } + } + return res +} + async function OnDocRemove (originTx: TxCUD, control: TriggerControl): Promise { const tx = TxProcessor.extractTx(originTx) as TxRemoveDoc @@ -1949,7 +1975,8 @@ export default async () => ({ trigger: { OnAttributeCreate, OnAttributeUpdate, - OnDocRemove + OnDocRemove, + OnEmployeeDeactivate }, function: { IsUserInFieldValueTypeMatch: isUserInFieldValueTypeMatch, diff --git a/server-plugins/notification/src/index.ts b/server-plugins/notification/src/index.ts index 36880d26ff..c4fa751dda 100644 --- a/server-plugins/notification/src/index.ts +++ b/server-plugins/notification/src/index.ts @@ -168,7 +168,8 @@ export default plugin(serverNotificationId, { OnAttributeCreate: '' as Resource, OnAttributeUpdate: '' as Resource, OnReactionChanged: '' as Resource, - OnDocRemove: '' as Resource + OnDocRemove: '' as Resource, + OnEmployeeDeactivate: '' as Resource }, function: { IsUserInFieldValueTypeMatch: '' as TypeMatchFunc,