-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(camunda8): implement createProcessInstanceWithResult
- Loading branch information
Showing
5 changed files
with
311 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import path from 'node:path' | ||
|
||
import { C8RestClient } from '../../../c8/lib/C8RestClient' | ||
import { LosslessDto } from '../../../lib' | ||
|
||
let bpmnProcessId: string | ||
let processDefinitionKey: string | ||
const restClient = new C8RestClient() | ||
|
||
beforeAll(async () => { | ||
const res = await restClient.deployResourcesFromFiles([ | ||
path.join('.', 'src', '__tests__', 'testdata', 'create-process-rest.bpmn'), | ||
]) | ||
;({ bpmnProcessId, processDefinitionKey } = res.processes[0]) | ||
}) | ||
|
||
class myVariableDto extends LosslessDto { | ||
someNumberField?: number | ||
} | ||
|
||
test('Can create a process from bpmn id', (done) => { | ||
restClient | ||
.createProcessInstance({ | ||
bpmnProcessId, | ||
variables: { | ||
someNumberField: 8, | ||
}, | ||
}) | ||
.then((res) => { | ||
expect(res.processKey).toEqual(processDefinitionKey) | ||
done() | ||
}) | ||
}) | ||
|
||
test('Can create a process from process definition key', (done) => { | ||
restClient | ||
.createProcessInstance({ | ||
processDefinitionKey, | ||
variables: { | ||
someNumberField: 8, | ||
}, | ||
}) | ||
.then((res) => { | ||
expect(res.processKey).toEqual(processDefinitionKey) | ||
done() | ||
}) | ||
}) | ||
|
||
test('Can create a process with a lossless Dto', (done) => { | ||
restClient | ||
.createProcessInstance({ | ||
processDefinitionKey, | ||
variables: new myVariableDto({ someNumberField: 8 }), | ||
}) | ||
.then((res) => { | ||
expect(res.processKey).toEqual(processDefinitionKey) | ||
done() | ||
}) | ||
}) | ||
|
||
test('Can create a process and get the result', (done) => { | ||
const variables = new myVariableDto({ someNumberField: 8 }) | ||
restClient | ||
.createProcessInstanceWithResult({ | ||
processDefinitionKey, | ||
variables, | ||
outputVariablesDto: myVariableDto, | ||
}) | ||
.then((res) => { | ||
console.log(res) | ||
expect(res.processKey).toEqual(processDefinitionKey) | ||
expect(res.variables.someNumberField).toBe(8) | ||
done() | ||
}) | ||
}) | ||
|
||
test('Can create a process and get the result', (done) => { | ||
restClient | ||
.createProcessInstanceWithResult({ | ||
processDefinitionKey, | ||
variables: new myVariableDto({ someNumberField: 9 }), | ||
}) | ||
.then((res) => { | ||
expect(res.processKey).toEqual(processDefinitionKey) | ||
// Without an outputVariablesDto, the response variables will be of type unknown | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
expect((res.variables as any).someNumberField).toBe(9) | ||
done() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_1ijuty4" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="5.27.0" modeler:executionPlatform="Camunda Cloud" modeler:executionPlatformVersion="8.5.0"> | ||
<bpmn:process id="create-process-rest" name="Create Process Test" isExecutable="true"> | ||
<bpmn:startEvent id="StartEvent_1" name="Start"> | ||
<bpmn:outgoing>Flow_15yxzfg</bpmn:outgoing> | ||
</bpmn:startEvent> | ||
<bpmn:endEvent id="Event_1j84ets" name="End"> | ||
<bpmn:incoming>Flow_15yxzfg</bpmn:incoming> | ||
</bpmn:endEvent> | ||
<bpmn:sequenceFlow id="Flow_15yxzfg" sourceRef="StartEvent_1" targetRef="Event_1j84ets" /> | ||
</bpmn:process> | ||
<bpmndi:BPMNDiagram id="BPMNDiagram_1"> | ||
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="create-process-rest"> | ||
<bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1"> | ||
<dc:Bounds x="179" y="79" width="36" height="36" /> | ||
<bpmndi:BPMNLabel> | ||
<dc:Bounds x="185" y="122" width="24" height="14" /> | ||
</bpmndi:BPMNLabel> | ||
</bpmndi:BPMNShape> | ||
<bpmndi:BPMNShape id="Event_1j84ets_di" bpmnElement="Event_1j84ets"> | ||
<dc:Bounds x="292" y="79" width="36" height="36" /> | ||
<bpmndi:BPMNLabel> | ||
<dc:Bounds x="300" y="122" width="20" height="14" /> | ||
</bpmndi:BPMNLabel> | ||
</bpmndi:BPMNShape> | ||
<bpmndi:BPMNEdge id="Flow_15yxzfg_di" bpmnElement="Flow_15yxzfg"> | ||
<di:waypoint x="215" y="97" /> | ||
<di:waypoint x="292" y="97" /> | ||
</bpmndi:BPMNEdge> | ||
</bpmndi:BPMNPlane> | ||
</bpmndi:BPMNDiagram> | ||
</bpmn:definitions> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { LosslessDto } from '../../lib' | ||
|
||
import { CreateProcessInstanceResponse } from './C8Dto' | ||
|
||
const factory = | ||
createMemoizedSpecializedCreateProcessInstanceResponseClassFactory() | ||
|
||
// Creates a specialized RestApiJob class that is cached based on output variables | ||
export const createSpecializedCreateProcessInstanceResponseClass = < | ||
Variables extends LosslessDto, | ||
>( | ||
outputVariableDto: new (obj: any) => Variables | ||
) => { | ||
return factory(outputVariableDto) | ||
} | ||
|
||
function createMemoizedSpecializedCreateProcessInstanceResponseClassFactory() { | ||
const cache = new Map<string, any>() | ||
|
||
return function <Variables extends LosslessDto>( | ||
outputVariableDto: new (obj: any) => Variables | ||
): new (obj: any) => CreateProcessInstanceResponse<Variables> { | ||
// Create a unique cache key based on the class and inputs | ||
const cacheKey = JSON.stringify({ | ||
outputVariableDto, | ||
}) | ||
|
||
// Check for cached result | ||
if (cache.has(cacheKey)) { | ||
return cache.get(cacheKey) | ||
} | ||
|
||
// Create a new class that extends the original class | ||
class CustomCreateProcessInstanceResponseClass< | ||
Variables extends LosslessDto, | ||
> extends CreateProcessInstanceResponse<Variables> { | ||
variables!: Variables | ||
} | ||
|
||
// Use Reflect to define the metadata on the new class's prototype | ||
Reflect.defineMetadata( | ||
'child:class', | ||
outputVariableDto, | ||
CustomCreateProcessInstanceResponseClass.prototype, | ||
'variables' | ||
) | ||
|
||
// Store the new class in cache | ||
cache.set(cacheKey, CustomCreateProcessInstanceResponseClass) | ||
|
||
// Return the new class | ||
return CustomCreateProcessInstanceResponseClass | ||
} | ||
} |