Skip to content

Commit 4501a1c

Browse files
authored
Fix hr module employee inactivate handler (#7079)
1 parent c10b0a6 commit 4501a1c

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import {
4949
} from '@hcengineering/server-notification-resources'
5050

5151
async function getOldDepartment (
52-
currentTx: TxMixin<Employee, Staff> | TxUpdateDoc<Employee>,
52+
currentTx: TxMixin<Employee, Staff>,
5353
control: TriggerControl
5454
): Promise<Ref<Department> | undefined> {
5555
const txes = await control.findAll<TxMixin<Employee, Staff>>(
@@ -73,7 +73,7 @@ async function getOldDepartment (
7373
async function buildHierarchy (_id: Ref<Department>, control: TriggerControl): Promise<Department[]> {
7474
const res: Department[] = []
7575
const ancestors = new Map<Ref<Department>, Ref<Department>>()
76-
const departments = await control.findAll(control.ctx, hr.class.Department, {})
76+
const departments = await control.queryFind(control.ctx, hr.class.Department, {})
7777
for (const department of departments) {
7878
if (department._id === hr.ids.Head || department.parent === undefined) continue
7979
ancestors.set(department._id, department.parent)
@@ -240,20 +240,20 @@ export async function OnEmployee (tx: Tx, control: TriggerControl): Promise<Tx[]
240240
*/
241241
export async function OnEmployeeDeactivate (tx: Tx, control: TriggerControl): Promise<Tx[]> {
242242
const actualTx = TxProcessor.extractTx(tx)
243-
if (core.class.TxUpdateDoc !== actualTx._class) {
243+
if (core.class.TxMixin !== actualTx._class) {
244244
return []
245245
}
246-
const ctx = actualTx as TxUpdateDoc<Employee>
247-
if (ctx.objectClass !== contact.mixin.Employee || ctx.operations.active !== false) {
246+
const ctx = actualTx as TxMixin<Person, Employee>
247+
if (ctx.mixin !== contact.mixin.Employee || ctx.attributes.active !== false) {
248248
return []
249249
}
250250

251251
const targetAccount = control.modelDb.getAccountByPersonId(ctx.objectId) as PersonAccount[]
252252
if (targetAccount.length === 0) return []
253-
const lastDepartment = await getOldDepartment(ctx, control)
254-
if (lastDepartment === undefined) return []
253+
const set = new Set(targetAccount.map((it) => it._id))
255254

256-
const removed = await buildHierarchy(lastDepartment, control)
255+
const departments = await control.queryFind(control.ctx, hr.class.Department, {})
256+
const removed = departments.filter((dep) => dep.members.some((p) => set.has(p)))
257257
return getTxes(
258258
control.txFactory,
259259
targetAccount.map((it) => it._id),

0 commit comments

Comments
 (0)