diff --git a/plugins/process-resources/src/middleware.ts b/plugins/process-resources/src/middleware.ts index f41e793c6b..5a22fe55d9 100644 --- a/plugins/process-resources/src/middleware.ts +++ b/plugins/process-resources/src/middleware.ts @@ -120,13 +120,14 @@ export class ProcessMiddleware extends BasePresentationMiddleware implements Pre }, { sort: { rank: SortingOrder.Ascending } } ) + if (transitions.length === 0) continue const inputContext = { ...execution.context, card: updated, operations: isUpdateTx(updateTx) ? updateTx.operations : updateTx.attributes } const transition = await pickTransition(this.client, execution, transitions, inputContext) - if (transition === undefined) return + if (transition === undefined) continue const result = await getNextStateUserInput(execution, transition, execution.context, inputContext) if (result?.changed === true) { preTx.push( diff --git a/plugins/process-resources/src/utils.ts b/plugins/process-resources/src/utils.ts index a77243257e..d29eb8b14a 100644 --- a/plugins/process-resources/src/utils.ts +++ b/plugins/process-resources/src/utils.ts @@ -463,11 +463,12 @@ export async function requestUserInput ( target: Transition, execution: Execution, userContext: ExecutionContext, - inputContext: Record = {} + inputContext: Record = {}, + skipExisting: boolean = false ): Promise<{ context: ExecutionContext, state: Ref, changed: boolean }> { const client = getClient() let changed = false - const tr = await getTransitionUserInput(processId, space, target, userContext) + const tr = await getTransitionUserInput(processId, space, target, userContext, skipExisting) if (tr !== undefined) { userContext = { ...userContext, ...tr } changed = true @@ -485,7 +486,9 @@ export async function requestUserInput ( process: processId, from: target.to }) - .filter((t) => client.getModel().findObject(t.trigger)?.auto === true) + .filter( + (t) => client.getModel().findObject(t.trigger)?.auto === true && t.trigger !== process.trigger.OnSubProcessesDone + ) if (nextAutoTransitions.length > 0) { const newExecution = { @@ -518,7 +521,8 @@ export async function getTransitionUserInput ( processId: Ref, space: Ref, transition: Transition, - userContext: ExecutionContext + userContext: ExecutionContext, + skipExisting: boolean = false ): Promise { let changed = false for (const action of transition.actions) { @@ -526,9 +530,20 @@ export async function getTransitionUserInput ( const inputs: SelectedUserRequest[] = [] for (const key in action.params) { const value = (action.params as any)[key] - const context = parseContext(value) - if (context !== undefined && context.type === 'userRequest') { - inputs.push(context) + if (typeof value === 'string') { + const context = parseContext(value) + if (context !== undefined && context.type === 'userRequest') { + if (skipExisting && userContext[context.id] !== undefined) continue + inputs.push(context) + } + } else if (typeof value === 'object' && !Array.isArray(value)) { + for (const element of Object.values(value)) { + const context = parseContext(element) + if (context !== undefined && context.type === 'userRequest') { + if (skipExisting && userContext[context.id] !== undefined) continue + inputs.push(context) + } + } } } if (inputs.length > 0) { @@ -576,7 +591,14 @@ export async function getSubProcessesUserInput ( if (action.methodId !== process.method.RunSubProcess) continue const processId = action.params._id as Ref if (processId === undefined) continue - const context: ExecutionContext = action.params.context ?? getEmptyContext() + const context: ExecutionContext = JSON.parse(JSON.stringify(action.params.context ?? getEmptyContext())) + for (const [k, v] of Object.entries(context)) { + const c = parseContext(v) + if (c !== undefined && c.type === 'userRequest') { + if (userContext[c.id] !== undefined) continue + ;(context as any)[k] = userContext[c.id] + } + } const initTransition = getClient().getModel().findAllSync(process.class.Transition, { process: processId, from: null @@ -597,9 +619,10 @@ export async function getSubProcessesUserInput ( modifiedOn: 0, modifiedBy: client.user } - const res = await newExecutionUserInput(processId, space, mockExecution) + const res = await newExecutionUserInput(processId, space, mockExecution, true) if (action.context == null || res === undefined) continue userContext[action.context._id] = res + userContext = { ...userContext, ...res.context } changed = true } return changed ? userContext : undefined @@ -608,7 +631,8 @@ export async function getSubProcessesUserInput ( export async function newExecutionUserInput ( _id: Ref, space: Ref, - execution: Execution + execution: Execution, + skipExisting: boolean = false ): Promise<{ context: ExecutionContext, state: Ref, changed: boolean } | undefined> { const client = getClient() const initTransition = client.getModel().findAllSync(process.class.Transition, { @@ -616,7 +640,7 @@ export async function newExecutionUserInput ( from: null })[0] if (initTransition === undefined) return undefined - return await requestUserInput(_id, space, initTransition, execution, execution.context) + return await requestUserInput(_id, space, initTransition, execution, execution.context, {}, skipExisting) } export async function getNextStateUserInput (