From d9cd918859d30351d61577683145605b57d2c835 Mon Sep 17 00:00:00 2001 From: Vyacheslav Tumanov Date: Wed, 3 May 2023 16:54:12 +0500 Subject: [PATCH] TSK-1236: correctly make map of all parents Signed-off-by: Vyacheslav Tumanov --- models/hr/src/migration.ts | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/models/hr/src/migration.ts b/models/hr/src/migration.ts index 561130958f8..b0b5b3c71ad 100644 --- a/models/hr/src/migration.ts +++ b/models/hr/src/migration.ts @@ -14,7 +14,7 @@ // import contact, { Employee } from '@hcengineering/contact' -import { DOMAIN_TX, Ref, TxCollectionCUD, TxCreateDoc, TxOperations, TxUpdateDoc } from '@hcengineering/core' +import { DOMAIN_TX, Ref, toIdMap, TxCollectionCUD, TxCreateDoc, TxOperations, TxUpdateDoc } from '@hcengineering/core' import { Department, Request, TzDate } from '@hcengineering/hr' import { MigrateOperation, MigrationClient, MigrationUpgradeClient } from '@hcengineering/model' import core, { DOMAIN_SPACE } from '@hcengineering/model-core' @@ -52,12 +52,26 @@ async function fixDuplicatesInDepartments (tx: TxOperations): Promise { } async function fixDepartmentsFromStaff (tx: TxOperations): Promise { - const ancestors: Map, Department[]> = new Map() const departments = await tx.findAll(hr.class.Department, {}) + const parentsWithDepartmentMap: Map, Department[]> = new Map() + const departmentsMap = toIdMap(departments) + const ancestors: Map, Ref> = new Map() for (const department of departments) { - const current = ancestors.get(department.space) ?? [] - current.push(department) - ancestors.set(department._id, current) + if (department._id === hr.ids.Head) continue + ancestors.set(department._id, department.space) + } + for (const departmentTest of departments) { + const parents: Department[] = parentsWithDepartmentMap.get(departmentTest._id) ?? [] + let _id = departmentTest._id + while (true) { + const department = departmentsMap.get(_id) + if (department === undefined) break + if (!parents.includes(department)) parents.push(department) + const next = ancestors.get(department._id) + if (next === undefined) break + _id = next + } + parentsWithDepartmentMap.set(departmentTest._id, parents) } const staff = await tx.findAll(hr.mixin.Staff, {}) const promises = [] @@ -66,7 +80,7 @@ async function fixDepartmentsFromStaff (tx: TxOperations): Promise { ) for (const st of staff) { if (st.department == null) continue - const correctDepartments: Department[] = ancestors.get(st.department) ?? [] + const correctDepartments: Department[] = parentsWithDepartmentMap.get(st.department) ?? [] promises.push( ...departments .filter((department) => !correctDepartments.includes(department))