mirror of
https://github.com/hcengineering/platform.git
synced 2026-09-09 19:27:44 +02:00
Process fixes (#10541)
* Process fixes Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com> * Some fixes Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com> --------- Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
@@ -20,6 +20,7 @@ import core, {
|
||||
Class,
|
||||
Data,
|
||||
Doc,
|
||||
fillDefaults,
|
||||
findProperty,
|
||||
generateId,
|
||||
getObjectValue,
|
||||
@@ -110,6 +111,7 @@ export async function CheckSubProcessMatch (
|
||||
|
||||
const subExecutions = await control.client.findAll(process.class.Execution, {
|
||||
parentId: execution._id,
|
||||
status: { $ne: ExecutionStatus.Cancelled },
|
||||
process: targetProcess
|
||||
})
|
||||
|
||||
@@ -315,19 +317,31 @@ export async function AddTag (
|
||||
const res: Tx[] = []
|
||||
const _process = control.client.getModel().findObject(execution.process)
|
||||
if (_process === undefined) throw processError(process.error.ObjectNotFound, { _id: execution.process })
|
||||
// todo fill default for tag and set parent tags
|
||||
const tx = control.client.txFactory.createTxMixin(execution.card, _process.masterTag, execution.space, tagId, props)
|
||||
res.push(tx)
|
||||
const card = control.cache.get(execution.card)
|
||||
const cardWithMixin =
|
||||
card !== undefined ? TxProcessor.updateMixin4Doc(control.client.getHierarchy().clone(card), tx) : undefined
|
||||
|
||||
const rollback = control.client.txFactory.createTxUpdateDoc(_process.masterTag, execution.space, execution.card, {
|
||||
$unset: { [tagId]: true }
|
||||
})
|
||||
const rollback: Tx[] = [
|
||||
control.client.txFactory.createTxUpdateDoc(_process.masterTag, execution.space, execution.card, {
|
||||
$unset: { [tagId]: true }
|
||||
})
|
||||
]
|
||||
|
||||
const processes = control.client
|
||||
.getModel()
|
||||
.findAllSync(process.class.Process, { masterTag: _process.masterTag, autoStart: true })
|
||||
for (const proc of processes) {
|
||||
const [txes, rbTxes] = await createExecution(proc._id, execution.card, execution, control)
|
||||
res.push(...txes)
|
||||
rollback.push(...rbTxes)
|
||||
}
|
||||
|
||||
return {
|
||||
txes: res,
|
||||
rollback: [rollback],
|
||||
rollback,
|
||||
context: [
|
||||
{
|
||||
_id: execution.card,
|
||||
@@ -704,11 +718,10 @@ export async function CreateCard (
|
||||
throw processError(process.error.RequiredParamsNotProvided, { params: key })
|
||||
}
|
||||
}
|
||||
const masterTag = _class as Ref<MasterTag>
|
||||
const _id = generateId<Card>()
|
||||
const newContent =
|
||||
content !== undefined && !isEmpty(content)
|
||||
? await getContent(control, content, _id, _class as Ref<Class<Card>>)
|
||||
: content
|
||||
content !== undefined && !isEmpty(content) ? await getContent(control, content, _id, masterTag) : content
|
||||
const data = {
|
||||
title,
|
||||
...attrs
|
||||
@@ -716,9 +729,26 @@ export async function CreateCard (
|
||||
if (newContent !== undefined) {
|
||||
data.content = content
|
||||
}
|
||||
const tx = control.client.txFactory.createTxCreateDoc(_class as Ref<MasterTag>, execution.space, data, _id)
|
||||
const filledData = fillDefaults(control.client.getHierarchy(), data, masterTag)
|
||||
|
||||
const tx = control.client.txFactory.createTxCreateDoc(masterTag, execution.space, filledData, _id)
|
||||
const res: Tx[] = [tx]
|
||||
const rollback: Tx[] = [control.client.txFactory.createTxRemoveDoc(_class as Ref<MasterTag>, execution.space, _id)]
|
||||
const rollback: Tx[] = [control.client.txFactory.createTxRemoveDoc(masterTag, execution.space, _id)]
|
||||
|
||||
const ancestors = control.client
|
||||
.getHierarchy()
|
||||
.getAncestors(masterTag)
|
||||
.filter((p) => control.client.getHierarchy().isDerived(p, cardPlugin.class.Card))
|
||||
|
||||
const processes = control.client.getModel().findAllSync(process.class.Process, {
|
||||
masterTag: { $in: ancestors },
|
||||
autoStart: true
|
||||
})
|
||||
for (const proc of processes) {
|
||||
const [txes, rbTxes] = await createExecution(proc._id, _id, execution, control)
|
||||
res.push(...txes)
|
||||
rollback.push(...rbTxes)
|
||||
}
|
||||
return {
|
||||
txes: res,
|
||||
rollback,
|
||||
@@ -734,3 +764,36 @@ export async function CreateCard (
|
||||
function isEmpty (value: any): boolean {
|
||||
return value === undefined || value === null || (typeof value === 'string' && value.trim() === '')
|
||||
}
|
||||
|
||||
async function createExecution (
|
||||
proc: Ref<Process>,
|
||||
_id: Ref<Card>,
|
||||
execution: Execution,
|
||||
control: ProcessControl
|
||||
): Promise<[Tx[], Tx[]]> {
|
||||
const res: Tx[] = []
|
||||
const rollback: Tx[] = []
|
||||
const initTransition = control.client.getModel().findAllSync(process.class.Transition, {
|
||||
process: proc,
|
||||
from: null
|
||||
})[0]
|
||||
if (initTransition === undefined) return [res, rollback]
|
||||
const execId = generateId()
|
||||
const tx = control.client.txFactory.createTxCreateDoc(
|
||||
process.class.Execution,
|
||||
execution.space,
|
||||
{
|
||||
process: proc,
|
||||
currentState: initTransition.to,
|
||||
card: _id,
|
||||
rollback: [],
|
||||
context: {},
|
||||
status: ExecutionStatus.Active
|
||||
},
|
||||
execId
|
||||
)
|
||||
|
||||
res.push(tx)
|
||||
rollback.push(control.client.txFactory.createTxRemoveDoc(process.class.Execution, execution.space, execId))
|
||||
return [res, rollback]
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ export function Random (value: Doc[]): Doc {
|
||||
}
|
||||
|
||||
export function All (value: Doc[]): Doc[] {
|
||||
return value
|
||||
return value ?? []
|
||||
}
|
||||
|
||||
export async function FirstMatchValue (
|
||||
|
||||
@@ -178,11 +178,14 @@ async function getRelationValue (
|
||||
const q = context.direction === 'A' ? { docB: execution.card } : { docA: execution.card }
|
||||
const relations = await control.client.findAll(core.class.Relation, { association: assoc._id, ...q })
|
||||
const name = context.direction === 'A' ? assoc.nameA : assoc.nameB
|
||||
if (relations.length === 0) throw processError(process.error.RelatedObjectNotFound, { attr: name })
|
||||
const shouldBeArray = assoc.type === 'N:N' || (assoc.type === '1:N' && context.direction === 'A')
|
||||
if (relations.length === 0) {
|
||||
if (shouldBeArray) return []
|
||||
throw processError(process.error.RelatedObjectNotFound, { attr: name })
|
||||
}
|
||||
const ids = relations.map((it) => {
|
||||
return context.direction === 'A' ? it.docA : it.docB
|
||||
})
|
||||
const shouldBeArray = assoc.type === 'N:N' || (assoc.type === '1:N' && context.direction === 'A')
|
||||
const target = await control.client.findAll(targetClass, { _id: { $in: ids } })
|
||||
if (target.length === 0) throw processError(process.error.RelatedObjectNotFound, { attr: context.name })
|
||||
const attr = context.key !== '' ? control.client.getHierarchy().findAttribute(targetClass, context.key) : undefined
|
||||
|
||||
Reference in New Issue
Block a user