platform/plugins/hr-resources/src/utils.ts
Andrey Sobolev a4b3cb4a44
Allow to do departament d&d (#2194)
Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
2022-07-02 23:55:15 +07:00

55 lines
1.8 KiB
TypeScript

import { Employee, formatName } from '@anticrm/contact'
import { TxOperations } from '@anticrm/core'
import { Department } from '@anticrm/hr'
import { MessageBox } from '@anticrm/presentation'
import { showPopup } from '@anticrm/ui'
import hr from './plugin'
export async function addMember (client: TxOperations, employee?: Employee, value?: Department): Promise<void> {
if (employee === null || employee === undefined || value === undefined) {
return
}
const hierarchy = client.getHierarchy()
if (!hierarchy.hasMixin(employee, hr.mixin.Staff)) {
await client.createMixin(employee._id, employee._class, employee.space, hr.mixin.Staff, {
department: value._id
})
} else {
const staff = hierarchy.as(employee, hr.mixin.Staff)
if (staff.department === value._id) return
const current = await client.findOne(hr.class.Department, {
_id: staff.department
})
if (current !== undefined) {
await new Promise((resolve) => {
showPopup(
MessageBox,
{
label: hr.string.MoveStaff,
labelProps: { name: formatName(employee.name) },
message: hr.string.MoveStaffDescr,
params: {
current: current.name,
department: value.name
}
},
undefined,
(res?: boolean) => {
if (res === true && value !== undefined) {
void client.updateMixin(employee._id, employee._class, employee.space, hr.mixin.Staff, {
department: value._id
})
}
resolve(null)
}
)
})
} else {
await client.updateMixin(employee._id, employee._class, employee.space, hr.mixin.Staff, {
department: value._id
})
}
}
}