@contractspec/example.workflow-system 1.57.0 → 1.58.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (209) hide show
  1. package/dist/approval/approval.enum.d.ts +2 -7
  2. package/dist/approval/approval.enum.d.ts.map +1 -1
  3. package/dist/approval/approval.enum.js +20 -26
  4. package/dist/approval/approval.event.d.ts +108 -114
  5. package/dist/approval/approval.event.d.ts.map +1 -1
  6. package/dist/approval/approval.event.js +103 -210
  7. package/dist/approval/approval.handler.d.ts +17 -18
  8. package/dist/approval/approval.handler.d.ts.map +1 -1
  9. package/dist/approval/approval.operations.d.ts +429 -435
  10. package/dist/approval/approval.operations.d.ts.map +1 -1
  11. package/dist/approval/approval.operations.js +364 -339
  12. package/dist/approval/approval.schema.d.ts +86 -91
  13. package/dist/approval/approval.schema.d.ts.map +1 -1
  14. package/dist/approval/approval.schema.js +71 -107
  15. package/dist/approval/index.d.ts +8 -5
  16. package/dist/approval/index.d.ts.map +1 -0
  17. package/dist/approval/index.js +484 -5
  18. package/dist/browser/approval/approval.enum.js +22 -0
  19. package/dist/browser/approval/approval.event.js +112 -0
  20. package/dist/browser/approval/approval.operations.js +369 -0
  21. package/dist/browser/approval/approval.schema.js +73 -0
  22. package/dist/browser/approval/index.js +484 -0
  23. package/dist/browser/docs/index.js +103 -0
  24. package/dist/browser/docs/workflow-system.docblock.js +103 -0
  25. package/dist/browser/entities/approval.js +119 -0
  26. package/dist/browser/entities/index.js +508 -0
  27. package/dist/browser/entities/instance.js +161 -0
  28. package/dist/browser/entities/step.js +124 -0
  29. package/dist/browser/entities/workflow.js +82 -0
  30. package/dist/browser/example.js +42 -0
  31. package/dist/browser/handlers/index.js +253 -0
  32. package/dist/browser/handlers/workflow.handlers.js +253 -0
  33. package/dist/browser/index.js +3120 -0
  34. package/dist/browser/instance/index.js +677 -0
  35. package/dist/browser/instance/instance.enum.js +15 -0
  36. package/dist/browser/instance/instance.event.js +164 -0
  37. package/dist/browser/instance/instance.handler.js +356 -0
  38. package/dist/browser/instance/instance.operations.js +9 -0
  39. package/dist/browser/instance/instance.schema.js +101 -0
  40. package/dist/browser/presentations/index.js +109 -0
  41. package/dist/browser/seeders/index.js +3 -0
  42. package/dist/browser/shared/index.js +3 -0
  43. package/dist/browser/shared/mock-data.js +11 -0
  44. package/dist/browser/shared/types.js +0 -0
  45. package/dist/browser/state-machine/index.js +6 -0
  46. package/dist/browser/tests/operations.test-spec.js +6 -0
  47. package/dist/browser/ui/WorkflowDashboard.js +3 -0
  48. package/dist/browser/ui/hooks/index.js +50 -0
  49. package/dist/browser/ui/hooks/useWorkflowList.js +50 -0
  50. package/dist/browser/ui/index.js +54 -0
  51. package/dist/browser/ui/renderers/index.js +227 -0
  52. package/dist/browser/ui/renderers/workflow.markdown.js +227 -0
  53. package/dist/browser/workflow/index.js +21 -0
  54. package/dist/browser/workflow/workflow.enum.js +36 -0
  55. package/dist/browser/workflow/workflow.event.js +6 -0
  56. package/dist/browser/workflow/workflow.handler.js +5 -0
  57. package/dist/browser/workflow/workflow.operations.js +8 -0
  58. package/dist/browser/workflow/workflow.schema.js +151 -0
  59. package/dist/browser/workflow-system.capability.js +5 -0
  60. package/dist/browser/workflow-system.feature.js +3 -0
  61. package/dist/docs/index.d.ts +2 -1
  62. package/dist/docs/index.d.ts.map +1 -0
  63. package/dist/docs/index.js +104 -1
  64. package/dist/docs/workflow-system.docblock.d.ts +2 -1
  65. package/dist/docs/workflow-system.docblock.d.ts.map +1 -0
  66. package/dist/docs/workflow-system.docblock.js +45 -56
  67. package/dist/entities/approval.d.ts +35 -40
  68. package/dist/entities/approval.d.ts.map +1 -1
  69. package/dist/entities/approval.js +116 -124
  70. package/dist/entities/index.d.ts +132 -137
  71. package/dist/entities/index.d.ts.map +1 -1
  72. package/dist/entities/index.js +506 -29
  73. package/dist/entities/instance.d.ts +46 -51
  74. package/dist/entities/instance.d.ts.map +1 -1
  75. package/dist/entities/instance.js +158 -164
  76. package/dist/entities/step.d.ts +31 -36
  77. package/dist/entities/step.d.ts.map +1 -1
  78. package/dist/entities/step.js +122 -132
  79. package/dist/entities/workflow.d.ts +22 -27
  80. package/dist/entities/workflow.d.ts.map +1 -1
  81. package/dist/entities/workflow.js +80 -99
  82. package/dist/example.d.ts +2 -6
  83. package/dist/example.d.ts.map +1 -1
  84. package/dist/example.js +41 -55
  85. package/dist/handlers/index.d.ts +2 -2
  86. package/dist/handlers/index.d.ts.map +1 -0
  87. package/dist/handlers/index.js +254 -3
  88. package/dist/handlers/workflow.handlers.d.ts +107 -106
  89. package/dist/handlers/workflow.handlers.d.ts.map +1 -1
  90. package/dist/handlers/workflow.handlers.js +237 -246
  91. package/dist/index.d.ts +15 -26
  92. package/dist/index.d.ts.map +1 -0
  93. package/dist/index.js +3121 -26
  94. package/dist/instance/index.d.ts +8 -5
  95. package/dist/instance/index.d.ts.map +1 -0
  96. package/dist/instance/index.js +677 -5
  97. package/dist/instance/instance.enum.d.ts +1 -6
  98. package/dist/instance/instance.enum.d.ts.map +1 -1
  99. package/dist/instance/instance.enum.js +14 -18
  100. package/dist/instance/instance.event.d.ts +313 -319
  101. package/dist/instance/instance.event.d.ts.map +1 -1
  102. package/dist/instance/instance.event.js +151 -279
  103. package/dist/instance/instance.handler.d.ts +21 -22
  104. package/dist/instance/instance.handler.d.ts.map +1 -1
  105. package/dist/instance/instance.handler.js +352 -89
  106. package/dist/instance/instance.operations.d.ts +819 -825
  107. package/dist/instance/instance.operations.d.ts.map +1 -1
  108. package/dist/instance/instance.operations.js +10 -464
  109. package/dist/instance/instance.schema.d.ts +196 -201
  110. package/dist/instance/instance.schema.d.ts.map +1 -1
  111. package/dist/instance/instance.schema.js +97 -167
  112. package/dist/presentations/index.d.ts +23 -28
  113. package/dist/presentations/index.d.ts.map +1 -1
  114. package/dist/presentations/index.js +104 -334
  115. package/dist/seeders/index.d.ts +4 -8
  116. package/dist/seeders/index.d.ts.map +1 -1
  117. package/dist/seeders/index.js +4 -19
  118. package/dist/shared/index.d.ts +6 -3
  119. package/dist/shared/index.d.ts.map +1 -0
  120. package/dist/shared/index.js +4 -3
  121. package/dist/shared/mock-data.d.ts +16 -16
  122. package/dist/shared/mock-data.d.ts.map +1 -1
  123. package/dist/shared/mock-data.js +11 -11
  124. package/dist/shared/types.d.ts +69 -72
  125. package/dist/shared/types.d.ts.map +1 -1
  126. package/dist/shared/types.js +1 -0
  127. package/dist/state-machine/index.d.ts +92 -95
  128. package/dist/state-machine/index.d.ts.map +1 -1
  129. package/dist/state-machine/index.js +6 -157
  130. package/dist/tests/operations.test-spec.d.ts +4 -9
  131. package/dist/tests/operations.test-spec.d.ts.map +1 -1
  132. package/dist/tests/operations.test-spec.js +7 -123
  133. package/dist/ui/WorkflowDashboard.d.ts +1 -6
  134. package/dist/ui/WorkflowDashboard.d.ts.map +1 -1
  135. package/dist/ui/WorkflowDashboard.js +3 -222
  136. package/dist/ui/hooks/index.d.ts +2 -2
  137. package/dist/ui/hooks/index.d.ts.map +1 -0
  138. package/dist/ui/hooks/index.js +51 -5
  139. package/dist/ui/hooks/useWorkflowList.d.ts +15 -19
  140. package/dist/ui/hooks/useWorkflowList.d.ts.map +1 -1
  141. package/dist/ui/hooks/useWorkflowList.js +47 -51
  142. package/dist/ui/index.d.ts +7 -6
  143. package/dist/ui/index.d.ts.map +1 -0
  144. package/dist/ui/index.js +55 -6
  145. package/dist/ui/renderers/index.d.ts +2 -2
  146. package/dist/ui/renderers/index.d.ts.map +1 -0
  147. package/dist/ui/renderers/index.js +227 -2
  148. package/dist/ui/renderers/workflow.markdown.d.ts +13 -14
  149. package/dist/ui/renderers/workflow.markdown.d.ts.map +1 -1
  150. package/dist/ui/renderers/workflow.markdown.js +223 -229
  151. package/dist/workflow/index.d.ts +8 -5
  152. package/dist/workflow/index.d.ts.map +1 -0
  153. package/dist/workflow/index.js +22 -6
  154. package/dist/workflow/workflow.enum.d.ts +4 -9
  155. package/dist/workflow/workflow.enum.d.ts.map +1 -1
  156. package/dist/workflow/workflow.enum.js +32 -42
  157. package/dist/workflow/workflow.event.d.ts +112 -118
  158. package/dist/workflow/workflow.event.d.ts.map +1 -1
  159. package/dist/workflow/workflow.event.js +7 -150
  160. package/dist/workflow/workflow.handler.d.ts +23 -24
  161. package/dist/workflow/workflow.handler.d.ts.map +1 -1
  162. package/dist/workflow/workflow.handler.js +6 -66
  163. package/dist/workflow/workflow.operations.d.ts +847 -853
  164. package/dist/workflow/workflow.operations.d.ts.map +1 -1
  165. package/dist/workflow/workflow.operations.js +9 -345
  166. package/dist/workflow/workflow.schema.d.ts +229 -234
  167. package/dist/workflow/workflow.schema.d.ts.map +1 -1
  168. package/dist/workflow/workflow.schema.js +146 -243
  169. package/dist/workflow-system.capability.d.ts +3 -8
  170. package/dist/workflow-system.capability.d.ts.map +1 -1
  171. package/dist/workflow-system.capability.js +6 -34
  172. package/dist/workflow-system.feature.d.ts +1 -6
  173. package/dist/workflow-system.feature.d.ts.map +1 -1
  174. package/dist/workflow-system.feature.js +4 -346
  175. package/package.json +415 -93
  176. package/dist/approval/approval.enum.js.map +0 -1
  177. package/dist/approval/approval.event.js.map +0 -1
  178. package/dist/approval/approval.handler.js +0 -72
  179. package/dist/approval/approval.handler.js.map +0 -1
  180. package/dist/approval/approval.operations.js.map +0 -1
  181. package/dist/approval/approval.schema.js.map +0 -1
  182. package/dist/docs/workflow-system.docblock.js.map +0 -1
  183. package/dist/entities/approval.js.map +0 -1
  184. package/dist/entities/index.js.map +0 -1
  185. package/dist/entities/instance.js.map +0 -1
  186. package/dist/entities/step.js.map +0 -1
  187. package/dist/entities/workflow.js.map +0 -1
  188. package/dist/example.js.map +0 -1
  189. package/dist/handlers/workflow.handlers.js.map +0 -1
  190. package/dist/instance/instance.enum.js.map +0 -1
  191. package/dist/instance/instance.event.js.map +0 -1
  192. package/dist/instance/instance.handler.js.map +0 -1
  193. package/dist/instance/instance.operations.js.map +0 -1
  194. package/dist/instance/instance.schema.js.map +0 -1
  195. package/dist/presentations/index.js.map +0 -1
  196. package/dist/seeders/index.js.map +0 -1
  197. package/dist/shared/mock-data.js.map +0 -1
  198. package/dist/state-machine/index.js.map +0 -1
  199. package/dist/tests/operations.test-spec.js.map +0 -1
  200. package/dist/ui/WorkflowDashboard.js.map +0 -1
  201. package/dist/ui/hooks/useWorkflowList.js.map +0 -1
  202. package/dist/ui/renderers/workflow.markdown.js.map +0 -1
  203. package/dist/workflow/workflow.enum.js.map +0 -1
  204. package/dist/workflow/workflow.event.js.map +0 -1
  205. package/dist/workflow/workflow.handler.js.map +0 -1
  206. package/dist/workflow/workflow.operations.js.map +0 -1
  207. package/dist/workflow/workflow.schema.js.map +0 -1
  208. package/dist/workflow-system.capability.js.map +0 -1
  209. package/dist/workflow-system.feature.js.map +0 -1
