mirror of
https://github.com/hcengineering/platform.git
synced 2026-09-11 04:07:43 +02:00
Rollback and custom events preparation (#10298)
Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
@@ -145,6 +145,16 @@ export function MatchCardCheck (
|
||||
return res.length > 0
|
||||
}
|
||||
|
||||
export function EventCheck (
|
||||
control: ProcessControl,
|
||||
execution: Execution,
|
||||
params: Record<string, any>,
|
||||
context: Record<string, any>
|
||||
): boolean {
|
||||
if (params.eventType === undefined) return false
|
||||
return context.eventType === params.eventType
|
||||
}
|
||||
|
||||
export function FieldChangedCheck (
|
||||
control: ProcessControl,
|
||||
execution: Execution,
|
||||
@@ -299,23 +309,23 @@ export async function AddTag (
|
||||
): Promise<ExecuteResult> {
|
||||
const { _id, props } = params
|
||||
if (_id === undefined) throw processError(process.error.RequiredParamsNotProvided, { params: '_id' })
|
||||
const tagId = _id as Ref<Tag>
|
||||
const res: Tx[] = []
|
||||
const _process = control.client.getModel().findObject(execution.process)
|
||||
if (_process === undefined) throw processError(process.error.ObjectNotFound, { _id: execution.process })
|
||||
const tx = control.client.txFactory.createTxMixin(
|
||||
execution.card,
|
||||
_process.masterTag,
|
||||
core.space.Workspace,
|
||||
_id as Ref<Tag>,
|
||||
props
|
||||
)
|
||||
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 }
|
||||
})
|
||||
|
||||
return {
|
||||
txes: res,
|
||||
rollback: undefined,
|
||||
rollback: [rollback],
|
||||
context: [
|
||||
{
|
||||
_id: execution.card,
|
||||
@@ -337,6 +347,7 @@ export async function RunSubProcess (
|
||||
if (target === undefined) throw processError(process.error.ObjectNotFound, { _id: processId })
|
||||
const res: Tx[] = []
|
||||
const resultContext: SuccessExecutionContext[] = []
|
||||
const rollback: Tx[] = []
|
||||
for (const _card of Array.isArray(card) ? card : [card]) {
|
||||
if (target.parallelExecutionForbidden === true) {
|
||||
const currentExecution = await control.client.findAll(process.class.Execution, {
|
||||
@@ -370,13 +381,15 @@ export async function RunSubProcess (
|
||||
_id
|
||||
)
|
||||
|
||||
rollback.push(control.client.txFactory.createTxRemoveDoc(process.class.Execution, core.space.Workspace, _id))
|
||||
|
||||
res.push(tx)
|
||||
resultContext.push({
|
||||
_id,
|
||||
value: TxProcessor.createDoc2Doc(tx, true)
|
||||
})
|
||||
}
|
||||
return { txes: res, rollback: undefined, context: resultContext }
|
||||
return { txes: res, rollback, context: resultContext }
|
||||
}
|
||||
|
||||
export async function CreateToDo (
|
||||
|
||||
@@ -38,7 +38,8 @@ import process, {
|
||||
State,
|
||||
Step,
|
||||
Transition,
|
||||
isUpdateTx
|
||||
isUpdateTx,
|
||||
ProcessCustomEvent
|
||||
} from '@hcengineering/process'
|
||||
import { QueueTopic, TriggerControl } from '@hcengineering/server-core'
|
||||
import { ProcessMessage } from '@hcengineering/server-process'
|
||||
@@ -95,7 +96,8 @@ import {
|
||||
CheckSubProcessesDone,
|
||||
CheckSubProcessMatch,
|
||||
CheckTime,
|
||||
FieldChangedCheck
|
||||
FieldChangedCheck,
|
||||
EventCheck
|
||||
} from './functions'
|
||||
import { ToDoCancellRollback, ToDoCloseRollback } from './rollback'
|
||||
|
||||
@@ -141,6 +143,31 @@ export async function OnProcessToDoClose (txes: Tx[], control: TriggerControl):
|
||||
return res
|
||||
}
|
||||
|
||||
export async function OnCustomEvent (txes: Tx[], control: TriggerControl): Promise<Tx[]> {
|
||||
for (const tx of txes) {
|
||||
if (tx._class !== core.class.TxCreateDoc) continue
|
||||
const createTx = tx as TxCreateDoc<ProcessCustomEvent>
|
||||
if (!control.hierarchy.isDerived(createTx.objectClass, process.class.ProcessCustomEvent)) continue
|
||||
const customEvent = TxProcessor.createDoc2Doc(createTx)
|
||||
const card = await control.findAll(control.ctx, cardPlugin.class.Card, { _id: customEvent.card }, { limit: 1 })
|
||||
if (card.length === 0) continue
|
||||
await putEventToQueue(
|
||||
{
|
||||
event: process.trigger.OnEvent,
|
||||
execution: customEvent.execution,
|
||||
createdOn: tx.modifiedOn,
|
||||
card: customEvent.card,
|
||||
context: {
|
||||
eventType: customEvent.eventType,
|
||||
card
|
||||
}
|
||||
},
|
||||
control
|
||||
)
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
export async function OnExecutionCreate (txes: Tx[], control: TriggerControl): Promise<Tx[]> {
|
||||
for (const tx of txes) {
|
||||
if (tx._class !== core.class.TxCreateDoc) continue
|
||||
@@ -249,6 +276,23 @@ export async function OnStateRemove (txes: Tx[], control: TriggerControl): Promi
|
||||
return res
|
||||
}
|
||||
|
||||
export async function OnExecutionRemove (txes: Tx[], control: TriggerControl): Promise<Tx[]> {
|
||||
const res: Tx[] = []
|
||||
for (const tx of txes) {
|
||||
if (tx._class !== core.class.TxRemoveDoc) continue
|
||||
const cudTx = tx as TxRemoveDoc<Execution>
|
||||
if (!control.hierarchy.isDerived(cudTx.objectClass, process.class.Execution)) continue
|
||||
const todos = await control.findAll(control.ctx, process.class.ProcessToDo, {
|
||||
execution: cudTx.objectId,
|
||||
doneOn: null
|
||||
})
|
||||
for (const todo of todos) {
|
||||
res.push(control.txFactory.createTxRemoveDoc(todo._class, todo.space, todo._id))
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
export async function OnTransition (txes: Tx[], control: TriggerControl): Promise<Tx[]> {
|
||||
const res: Tx[] = []
|
||||
for (const tx of txes) {
|
||||
@@ -400,7 +444,8 @@ export default async () => ({
|
||||
MatchCardCheck,
|
||||
CheckSubProcessesDone,
|
||||
CheckSubProcessMatch,
|
||||
CheckTime
|
||||
CheckTime,
|
||||
EventCheck
|
||||
},
|
||||
transform: {
|
||||
CurrentDate,
|
||||
@@ -454,6 +499,8 @@ export default async () => ({
|
||||
OnExecutionCreate,
|
||||
OnProcessToDoClose,
|
||||
OnProcessToDoRemove,
|
||||
OnExecutionContinue
|
||||
OnExecutionContinue,
|
||||
OnCustomEvent,
|
||||
OnExecutionRemove
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user