2023-04-06 13:52:32 +03:00
|
|
|
import { Organization } from '@hcengineering/contact'
|
2024-08-22 14:03:42 +03:00
|
|
|
import core, {
|
|
|
|
Account,
|
|
|
|
Client,
|
|
|
|
Data,
|
|
|
|
Doc,
|
|
|
|
Ref,
|
|
|
|
SortingOrder,
|
|
|
|
Status,
|
|
|
|
TxOperations,
|
2024-11-29 12:58:50 +03:00
|
|
|
generateId
|
2024-08-22 14:03:42 +03:00
|
|
|
} from '@hcengineering/core'
|
2023-04-13 18:25:34 +03:00
|
|
|
import recruit, { Applicant, Vacancy } from '@hcengineering/recruit'
|
2024-03-05 08:38:10 +03:00
|
|
|
import task, { ProjectType, makeRank } from '@hcengineering/task'
|
2023-04-06 13:52:32 +03:00
|
|
|
|
|
|
|
export async function createVacancy (
|
|
|
|
rawClient: Client,
|
|
|
|
name: string,
|
2023-10-27 08:22:43 +03:00
|
|
|
typeId: Ref<ProjectType>,
|
2023-04-06 13:52:32 +03:00
|
|
|
account: Ref<Account>,
|
|
|
|
company?: Ref<Organization>
|
|
|
|
): Promise<Ref<Vacancy>> {
|
|
|
|
const client = new TxOperations(rawClient, account)
|
2023-10-27 08:22:43 +03:00
|
|
|
const type = await client.findOne(task.class.ProjectType, { _id: typeId })
|
|
|
|
if (type === undefined) {
|
|
|
|
throw Error(`Failed to find target project type: ${typeId}`)
|
2023-04-06 13:52:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const sequence = await client.findOne(task.class.Sequence, { attachedTo: recruit.class.Vacancy })
|
|
|
|
if (sequence === undefined) {
|
|
|
|
throw new Error('sequence object not found')
|
|
|
|
}
|
|
|
|
|
|
|
|
const incResult = await client.update(sequence, { $inc: { sequence: 1 } }, true)
|
|
|
|
|
2024-08-22 14:03:42 +03:00
|
|
|
const id: Ref<Vacancy> = generateId()
|
|
|
|
await client.createDoc(
|
|
|
|
recruit.class.Vacancy,
|
|
|
|
core.space.Space,
|
|
|
|
{
|
|
|
|
name,
|
|
|
|
description: type.shortDescription ?? '',
|
2024-11-29 12:58:50 +03:00
|
|
|
fullDescription: null,
|
2024-08-22 14:03:42 +03:00
|
|
|
private: false,
|
|
|
|
archived: false,
|
|
|
|
company,
|
|
|
|
number: (incResult as any).object.sequence,
|
|
|
|
members: [],
|
|
|
|
type: typeId
|
|
|
|
},
|
|
|
|
id
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO type.description
|
2023-04-06 13:52:32 +03:00
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createApplication (
|
|
|
|
client: TxOperations,
|
2023-10-27 08:22:43 +03:00
|
|
|
selectedState: Status,
|
2023-04-06 13:52:32 +03:00
|
|
|
_space: Ref<Vacancy>,
|
2023-04-13 18:25:34 +03:00
|
|
|
doc: Doc,
|
|
|
|
data: Data<Applicant>
|
2023-04-06 13:52:32 +03:00
|
|
|
): Promise<void> {
|
|
|
|
if (selectedState === undefined) {
|
|
|
|
throw new Error(`Please select initial state:${_space}`)
|
|
|
|
}
|
|
|
|
const sequence = await client.findOne(task.class.Sequence, { attachedTo: recruit.class.Applicant })
|
|
|
|
if (sequence === undefined) {
|
|
|
|
throw new Error('sequence object not found')
|
|
|
|
}
|
|
|
|
|
|
|
|
const lastOne = await client.findOne(recruit.class.Applicant, {}, { sort: { rank: SortingOrder.Descending } })
|
|
|
|
const incResult = await client.update(sequence, { $inc: { sequence: 1 } }, true)
|
|
|
|
|
|
|
|
await client.addCollection(recruit.class.Applicant, _space, doc._id, recruit.mixin.Candidate, 'applications', {
|
2023-04-13 18:25:34 +03:00
|
|
|
...data,
|
2023-10-27 08:22:43 +03:00
|
|
|
status: selectedState._id,
|
2023-04-06 13:52:32 +03:00
|
|
|
number: (incResult as any).object.sequence,
|
2024-03-05 08:38:10 +03:00
|
|
|
rank: makeRank(lastOne?.rank, undefined)
|
2023-04-06 13:52:32 +03:00
|
|
|
})
|
|
|
|
}
|