@@ -1,94 +1,357 @@
1
- import { mockDataStore } from "../shared/mock-data.js";
2
- import { buildStateMachineDefinition, createStateMachineEngine } from "../state-machine/index.js";
3
- import { createApprovalRequests } from "../approval/approval.handler.js";
1
+ // @bun
2
+ // src/shared/mock-data.ts
3
+ var mockDataStore = {
4
+ workflows: new Map,
5
+ steps: new Map,
6
+ instances: new Map,
7
+ approvals: new Map,
8
+ stepExecutions: new Map
9
+ };
4
10
 
5
- //#region src/instance/instance.handler.ts
11
+ // src/state-machine/index.ts
12
+ class BasicStateMachineEngine {
13
+ canTransition(definition, state, action, context) {
14
+ if (state.status !== "RUNNING" && state.status !== "WAITING") {
15
+ return {
16
+ allowed: false,
17
+ reason: `Workflow is ${state.status}, cannot transition`
18
+ };
19
+ }
20
+ const currentStep = definition.steps[state.currentStepKey];
21
+ if (!currentStep) {
22
+ return {
23
+ allowed: false,
24
+ reason: `Step ${state.currentStepKey} not found`
25
+ };
26
+ }
27
+ const transition = currentStep.transitions[action];
28
+ if (!transition) {
29
+ return {
30
+ allowed: false,
31
+ reason: `Action ${action} not available in step ${state.currentStepKey}`
32
+ };
33
+ }
34
+ if (currentStep.allowedRoles && currentStep.allowedRoles.length > 0) {
35
+ const hasRole = currentStep.allowedRoles.some((role) => context.userRoles.includes(role));
36
+ if (!hasRole) {
37
+ return {
38
+ allowed: false,
39
+ reason: `User lacks required role for this action`
40
+ };
41
+ }
42
+ }
43
+ if (typeof transition === "object" && transition.allowedRoles && transition.allowedRoles.length > 0) {
44
+ const hasRole = transition.allowedRoles.some((role) => context.userRoles.includes(role));
45
+ if (!hasRole) {
46
+ return {
47
+ allowed: false,
48
+ reason: `User lacks required role for action ${action}`
49
+ };
50
+ }
51
+ }
52
+ return { allowed: true };
53
+ }
54
+ getAvailableActions(definition, state, context) {
55
+ if (state.status !== "RUNNING" && state.status !== "WAITING") {
56
+ return [];
57
+ }
58
+ const currentStep = definition.steps[state.currentStepKey];
59
+ if (!currentStep) {
60
+ return [];
61
+ }
62
+ return Object.keys(currentStep.transitions).filter((action) => {
63
+ const result = this.canTransition(definition, state, action, context);
64
+ return result.allowed;
65
+ });
66
+ }
67
+ transition(definition, state, action, context) {
68
+ const validation = this.canTransition(definition, state, action, context);
69
+ if (!validation.allowed) {
70
+ return {
71
+ success: false,
72
+ previousStepKey: state.currentStepKey,
73
+ currentStepKey: state.currentStepKey,
74
+ status: state.status,
75
+ error: validation.reason
76
+ };
77
+ }
78
+ const currentStep = definition.steps[state.currentStepKey];
79
+ if (!currentStep) {
80
+ return {
81
+ success: false,
82
+ previousStepKey: state.currentStepKey,
83
+ currentStepKey: state.currentStepKey,
84
+ status: state.status,
85
+ error: `Current step ${state.currentStepKey} not found`
86
+ };
87
+ }
88
+ const transition = currentStep.transitions[action];
89
+ if (!transition) {
90
+ return {
91
+ success: false,
92
+ previousStepKey: state.currentStepKey,
93
+ currentStepKey: state.currentStepKey,
94
+ status: state.status,
95
+ error: `Transition for action ${action} not found`
96
+ };
97
+ }
98
+ const targetStepKey = typeof transition === "string" ? transition : transition.targetStepKey;
99
+ const targetStep = definition.steps[targetStepKey];
100
+ if (!targetStep) {
101
+ return {
102
+ success: false,
103
+ previousStepKey: state.currentStepKey,
104
+ currentStepKey: state.currentStepKey,
105
+ status: state.status,
106
+ error: `Target step ${targetStepKey} not found`
107
+ };
108
+ }
109
+ let newStatus = "RUNNING";
110
+ if (targetStep.type === "END") {
111
+ newStatus = "COMPLETED";
112
+ } else if (targetStep.type === "APPROVAL" || targetStep.type === "WAIT") {
113
+ newStatus = "WAITING";
114
+ }
115
+ return {
116
+ success: true,
117
+ previousStepKey: state.currentStepKey,
118
+ currentStepKey: targetStepKey,
119
+ status: newStatus
120
+ };
121
+ }
122
+ evaluateCondition(expression, contextData) {
123
+ try {
124
+ const match = expression.match(/^(\w+)\s*(>=|<=|>|<|===|!==|==|!=)\s*(.+)$/);
125
+ if (match) {
126
+ const [, prop, operator, value] = match;
127
+ if (!prop || !operator || value === undefined) {
128
+ return false;
129
+ }
130
+ const propValue = contextData[prop];
131
+ const compareValue = JSON.parse(value);
132
+ switch (operator) {
133
+ case ">":
134
+ return Number(propValue) > Number(compareValue);
135
+ case "<":
136
+ return Number(propValue) < Number(compareValue);
137
+ case ">=":
138
+ return Number(propValue) >= Number(compareValue);
139
+ case "<=":
140
+ return Number(propValue) <= Number(compareValue);
141
+ case "===":
142
+ case "==":
143
+ return propValue === compareValue;
144
+ case "!==":
145
+ case "!=":
146
+ return propValue !== compareValue;
147
+ }
148
+ }
149
+ if (expression in contextData) {
150
+ return Boolean(contextData[expression]);
151
+ }
152
+ return false;
153
+ } catch {
154
+ return false;
155
+ }
156
+ }
157
+ }
158
+ function createStateMachineEngine() {
159
+ return new BasicStateMachineEngine;
160
+ }
161
+ function buildStateMachineDefinition(workflow, steps) {
162
+ const stepMap = {};
163
+ for (const step of steps) {
164
+ stepMap[step.key] = {
165
+ key: step.key,
166
+ name: step.name,
167
+ type: step.type,
168
+ transitions: step.transitions,
169
+ approvalMode: step.approvalMode,
170
+ allowedRoles: step.approverRoles,
171
+ timeoutSeconds: step.timeoutSeconds,
172
+ conditionExpression: step.conditionExpression
173
+ };
174
+ }
175
+ const startStep = steps.find((s) => s.type === "START");
176
+ const initialStepKey = startStep?.key ?? steps[0]?.key ?? "";
177
+ return {
178
+ key: workflow.key,
179
+ name: workflow.name,
180
+ version: workflow.version,
181
+ initialStepKey,
182
+ steps: stepMap
183
+ };
184
+ }
185
+ function createInitialState(definition, contextData = {}) {
186
+ return {
187
+ currentStepKey: definition.initialStepKey,
188
+ status: "RUNNING",
189
+ contextData,
190
+ history: []
191
+ };
192
+ }
193
+
194
+ // src/approval/approval.handler.ts
195
+ async function createApprovalRequests(instance, step, _context) {
196
+ const now = new Date;
197
+ for (let i = 0;i < step.approverRoles.length; i++) {
198
+ const role = step.approverRoles[i];
199
+ const id = `approval_${Date.now()}_${i}`;
200
+ const request = {
201
+ id,
202
+ workflowInstanceId: instance.id,
203
+ stepExecutionId: `exec_${instance.id}_${step.id}`,
204
+ approverId: `user_${role}`,
205
+ approverRole: role,
206
+ title: `Approval required for ${step.name}`,
207
+ description: step.description,
208
+ status: "PENDING",
209
+ contextSnapshot: instance.contextData,
210
+ sequenceOrder: i,
211
+ createdAt: now,
212
+ updatedAt: now
213
+ };
214
+ mockDataStore.approvals.set(id, request);
215
+ }
216
+ }
217
+ async function handleSubmitDecision(input, context) {
218
+ const request = mockDataStore.approvals.get(input.requestId);
219
+ if (!request) {
220
+ throw new Error(`Approval request ${input.requestId} not found`);
221
+ }
222
+ if (request.approverId !== context.userId && !context.userRoles.includes(request.approverRole ?? "")) {
223
+ throw new Error("User is not authorized to make this decision");
224
+ }
225
+ const now = new Date;
226
+ request.decision = input.decision;
227
+ request.decisionComment = input.comment;
228
+ request.decidedAt = now;
229
+ request.updatedAt = now;
230
+ if (input.decision === "APPROVE") {
231
+ request.status = "APPROVED";
232
+ await handleTransitionWorkflow({
233
+ instanceId: request.workflowInstanceId,
234
+ action: "approve",
235
+ data: input.data,
236
+ comment: input.comment
237
+ }, context);
238
+ } else if (input.decision === "REJECT") {
239
+ request.status = "REJECTED";
240
+ await handleTransitionWorkflow({
241
+ instanceId: request.workflowInstanceId,
242
+ action: "reject",
243
+ data: input.data,
244
+ comment: input.comment
245
+ }, context);
246
+ }
247
+ return request;
248
+ }
249
+ async function handleListMyApprovals(input, context) {
250
+ let requests = Array.from(mockDataStore.approvals.values()).filter((r) => r.approverId === context.userId || context.userRoles.includes(r.approverRole ?? ""));
251
+ const pendingCount = requests.filter((r) => r.status === "PENDING").length;
252
+ if (input.status) {
253
+ requests = requests.filter((r) => r.status === input.status);
254
+ }
255
+ const total = requests.length;
256
+ const offset = input.offset ?? 0;
257
+ const limit = input.limit ?? 20;
258
+ requests = requests.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime()).slice(offset, offset + limit);
259
+ return { requests, total, pendingCount };
260
+ }
261
+
262
+ // src/instance/instance.handler.ts
6
263
  async function handleStartWorkflow(input, context) {
7
- const workflow = Array.from(mockDataStore.workflows.values()).find((w) => w.key === input.workflowKey && w.status === "ACTIVE" && w.organizationId === context.organizationId);
8
- if (!workflow) throw new Error(`Active workflow ${input.workflowKey} not found`);
9
- const id = `inst_${Date.now()}`;
10
- const now = /* @__PURE__ */ new Date();
11
- const instance = {
12
- id,
13
- workflowDefinitionId: workflow.id,
14
- referenceId: input.referenceId,
15
- referenceType: input.referenceType,
16
- status: "RUNNING",
17
- currentStepId: workflow.initialStepId,
18
- contextData: input.contextData ?? {},
19
- triggeredBy: context.userId,
20
- organizationId: context.organizationId,
21
- priority: input.priority ?? 0,
22
- dueAt: input.dueAt,
23
- createdAt: now,
24
- updatedAt: now,
25
- startedAt: now
26
- };
27
- mockDataStore.instances.set(id, instance);
28
- if (workflow.initialStepId) {
29
- const firstStep = mockDataStore.steps.get(workflow.initialStepId);
30
- if (firstStep?.type === "APPROVAL") {
31
- instance.status = "WAITING";
32
- await createApprovalRequests(instance, firstStep, context);
33
- }
34
- }
35
- return instance;
264
+ const workflow = Array.from(mockDataStore.workflows.values()).find((w) => w.key === input.workflowKey && w.status === "ACTIVE" && w.organizationId === context.organizationId);
265
+ if (!workflow) {
266
+ throw new Error(`Active workflow ${input.workflowKey} not found`);
267
+ }
268
+ const id = `inst_${Date.now()}`;
269
+ const now = new Date;
270
+ const instance = {
271
+ id,
272
+ workflowDefinitionId: workflow.id,
273
+ referenceId: input.referenceId,
274
+ referenceType: input.referenceType,
275
+ status: "RUNNING",
276
+ currentStepId: workflow.initialStepId,
277
+ contextData: input.contextData ?? {},
278
+ triggeredBy: context.userId,
279
+ organizationId: context.organizationId,
280
+ priority: input.priority ?? 0,
281
+ dueAt: input.dueAt,
282
+ createdAt: now,
283
+ updatedAt: now,
284
+ startedAt: now
285
+ };
286
+ mockDataStore.instances.set(id, instance);
287
+ if (workflow.initialStepId) {
288
+ const firstStep = mockDataStore.steps.get(workflow.initialStepId);
289
+ if (firstStep?.type === "APPROVAL") {
290
+ instance.status = "WAITING";
291
+ await createApprovalRequests(instance, firstStep, context);
292
+ }
293
+ }
294
+ return instance;
36
295
  }
