Skip to content

Commit

Permalink
TSK-1236: correctly make map of all parents
Browse files Browse the repository at this point in the history
Signed-off-by: Vyacheslav Tumanov <me@slavatumanov.me>
  • Loading branch information
ThetaDR committed May 3, 2023
1 parent 8cb524d commit d9cd918
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions models/hr/src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -52,12 +52,26 @@ async function fixDuplicatesInDepartments (tx: TxOperations): Promise<void> {
}

async function fixDepartmentsFromStaff (tx: TxOperations): Promise<void> {
const ancestors: Map<Ref<Department>, Department[]> = new Map()
const departments = await tx.findAll(hr.class.Department, {})
const parentsWithDepartmentMap: Map<Ref<Department>, Department[]> = new Map()
const departmentsMap = toIdMap(departments)
const ancestors: Map<Ref<Department>, Ref<Department>> = 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 = []
Expand All @@ -66,7 +80,7 @@ async function fixDepartmentsFromStaff (tx: TxOperations): Promise<void> {
)
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))
Expand Down

0 comments on commit d9cd918

Please sign in to comment.