Skip to content

Commit 8bd4590

Browse files
authored
Remove notification subscriptions when an employee is deactivated (#7089)
Signed-off-by: Denis Bykhov <[email protected]>
1 parent bf9ed35 commit 8bd4590

File tree

6 files changed

+51
-2
lines changed

6 files changed

+51
-2
lines changed

models/server-hr/src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ export function createModel (builder: Builder): void {
7676
}
7777
})
7878

79+
builder.createDoc(serverCore.class.Trigger, core.space.Model, {
80+
trigger: serverHr.trigger.OnEmployeeDeactivate,
81+
isAsync: true,
82+
txMatch: {
83+
_class: core.class.TxMixin,
84+
mixin: contact.mixin.Employee
85+
}
86+
})
87+
7988
builder.createDoc(serverCore.class.Trigger, core.space.Model, {
8089
trigger: serverHr.trigger.OnPublicHolidayCreate,
8190
txMatch: {

models/server-notification/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"dependencies": {
3131
"@hcengineering/activity": "^0.6.0",
3232
"@hcengineering/core": "^0.6.32",
33+
"@hcengineering/contact": "^0.6.24",
3334
"@hcengineering/model": "^0.6.11",
3435
"@hcengineering/model-chunter": "^0.6.0",
3536
"@hcengineering/model-core": "^0.6.0",

models/server-notification/src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import serverNotification, {
3333
type TypeMatch,
3434
type TypeMatchFunc
3535
} from '@hcengineering/server-notification'
36+
import contact from '@hcengineering/contact'
3637

3738
export { serverNotificationId } from '@hcengineering/server-notification'
3839

@@ -90,4 +91,13 @@ export function createModel (builder: Builder): void {
9091
builder.createDoc(serverCore.class.Trigger, core.space.Model, {
9192
trigger: serverNotification.trigger.OnDocRemove
9293
})
94+
95+
builder.createDoc(serverCore.class.Trigger, core.space.Model, {
96+
trigger: serverNotification.trigger.OnEmployeeDeactivate,
97+
isAsync: true,
98+
txMatch: {
99+
_class: core.class.TxMixin,
100+
mixin: contact.mixin.Employee
101+
}
102+
})
93103
}

server-plugins/hr/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export const serverHrId = 'server-hr' as Plugin
2929
export default plugin(serverHrId, {
3030
trigger: {
3131
OnEmployee: '' as Resource<TriggerFunc>,
32+
OnEmployeeDeactivate: '' as Resource<TriggerFunc>,
3233
OnDepartmentStaff: '' as Resource<TriggerFunc>,
3334
OnDepartmentRemove: '' as Resource<TriggerFunc>,
3435
OnRequestCreate: '' as Resource<TriggerFunc>,

server-plugins/notification-resources/src/index.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import activity, { ActivityMessage, DocUpdateMessage } from '@hcengineering/activity'
1818
import chunter, { ChatMessage } from '@hcengineering/chunter'
1919
import contact, {
20+
Employee,
2021
getAvatarProviderId,
2122
getGravatarUrl,
2223
Person,
@@ -1908,6 +1909,31 @@ async function OnActivityMessageRemove (message: ActivityMessage, control: Trigg
19081909
return res
19091910
}
19101911

1912+
async function OnEmployeeDeactivate (tx: TxCUD<Doc>, control: TriggerControl): Promise<Tx[]> {
1913+
const actualTx = TxProcessor.extractTx(tx)
1914+
if (core.class.TxMixin !== actualTx._class) {
1915+
return []
1916+
}
1917+
const ctx = actualTx as TxMixin<Person, Employee>
1918+
if (ctx.mixin !== contact.mixin.Employee || ctx.attributes.active !== false) {
1919+
return []
1920+
}
1921+
1922+
const targetAccount = control.modelDb.getAccountByPersonId(ctx.objectId) as PersonAccount[]
1923+
if (targetAccount.length === 0) return []
1924+
1925+
const res: Tx[] = []
1926+
for (const acc of targetAccount) {
1927+
const subscriptions = await control.findAll(control.ctx, notification.class.PushSubscription, {
1928+
user: acc._id
1929+
})
1930+
for (const sub of subscriptions) {
1931+
res.push(control.txFactory.createTxRemoveDoc(sub._class, sub.space, sub._id))
1932+
}
1933+
}
1934+
return res
1935+
}
1936+
19111937
async function OnDocRemove (originTx: TxCUD<Doc>, control: TriggerControl): Promise<Tx[]> {
19121938
const tx = TxProcessor.extractTx(originTx) as TxRemoveDoc<Doc>
19131939

@@ -1949,7 +1975,8 @@ export default async () => ({
19491975
trigger: {
19501976
OnAttributeCreate,
19511977
OnAttributeUpdate,
1952-
OnDocRemove
1978+
OnDocRemove,
1979+
OnEmployeeDeactivate
19531980
},
19541981
function: {
19551982
IsUserInFieldValueTypeMatch: isUserInFieldValueTypeMatch,

server-plugins/notification/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ export default plugin(serverNotificationId, {
168168
OnAttributeCreate: '' as Resource<TriggerFunc>,
169169
OnAttributeUpdate: '' as Resource<TriggerFunc>,
170170
OnReactionChanged: '' as Resource<TriggerFunc>,
171-
OnDocRemove: '' as Resource<TriggerFunc>
171+
OnDocRemove: '' as Resource<TriggerFunc>,
172+
OnEmployeeDeactivate: '' as Resource<TriggerFunc>
172173
},
173174
function: {
174175
IsUserInFieldValueTypeMatch: '' as TypeMatchFunc,

0 commit comments

Comments
 (0)