37
296
  async function handleTransitionWorkflow(input, context) {
38
- const instance = mockDataStore.instances.get(input.instanceId);
39
- if (!instance) throw new Error(`Instance ${input.instanceId} not found`);
40
- const workflow = mockDataStore.workflows.get(instance.workflowDefinitionId);
41
- if (!workflow) throw new Error(`Workflow ${instance.workflowDefinitionId} not found`);
42
- const steps = Array.from(mockDataStore.steps.values()).filter((s) => s.workflowDefinitionId === workflow.id);
43
- const definition = buildStateMachineDefinition({
44
- key: workflow.key,
45
- name: workflow.name,
46
- version: workflow.version,
47
- initialStepId: workflow.initialStepId ?? null
48
- }, steps);
49
- const currentStep = steps.find((s) => s.id === instance.currentStepId);
50
- const state = {
51
- currentStepKey: currentStep?.key ?? "",
52
- status: instance.status,
53
- contextData: instance.contextData,
54
- history: []
55
- };
56
- const transitionContext = {
57
- userId: context.userId,
58
- userRoles: context.userRoles,
59
- data: input.data
60
- };
61
- const result = createStateMachineEngine().transition(definition, state, input.action, transitionContext);
62
- if (!result.success) return {
63
- success: false,
64
- instance,
65
- message: result.error
66
- };
67
- const previousStepKey = currentStep?.key;
68
- const newStep = steps.find((s) => s.key === result.currentStepKey);
69
- instance.currentStepId = newStep?.id;
70
- instance.status = result.status;
71
- instance.contextData = {
72
- ...instance.contextData,
73
- ...input.data
74
- };
75
- instance.updatedAt = /* @__PURE__ */ new Date();
76
- if (result.status === "COMPLETED") {
77
- instance.completedAt = /* @__PURE__ */ new Date();
78
- instance.outcome = input.action;
79
- }
80
- if (newStep?.type === "APPROVAL") {
81
- instance.status = "WAITING";
82
- await createApprovalRequests(instance, newStep, context);
83
- }
84
- return {
85
- success: true,
86
- instance,
87
- previousStepKey,
88
- currentStepKey: result.currentStepKey ?? void 0
89
- };
297
+ const instance = mockDataStore.instances.get(input.instanceId);
298
+ if (!instance) {
299
+ throw new Error(`Instance ${input.instanceId} not found`);
300
+ }
301
+ const workflow = mockDataStore.workflows.get(instance.workflowDefinitionId);
302
+ if (!workflow) {
303
+ throw new Error(`Workflow ${instance.workflowDefinitionId} not found`);
304
+ }
305
+ const steps = Array.from(mockDataStore.steps.values()).filter((s) => s.workflowDefinitionId === workflow.id);
306
+ const definition = buildStateMachineDefinition({
307
+ key: workflow.key,
308
+ name: workflow.name,
309
+ version: workflow.version,
310
+ initialStepId: workflow.initialStepId ?? null
311
+ }, steps);
312
+ const currentStep = steps.find((s) => s.id === instance.currentStepId);
313
+ const state = {
314
+ currentStepKey: currentStep?.key ?? "",
315
+ status: instance.status,
316
+ contextData: instance.contextData,
317
+ history: []
318
+ };
319
+ const transitionContext = {
320
+ userId: context.userId,
321
+ userRoles: context.userRoles,
322
+ data: input.data
323
+ };
324
+ const engine = createStateMachineEngine();
325
+ const result = engine.transition(definition, state, input.action, transitionContext);
326
+ if (!result.success) {
327
+ return {
328
+ success: false,
329
+ instance,
330
+ message: result.error
331
+ };
332
+ }
333
+ const previousStepKey = currentStep?.key;
334
+ const newStep = steps.find((s) => s.key === result.currentStepKey);
335
+ instance.currentStepId = newStep?.id;
336
+ instance.status = result.status;
337
+ instance.contextData = { ...instance.contextData, ...input.data };
338
+ instance.updatedAt = new Date;
339
+ if (result.status === "COMPLETED") {
340
+ instance.completedAt = new Date;
341
+ instance.outcome = input.action;
342
+ }
343
+ if (newStep?.type === "APPROVAL") {
344
+ instance.status = "WAITING";
345
+ await createApprovalRequests(instance, newStep, context);
346
+ }
347
+ return {
348
+ success: true,
349
+ instance,
350
+ previousStepKey,
351
+ currentStepKey: result.currentStepKey ?? undefined
352
+ };
90
353
  }
91
-
92
- //#endregion
93
- export { handleStartWorkflow, handleTransitionWorkflow };
94
- //# sourceMappingURL=instance.handler.js.map
354
+ export {
355
+ handleTransitionWorkflow,
356
+ handleStartWorkflow
357
+ };