Skip to content

Commit 8ba1e6c

Browse files
authored
Process: Use previous step context in the same transition (#9877)
Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
1 parent d0a42d6 commit 8ba1e6c

4 files changed

Lines changed: 112 additions & 60 deletions

File tree

server-plugins/process-resources/src/functions.ts

Lines changed: 99 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import core, {
2525
Ref,
2626
Relation,
2727
splitMixinUpdate,
28-
Tx
28+
Tx,
29+
TxProcessor
2930
} from '@hcengineering/core'
3031
import process, {
3132
Execution,
@@ -36,7 +37,7 @@ import process, {
3637
processError,
3738
ProcessToDo
3839
} from '@hcengineering/process'
39-
import { ExecuteResult, ProcessControl } from '@hcengineering/server-process'
40+
import { ExecuteResult, ProcessControl, SuccessExecutionContext } from '@hcengineering/server-process'
4041
import time, { ToDoPriority } from '@hcengineering/time'
4142

4243
export async function CheckToDoDone (
@@ -122,9 +123,19 @@ export async function AddRelation (
122123
docA,
123124
docB
124125
}
125-
const res: Tx[] = [control.client.txFactory.createTxCreateDoc(core.class.Relation, core.space.Workspace, data, _id)]
126+
const resTx = control.client.txFactory.createTxCreateDoc(core.class.Relation, core.space.Workspace, data, _id)
127+
const res: Tx[] = [resTx]
126128
const rollback: Tx[] = [control.client.txFactory.createTxRemoveDoc(core.class.Relation, core.space.Workspace, _id)]
127-
return { txes: res, rollback, context: _id }
129+
return {
130+
txes: res,
131+
rollback,
132+
context: [
133+
{
134+
_id,
135+
value: TxProcessor.createDoc2Doc(resTx, true)
136+
}
137+
]
138+
}
128139
}
129140

130141
export async function UpdateCard (
@@ -189,19 +200,29 @@ export async function AddTag (
189200
const { _id, props } = params
190201
if (_id === undefined) throw processError(process.error.RequiredParamsNotProvided, { params: '_id' })
191202
const res: Tx[] = []
192-
const context: Ref<Card>[] = [execution.card]
193203
const _process = control.client.getModel().findObject(execution.process)
194204
if (_process === undefined) throw processError(process.error.ObjectNotFound, { _id: execution.process })
195-
res.push(
196-
control.client.txFactory.createTxMixin(
197-
execution.card,
198-
_process.masterTag,
199-
core.space.Workspace,
200-
_id as Ref<Tag>,
201-
props
202-
)
205+
const tx = control.client.txFactory.createTxMixin(
206+
execution.card,
207+
_process.masterTag,
208+
core.space.Workspace,
209+
_id as Ref<Tag>,
210+
props
203211
)
204-
return { txes: res, rollback: undefined, context }
212+
res.push(tx)
213+
const card = control.cache.get(execution.card)
214+
const cardWithMixin =
215+
card !== undefined ? TxProcessor.updateMixin4Doc(control.client.getHierarchy().clone(card), tx) : undefined
216+
return {
217+
txes: res,
218+
rollback: undefined,
219+
context: [
220+
{
221+
_id: execution.card,
222+
value: cardWithMixin
223+
}
224+
]
225+
}
205226
}
206227

207228
export async function RunSubProcess (
@@ -215,7 +236,7 @@ export async function RunSubProcess (
215236
const target = control.client.getModel().findObject(processId)
216237
if (target === undefined) throw processError(process.error.ObjectNotFound, { _id: processId })
217238
const res: Tx[] = []
218-
const resultContext: Ref<Execution>[] = []
239+
const resultContext: SuccessExecutionContext[] = []
219240
for (const _card of Array.isArray(card) ? card : [card]) {
220241
if (target.parallelExecutionForbidden === true) {
221242
const currentExecution = await control.client.findAll(process.class.Execution, {
@@ -233,24 +254,27 @@ export async function RunSubProcess (
233254
.findAllSync(process.class.Transition, { process: target._id, from: null })[0]
234255
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
235256
const context = params.context ?? ({} as ExecutionContext)
236-
const id = generateId<Execution>()
237-
res.push(
238-
control.client.txFactory.createTxCreateDoc(
239-
process.class.Execution,
240-
core.space.Workspace,
241-
{
242-
process: processId,
243-
currentState: initTransition.to,
244-
card: _card,
245-
context,
246-
status: ExecutionStatus.Active,
247-
rollback: [],
248-
parentId: execution._id
249-
},
250-
id
251-
)
257+
const _id = generateId<Execution>()
258+
const tx = control.client.txFactory.createTxCreateDoc(
259+
process.class.Execution,
260+
core.space.Workspace,
261+
{
262+
process: processId,
263+
currentState: initTransition.to,
264+
card: _card,
265+
context,
266+
status: ExecutionStatus.Active,
267+
rollback: [],
268+
parentId: execution._id
269+
},
270+
_id
252271
)
253-
resultContext.push(id)
272+
273+
res.push(tx)
274+
resultContext.push({
275+
_id,
276+
value: TxProcessor.createDoc2Doc(tx, true)
277+
})
254278
}
255279
return { txes: res, rollback: undefined, context: resultContext }
256280
}
@@ -270,30 +294,38 @@ export async function CreateToDo (
270294
const res: Tx[] = []
271295
const rollback: Tx[] = []
272296
const id = generateId<ProcessToDo>()
273-
res.push(
274-
control.client.txFactory.createTxCreateDoc(
275-
process.class.ProcessToDo,
276-
time.space.ToDos,
277-
{
278-
attachedTo: execution.card,
279-
attachedToClass: cardPlugin.class.Card,
280-
collection: 'todos',
281-
workslots: 0,
282-
execution: execution._id,
283-
title: params.title,
284-
user: params.user,
285-
description: params.description ?? '',
286-
dueDate: params.dueDate,
287-
priority: params.priority ?? ToDoPriority.NoPriority,
288-
visibility: 'public',
289-
doneOn: null,
290-
rank: '',
291-
withRollback: params.withRollback ?? false
292-
},
293-
id
294-
)
297+
const tx = control.client.txFactory.createTxCreateDoc(
298+
process.class.ProcessToDo,
299+
time.space.ToDos,
300+
{
301+
attachedTo: execution.card,
302+
attachedToClass: cardPlugin.class.Card,
303+
collection: 'todos',
304+
workslots: 0,
305+
execution: execution._id,
306+
title: params.title,
307+
user: params.user,
308+
description: params.description ?? '',
309+
dueDate: params.dueDate,
310+
priority: params.priority ?? ToDoPriority.NoPriority,
311+
visibility: 'public',
312+
doneOn: null,
313+
rank: '',
314+
withRollback: params.withRollback ?? false
315+
},
316+
id
295317
)
296-
return { txes: res, rollback, context: id }
318+
res.push(tx)
319+
return {
320+
txes: res,
321+
rollback,
322+
context: [
323+
{
324+
_id: id,
325+
value: TxProcessor.createDoc2Doc(tx, true)
326+
}
327+
]
328+
}
297329
}
298330

299331
export async function CreateCard (
@@ -313,9 +345,19 @@ export async function CreateCard (
313345
title,
314346
...attrs
315347
} as any
316-
const res: Tx[] = [control.client.txFactory.createTxCreateDoc(_class as Ref<MasterTag>, execution.space, data, _id)]
348+
const tx = control.client.txFactory.createTxCreateDoc(_class as Ref<MasterTag>, execution.space, data, _id)
349+
const res: Tx[] = [tx]
317350
const rollback: Tx[] = [control.client.txFactory.createTxRemoveDoc(_class as Ref<MasterTag>, execution.space, _id)]
318-
return { txes: res, rollback, context: _id }
351+
return {
352+
txes: res,
353+
rollback,
354+
context: [
355+
{
356+
_id,
357+
value: TxProcessor.createDoc2Doc(tx, true)
358+
}
359+
]
360+
}
319361
}
320362

321363
function isEmpty (value: any): boolean {

server-plugins/process-resources/src/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ async function getExecutionContextValue (
275275
if (userContext !== undefined) {
276276
if (context.key === '' || context.key === '_id') return userContext
277277
if (processContext !== undefined) {
278-
const contextVal = await control.client.findOne(processContext?._class, { _id: userContext })
278+
const contextVal =
279+
control.cache.get(userContext) ?? (await control.client.findOne(processContext?._class, { _id: userContext }))
279280
if (contextVal !== undefined) {
280281
const val = getValue(control, execution, context.key, contextVal)
281282
return val

server-plugins/process/src/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ export type ExecuteResult = SuccessExecutionResult | ExecutionError
1313
export interface SuccessExecutionResult {
1414
txes: Tx[]
1515
rollback: Tx[] | undefined
16-
context: any | null
16+
context: SuccessExecutionContext[] | null
17+
}
18+
19+
export interface SuccessExecutionContext {
20+
_id: string
21+
value: any
1722
}
1823

1924
export type TransformFunc = (

services/process/src/main.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,11 @@ async function executeAction<T extends Doc> (
449449
const f = await getResource(impl.func)
450450
const res = await f(params, execution, control)
451451
if (!isError(res) && action.context?._id != null && res.context != null) {
452-
execution.context[action.context._id] = res.context
452+
execution.context[action.context._id] =
453+
res.context.length === 1 ? res.context[0]._id : res.context.map((it) => it._id)
454+
for (const ctx of res.context) {
455+
control.cache.set(ctx._id, ctx.value)
456+
}
453457
}
454458
return res
455459
} catch (err) {

0 commit comments

Comments
 (0)