Fix TSK-104 Create Candidate hang (#2072)

Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
This commit is contained in:
Andrey Sobolev 2022-06-15 01:00:55 +07:00 committed by GitHub
parent 665ef78199
commit 51b19dece2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,7 +15,7 @@
<script lang="ts"> <script lang="ts">
import type { Contact, Employee, Person } from '@anticrm/contact' import type { Contact, Employee, Person } from '@anticrm/contact'
import contact from '@anticrm/contact' import contact from '@anticrm/contact'
import { Account, Class, Client, Doc, generateId, Ref, SortingOrder } from '@anticrm/core' import { Account, Class, Client, Doc, generateId, Ref, SortingOrder, Space } from '@anticrm/core'
import { getResource, OK, Resource, Severity, Status } from '@anticrm/platform' import { getResource, OK, Resource, Severity, Status } from '@anticrm/platform'
import { Card, createQuery, getClient, SpaceSelector, UserBox } from '@anticrm/presentation' import { Card, createQuery, getClient, SpaceSelector, UserBox } from '@anticrm/presentation'
import type { Applicant, Candidate } from '@anticrm/recruit' import type { Applicant, Candidate } from '@anticrm/recruit'
@ -44,6 +44,8 @@
let status: Status = OK let status: Status = OK
let createMore: boolean = false let createMore: boolean = false
let _space = space
let doc: Applicant = { let doc: Applicant = {
state: '' as Ref<State>, state: '' as Ref<State>,
doneState: null, doneState: null,
@ -71,9 +73,12 @@
} }
async function createApplication () { async function createApplication () {
const state = await client.findOne(task.class.State, { space: doc.space, _id: doc.state }) if (selectedState === undefined) {
throw new Error(`Please select initial state:${_space}`)
}
const state = await client.findOne(task.class.State, { space: _space, _id: selectedState?._id })
if (state === undefined) { if (state === undefined) {
throw new Error(`create application: state not found space:${doc.space}`) throw new Error(`create application: state not found space:${_space}`)
} }
const sequence = await client.findOne(task.class.Sequence, { attachedTo: recruit.class.Applicant }) const sequence = await client.findOne(task.class.Sequence, { attachedTo: recruit.class.Applicant })
if (sequence === undefined) { if (sequence === undefined) {
@ -103,7 +108,7 @@
await client.addCollection( await client.addCollection(
recruit.class.Applicant, recruit.class.Applicant,
doc.space, _space,
candidateInstance._id, candidateInstance._id,
recruit.mixin.Candidate, recruit.mixin.Candidate,
'applications', 'applications',
@ -121,7 +126,7 @@
if (createMore) { if (createMore) {
// Prepare for next // Prepare for next
doc = { doc = {
state: selectedState._id, state: selectedState?._id as Ref<State>,
doneState: null, doneState: null,
number: 0, number: 0,
assignee: assignee, assignee: assignee,
@ -129,7 +134,7 @@
attachedTo: candidate, attachedTo: candidate,
attachedToClass: recruit.mixin.Candidate, attachedToClass: recruit.mixin.Candidate,
_class: recruit.class.Applicant, _class: recruit.class.Applicant,
space: space, space: _space,
_id: generateId(), _id: generateId(),
collection: 'applications', collection: 'applications',
modifiedOn: Date.now(), modifiedOn: Date.now(),
@ -144,42 +149,47 @@
action: Resource<<T extends Doc>(doc: T, client: Client) => Promise<Status>> action: Resource<<T extends Doc>(doc: T, client: Client) => Promise<Status>>
): Promise<Status> { ): Promise<Status> {
const impl = await getResource(action) const impl = await getResource(action)
return await impl(doc, client) return await impl({ ...doc, space: _space }, client)
} }
async function validate (doc: Applicant, _class: Ref<Class<Doc>>): Promise<void> { async function validate (doc: Applicant, space: Ref<Space>, _class: Ref<Class<Doc>>): Promise<void> {
const clazz = hierarchy.getClass(_class) const clazz = hierarchy.getClass(_class)
const validatorMixin = hierarchy.as(clazz, view.mixin.ObjectValidator) const validatorMixin = hierarchy.as(clazz, view.mixin.ObjectValidator)
if (validatorMixin?.validator != null) { if (validatorMixin?.validator != null) {
status = await invokeValidate(validatorMixin.validator) status = await invokeValidate(validatorMixin.validator)
} else if (clazz.extends != null) { } else if (clazz.extends != null) {
await validate(doc, clazz.extends) await validate(doc, space, clazz.extends)
} else { } else {
status = OK status = OK
} }
} }
$: validate(doc, doc._class) $: validate(doc, _space, doc._class)
let states: Array<{ id: number | string; color: number; label: string }> = [] let states: Array<{ id: number | string; color: number; label: string }> = []
let selectedState: State let selectedState: State | undefined
let rawStates: State[] = []
const statesQuery = createQuery() const statesQuery = createQuery()
$: if (doc.space) {
$: if (_space) {
statesQuery.query( statesQuery.query(
task.class.State, task.class.State,
{ space: doc.space }, { space: _space },
(res) => { (res) => {
states = res.map((s) => { rawStates = res
return { id: s._id, label: s.title, color: s.color }
})
selectedState = res.filter((s) => s._id === doc.state)[0] ?? res[0]
doc.state = selectedState._id
}, },
{ sort: { rank: SortingOrder.Ascending } } { sort: { rank: SortingOrder.Ascending } }
) )
} else {
states = []
} }
$: if (rawStates.findIndex((it) => it._id === selectedState?._id) === -1) {
selectedState = rawStates[0]
}
$: states = rawStates.map((s) => {
return { id: s._id, label: s.title, color: s.color }
})
const manager = createFocusManager() const manager = createFocusManager()
const existingApplicationsQuery = createQuery() const existingApplicationsQuery = createQuery()
@ -187,7 +197,7 @@
$: existingApplicationsQuery.query( $: existingApplicationsQuery.query(
recruit.class.Applicant, recruit.class.Applicant,
{ {
space: doc.space space: _space
}, },
(result) => { (result) => {
existingApplicants = result.map((it) => it.attachedTo) existingApplicants = result.map((it) => it.attachedTo)
@ -221,7 +231,7 @@
component: recruit.component.CreateVacancy, component: recruit.component.CreateVacancy,
label: recruit.string.CreateVacancy label: recruit.string.CreateVacancy
}} }}
bind:space={doc.space} bind:space={_space}
/> />
</svelte:fragment> </svelte:fragment>
<svelte:fragment slot="title"> <svelte:fragment slot="title">
@ -270,7 +280,7 @@
kind={'no-border'} kind={'no-border'}
size={'small'} size={'small'}
/> />
{#if states && doc.space} {#if states.length > 0}
<Button <Button
focusIndex={3} focusIndex={3}
width="min-content" width="min-content"
@ -283,9 +293,7 @@
eventToHTMLElement(ev), eventToHTMLElement(ev),
(result) => { (result) => {
if (result && result.id) { if (result && result.id) {
doc.state = result.id selectedState = { ...result, _id: result.id, title: result.label }
selectedState = result
selectedState.title = result.label
} }
manager.setFocusPos(3) manager.setFocusPos(3)
} }