@contractspec/example.workflow-system 3.7.6 → 3.8.2
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.
- package/README.md +72 -119
- package/dist/approval/approval.event.js +1 -1
- package/dist/approval/approval.handler.d.ts +1 -1
- package/dist/approval/index.d.ts +4 -4
- package/dist/approval/index.js +1 -1
- package/dist/browser/approval/approval.event.js +1 -1
- package/dist/browser/approval/index.js +1 -1
- package/dist/browser/entities/index.js +166 -166
- package/dist/browser/handlers/index.js +43 -43
- package/dist/browser/handlers/workflow.handlers.js +43 -43
- package/dist/browser/index.js +1935 -1548
- package/dist/browser/instance/index.js +210 -210
- package/dist/browser/instance/instance.event.js +1 -1
- package/dist/browser/shared/demo-scenario.js +213 -0
- package/dist/browser/ui/WorkflowDashboard.visualizations.js +239 -0
- package/dist/browser/ui/hooks/index.js +0 -47
- package/dist/browser/ui/hooks/useWorkflowList.js +6 -4
- package/dist/browser/ui/index.js +6 -4
- package/dist/browser/ui/renderers/index.js +409 -73
- package/dist/browser/ui/renderers/workflow.markdown.js +409 -73
- package/dist/browser/visualizations/catalog.js +132 -0
- package/dist/browser/visualizations/index.js +133 -0
- package/dist/browser/visualizations/selectors.js +195 -0
- package/dist/entities/index.d.ts +53 -53
- package/dist/entities/index.js +166 -166
- package/dist/example.test.d.ts +1 -0
- package/dist/handlers/index.js +43 -43
- package/dist/handlers/workflow.handlers.js +43 -43
- package/dist/index.d.ts +6 -5
- package/dist/index.js +1935 -1548
- package/dist/instance/index.d.ts +3 -3
- package/dist/instance/index.js +210 -210
- package/dist/instance/instance.event.js +1 -1
- package/dist/instance/instance.handler.d.ts +1 -1
- package/dist/shared/demo-scenario.d.ts +43 -0
- package/dist/shared/demo-scenario.js +214 -0
- package/dist/shared/index.d.ts +1 -1
- package/dist/shared/mock-data.d.ts +1 -1
- package/dist/ui/WorkflowDashboard.visualizations.d.ts +4 -0
- package/dist/ui/WorkflowDashboard.visualizations.js +240 -0
- package/dist/ui/hooks/index.js +0 -47
- package/dist/ui/hooks/useWorkflowList.d.ts +2 -1
- package/dist/ui/hooks/useWorkflowList.js +6 -4
- package/dist/ui/index.d.ts +1 -1
- package/dist/ui/index.js +6 -4
- package/dist/ui/renderers/index.js +409 -73
- package/dist/ui/renderers/workflow.markdown.js +409 -73
- package/dist/visualizations/catalog.d.ts +11 -0
- package/dist/visualizations/catalog.js +133 -0
- package/dist/visualizations/index.d.ts +2 -0
- package/dist/visualizations/index.js +134 -0
- package/dist/visualizations/selectors.d.ts +11 -0
- package/dist/visualizations/selectors.js +196 -0
- package/dist/visualizations/selectors.test.d.ts +1 -0
- package/dist/workflow/index.d.ts +4 -4
- package/dist/workflow/workflow.handler.d.ts +1 -1
- package/package.json +71 -10
package/dist/entities/index.js
CHANGED
|
@@ -113,82 +113,160 @@ var ApprovalCommentEntity = defineEntity({
|
|
|
113
113
|
indexes: [index.on(["approvalRequestId", "createdAt"])]
|
|
114
114
|
});
|
|
115
115
|
|
|
116
|
-
// src/entities/
|
|
116
|
+
// src/entities/instance.ts
|
|
117
117
|
import {
|
|
118
118
|
defineEntity as defineEntity2,
|
|
119
119
|
defineEntityEnum as defineEntityEnum2,
|
|
120
120
|
field as field2,
|
|
121
121
|
index as index2
|
|
122
122
|
} from "@contractspec/lib.schema";
|
|
123
|
-
var
|
|
124
|
-
name: "
|
|
125
|
-
values: [
|
|
123
|
+
var InstanceStatusEnum = defineEntityEnum2({
|
|
124
|
+
name: "InstanceStatus",
|
|
125
|
+
values: [
|
|
126
|
+
"PENDING",
|
|
127
|
+
"RUNNING",
|
|
128
|
+
"WAITING",
|
|
129
|
+
"PAUSED",
|
|
130
|
+
"COMPLETED",
|
|
131
|
+
"CANCELLED",
|
|
132
|
+
"FAILED",
|
|
133
|
+
"TIMEOUT"
|
|
134
|
+
],
|
|
126
135
|
schema: "workflow",
|
|
127
|
-
description: "Status of a workflow
|
|
136
|
+
description: "Status of a workflow instance."
|
|
128
137
|
});
|
|
129
|
-
var
|
|
130
|
-
name: "
|
|
131
|
-
values: [
|
|
138
|
+
var StepExecutionStatusEnum = defineEntityEnum2({
|
|
139
|
+
name: "StepExecutionStatus",
|
|
140
|
+
values: [
|
|
141
|
+
"PENDING",
|
|
142
|
+
"ACTIVE",
|
|
143
|
+
"COMPLETED",
|
|
144
|
+
"SKIPPED",
|
|
145
|
+
"FAILED",
|
|
146
|
+
"TIMEOUT"
|
|
147
|
+
],
|
|
132
148
|
schema: "workflow",
|
|
133
|
-
description: "
|
|
149
|
+
description: "Status of a step execution."
|
|
134
150
|
});
|
|
135
|
-
var
|
|
136
|
-
name: "
|
|
137
|
-
description: "A
|
|
151
|
+
var WorkflowInstanceEntity = defineEntity2({
|
|
152
|
+
name: "WorkflowInstance",
|
|
153
|
+
description: "A running instance of a workflow definition.",
|
|
138
154
|
schema: "workflow",
|
|
139
|
-
map: "
|
|
155
|
+
map: "workflow_instance",
|
|
140
156
|
fields: {
|
|
141
|
-
id: field2.id({ description: "Unique
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
157
|
+
id: field2.id({ description: "Unique instance ID" }),
|
|
158
|
+
workflowDefinitionId: field2.foreignKey(),
|
|
159
|
+
referenceId: field2.string({
|
|
160
|
+
isOptional: true,
|
|
161
|
+
description: "External reference (e.g., order ID)"
|
|
145
162
|
}),
|
|
146
|
-
|
|
163
|
+
referenceType: field2.string({
|
|
147
164
|
isOptional: true,
|
|
148
|
-
description:
|
|
165
|
+
description: 'Type of reference (e.g., "Order")'
|
|
149
166
|
}),
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
167
|
+
status: field2.enum("InstanceStatus", { default: "PENDING" }),
|
|
168
|
+
currentStepId: field2.string({
|
|
169
|
+
isOptional: true,
|
|
170
|
+
description: "Current step being executed"
|
|
171
|
+
}),
|
|
172
|
+
contextData: field2.json({ description: "Data context for this instance" }),
|
|
173
|
+
triggeredBy: field2.foreignKey({
|
|
174
|
+
description: "User who triggered the workflow"
|
|
153
175
|
}),
|
|
154
|
-
|
|
155
|
-
triggerType: field2.enum("WorkflowTriggerType", { default: "MANUAL" }),
|
|
156
|
-
triggerConfig: field2.json({
|
|
176
|
+
triggerSource: field2.string({
|
|
157
177
|
isOptional: true,
|
|
158
|
-
description: "
|
|
178
|
+
description: 'Source of trigger (e.g., "api", "ui")'
|
|
179
|
+
}),
|
|
180
|
+
organizationId: field2.foreignKey(),
|
|
181
|
+
priority: field2.int({
|
|
182
|
+
default: 0,
|
|
183
|
+
description: "Processing priority (higher = more urgent)"
|
|
159
184
|
}),
|
|
160
|
-
|
|
185
|
+
dueAt: field2.dateTime({
|
|
161
186
|
isOptional: true,
|
|
162
|
-
description: "
|
|
187
|
+
description: "When this instance should complete"
|
|
163
188
|
}),
|
|
164
|
-
|
|
189
|
+
outcome: field2.string({
|
|
165
190
|
isOptional: true,
|
|
166
|
-
description:
|
|
191
|
+
description: 'Final outcome (e.g., "approved", "rejected")'
|
|
167
192
|
}),
|
|
168
|
-
|
|
193
|
+
resultData: field2.json({
|
|
169
194
|
isOptional: true,
|
|
170
|
-
description: "
|
|
195
|
+
description: "Final result data"
|
|
171
196
|
}),
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
description: "User who created this workflow"
|
|
197
|
+
errorMessage: field2.string({
|
|
198
|
+
isOptional: true,
|
|
199
|
+
description: "Error message if failed"
|
|
176
200
|
}),
|
|
177
201
|
createdAt: field2.createdAt(),
|
|
178
202
|
updatedAt: field2.updatedAt(),
|
|
179
|
-
|
|
203
|
+
startedAt: field2.dateTime({ isOptional: true }),
|
|
204
|
+
completedAt: field2.dateTime({ isOptional: true }),
|
|
205
|
+
workflowDefinition: field2.belongsTo("WorkflowDefinition", ["workflowDefinitionId"], ["id"]),
|
|
206
|
+
currentStep: field2.belongsTo("WorkflowStep", ["currentStepId"], ["id"]),
|
|
207
|
+
stepExecutions: field2.hasMany("StepExecution"),
|
|
208
|
+
approvalRequests: field2.hasMany("ApprovalRequest")
|
|
209
|
+
},
|
|
210
|
+
indexes: [
|
|
211
|
+
index2.on(["organizationId", "status"]),
|
|
212
|
+
index2.on(["workflowDefinitionId", "status"]),
|
|
213
|
+
index2.on(["referenceType", "referenceId"]),
|
|
214
|
+
index2.on(["triggeredBy", "status"]),
|
|
215
|
+
index2.on(["status", "dueAt"]),
|
|
216
|
+
index2.on(["createdAt"])
|
|
217
|
+
],
|
|
218
|
+
enums: [InstanceStatusEnum]
|
|
219
|
+
});
|
|
220
|
+
var StepExecutionEntity = defineEntity2({
|
|
221
|
+
name: "StepExecution",
|
|
222
|
+
description: "Execution record of a step within a workflow instance.",
|
|
223
|
+
schema: "workflow",
|
|
224
|
+
map: "step_execution",
|
|
225
|
+
fields: {
|
|
226
|
+
id: field2.id({ description: "Unique execution ID" }),
|
|
227
|
+
workflowInstanceId: field2.foreignKey(),
|
|
228
|
+
workflowStepId: field2.foreignKey(),
|
|
229
|
+
status: field2.enum("StepExecutionStatus", { default: "PENDING" }),
|
|
230
|
+
executionOrder: field2.int({
|
|
231
|
+
default: 0,
|
|
232
|
+
description: "Order of execution within instance"
|
|
233
|
+
}),
|
|
234
|
+
inputData: field2.json({
|
|
180
235
|
isOptional: true,
|
|
181
|
-
description: "
|
|
236
|
+
description: "Data when entering step"
|
|
237
|
+
}),
|
|
238
|
+
outputData: field2.json({
|
|
239
|
+
isOptional: true,
|
|
240
|
+
description: "Data when exiting step"
|
|
241
|
+
}),
|
|
242
|
+
actionTaken: field2.string({
|
|
243
|
+
isOptional: true,
|
|
244
|
+
description: 'Action that caused transition (e.g., "approve")'
|
|
182
245
|
}),
|
|
183
|
-
|
|
184
|
-
|
|
246
|
+
transitionedTo: field2.string({
|
|
247
|
+
isOptional: true,
|
|
248
|
+
description: "Step key transitioned to"
|
|
249
|
+
}),
|
|
250
|
+
executedBy: field2.string({
|
|
251
|
+
isOptional: true,
|
|
252
|
+
description: "User who completed this step"
|
|
253
|
+
}),
|
|
254
|
+
errorMessage: field2.string({ isOptional: true }),
|
|
255
|
+
errorDetails: field2.json({ isOptional: true }),
|
|
256
|
+
retryCount: field2.int({ default: 0 }),
|
|
257
|
+
createdAt: field2.createdAt(),
|
|
258
|
+
updatedAt: field2.updatedAt(),
|
|
259
|
+
startedAt: field2.dateTime({ isOptional: true }),
|
|
260
|
+
completedAt: field2.dateTime({ isOptional: true }),
|
|
261
|
+
workflowInstance: field2.belongsTo("WorkflowInstance", ["workflowInstanceId"], ["id"], { onDelete: "Cascade" }),
|
|
262
|
+
workflowStep: field2.belongsTo("WorkflowStep", ["workflowStepId"], ["id"])
|
|
185
263
|
},
|
|
186
264
|
indexes: [
|
|
187
|
-
index2.
|
|
188
|
-
index2.on(["
|
|
189
|
-
index2.on(["
|
|
265
|
+
index2.on(["workflowInstanceId", "executionOrder"]),
|
|
266
|
+
index2.on(["workflowInstanceId", "workflowStepId"]),
|
|
267
|
+
index2.on(["status"])
|
|
190
268
|
],
|
|
191
|
-
enums: [
|
|
269
|
+
enums: [StepExecutionStatusEnum]
|
|
192
270
|
});
|
|
193
271
|
|
|
194
272
|
// src/entities/step.ts
|
|
@@ -311,160 +389,82 @@ var WorkflowStepEntity = defineEntity3({
|
|
|
311
389
|
enums: [StepTypeEnum, ApprovalModeEnum]
|
|
312
390
|
});
|
|
313
391
|
|
|
314
|
-
// src/entities/
|
|
392
|
+
// src/entities/workflow.ts
|
|
315
393
|
import {
|
|
316
394
|
defineEntity as defineEntity4,
|
|
317
395
|
defineEntityEnum as defineEntityEnum4,
|
|
318
396
|
field as field4,
|
|
319
397
|
index as index4
|
|
320
398
|
} from "@contractspec/lib.schema";
|
|
321
|
-
var
|
|
322
|
-
name: "
|
|
323
|
-
values: [
|
|
324
|
-
"PENDING",
|
|
325
|
-
"RUNNING",
|
|
326
|
-
"WAITING",
|
|
327
|
-
"PAUSED",
|
|
328
|
-
"COMPLETED",
|
|
329
|
-
"CANCELLED",
|
|
330
|
-
"FAILED",
|
|
331
|
-
"TIMEOUT"
|
|
332
|
-
],
|
|
399
|
+
var WorkflowStatusEnum = defineEntityEnum4({
|
|
400
|
+
name: "WorkflowStatus",
|
|
401
|
+
values: ["DRAFT", "ACTIVE", "DEPRECATED", "ARCHIVED"],
|
|
333
402
|
schema: "workflow",
|
|
334
|
-
description: "Status of a workflow
|
|
403
|
+
description: "Status of a workflow definition."
|
|
335
404
|
});
|
|
336
|
-
var
|
|
337
|
-
name: "
|
|
338
|
-
values: [
|
|
339
|
-
"PENDING",
|
|
340
|
-
"ACTIVE",
|
|
341
|
-
"COMPLETED",
|
|
342
|
-
"SKIPPED",
|
|
343
|
-
"FAILED",
|
|
344
|
-
"TIMEOUT"
|
|
345
|
-
],
|
|
405
|
+
var WorkflowTriggerTypeEnum = defineEntityEnum4({
|
|
406
|
+
name: "WorkflowTriggerType",
|
|
407
|
+
values: ["MANUAL", "EVENT", "SCHEDULED", "API"],
|
|
346
408
|
schema: "workflow",
|
|
347
|
-
description: "
|
|
409
|
+
description: "What triggers workflow instantiation."
|
|
348
410
|
});
|
|
349
|
-
var
|
|
350
|
-
name: "
|
|
351
|
-
description: "A
|
|
411
|
+
var WorkflowDefinitionEntity = defineEntity4({
|
|
412
|
+
name: "WorkflowDefinition",
|
|
413
|
+
description: "A workflow blueprint that defines the process structure.",
|
|
352
414
|
schema: "workflow",
|
|
353
|
-
map: "
|
|
415
|
+
map: "workflow_definition",
|
|
354
416
|
fields: {
|
|
355
|
-
id: field4.id({ description: "Unique
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
description: "External reference (e.g., order ID)"
|
|
360
|
-
}),
|
|
361
|
-
referenceType: field4.string({
|
|
362
|
-
isOptional: true,
|
|
363
|
-
description: 'Type of reference (e.g., "Order")'
|
|
417
|
+
id: field4.id({ description: "Unique workflow definition ID" }),
|
|
418
|
+
name: field4.string({ description: "Human-readable workflow name" }),
|
|
419
|
+
key: field4.string({
|
|
420
|
+
description: 'Unique key for referencing (e.g., "purchase_approval")'
|
|
364
421
|
}),
|
|
365
|
-
|
|
366
|
-
currentStepId: field4.string({
|
|
422
|
+
description: field4.string({
|
|
367
423
|
isOptional: true,
|
|
368
|
-
description: "
|
|
424
|
+
description: "Detailed description"
|
|
369
425
|
}),
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
description: "
|
|
426
|
+
version: field4.int({
|
|
427
|
+
default: 1,
|
|
428
|
+
description: "Version number for versioning definitions"
|
|
373
429
|
}),
|
|
374
|
-
|
|
430
|
+
status: field4.enum("WorkflowStatus", { default: "DRAFT" }),
|
|
431
|
+
triggerType: field4.enum("WorkflowTriggerType", { default: "MANUAL" }),
|
|
432
|
+
triggerConfig: field4.json({
|
|
375
433
|
isOptional: true,
|
|
376
|
-
description:
|
|
377
|
-
}),
|
|
378
|
-
organizationId: field4.foreignKey(),
|
|
379
|
-
priority: field4.int({
|
|
380
|
-
default: 0,
|
|
381
|
-
description: "Processing priority (higher = more urgent)"
|
|
434
|
+
description: "Trigger-specific configuration"
|
|
382
435
|
}),
|
|
383
|
-
|
|
436
|
+
initialStepId: field4.string({
|
|
384
437
|
isOptional: true,
|
|
385
|
-
description: "
|
|
438
|
+
description: "First step when workflow starts"
|
|
386
439
|
}),
|
|
387
|
-
|
|
440
|
+
featureFlagKey: field4.string({
|
|
388
441
|
isOptional: true,
|
|
389
|
-
description:
|
|
442
|
+
description: "Feature flag to control availability"
|
|
390
443
|
}),
|
|
391
|
-
|
|
444
|
+
settings: field4.json({
|
|
392
445
|
isOptional: true,
|
|
393
|
-
description: "
|
|
446
|
+
description: "Workflow-wide settings"
|
|
394
447
|
}),
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
448
|
+
metadata: field4.json({ isOptional: true, description: "Custom metadata" }),
|
|
449
|
+
organizationId: field4.foreignKey({ description: "Owning organization" }),
|
|
450
|
+
createdBy: field4.foreignKey({
|
|
451
|
+
description: "User who created this workflow"
|
|
398
452
|
}),
|
|
399
453
|
createdAt: field4.createdAt(),
|
|
400
454
|
updatedAt: field4.updatedAt(),
|
|
401
|
-
|
|
402
|
-
completedAt: field4.dateTime({ isOptional: true }),
|
|
403
|
-
workflowDefinition: field4.belongsTo("WorkflowDefinition", ["workflowDefinitionId"], ["id"]),
|
|
404
|
-
currentStep: field4.belongsTo("WorkflowStep", ["currentStepId"], ["id"]),
|
|
405
|
-
stepExecutions: field4.hasMany("StepExecution"),
|
|
406
|
-
approvalRequests: field4.hasMany("ApprovalRequest")
|
|
407
|
-
},
|
|
408
|
-
indexes: [
|
|
409
|
-
index4.on(["organizationId", "status"]),
|
|
410
|
-
index4.on(["workflowDefinitionId", "status"]),
|
|
411
|
-
index4.on(["referenceType", "referenceId"]),
|
|
412
|
-
index4.on(["triggeredBy", "status"]),
|
|
413
|
-
index4.on(["status", "dueAt"]),
|
|
414
|
-
index4.on(["createdAt"])
|
|
415
|
-
],
|
|
416
|
-
enums: [InstanceStatusEnum]
|
|
417
|
-
});
|
|
418
|
-
var StepExecutionEntity = defineEntity4({
|
|
419
|
-
name: "StepExecution",
|
|
420
|
-
description: "Execution record of a step within a workflow instance.",
|
|
421
|
-
schema: "workflow",
|
|
422
|
-
map: "step_execution",
|
|
423
|
-
fields: {
|
|
424
|
-
id: field4.id({ description: "Unique execution ID" }),
|
|
425
|
-
workflowInstanceId: field4.foreignKey(),
|
|
426
|
-
workflowStepId: field4.foreignKey(),
|
|
427
|
-
status: field4.enum("StepExecutionStatus", { default: "PENDING" }),
|
|
428
|
-
executionOrder: field4.int({
|
|
429
|
-
default: 0,
|
|
430
|
-
description: "Order of execution within instance"
|
|
431
|
-
}),
|
|
432
|
-
inputData: field4.json({
|
|
433
|
-
isOptional: true,
|
|
434
|
-
description: "Data when entering step"
|
|
435
|
-
}),
|
|
436
|
-
outputData: field4.json({
|
|
437
|
-
isOptional: true,
|
|
438
|
-
description: "Data when exiting step"
|
|
439
|
-
}),
|
|
440
|
-
actionTaken: field4.string({
|
|
455
|
+
publishedAt: field4.dateTime({
|
|
441
456
|
isOptional: true,
|
|
442
|
-
description:
|
|
443
|
-
}),
|
|
444
|
-
transitionedTo: field4.string({
|
|
445
|
-
isOptional: true,
|
|
446
|
-
description: "Step key transitioned to"
|
|
447
|
-
}),
|
|
448
|
-
executedBy: field4.string({
|
|
449
|
-
isOptional: true,
|
|
450
|
-
description: "User who completed this step"
|
|
457
|
+
description: "When workflow was activated"
|
|
451
458
|
}),
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
retryCount: field4.int({ default: 0 }),
|
|
455
|
-
createdAt: field4.createdAt(),
|
|
456
|
-
updatedAt: field4.updatedAt(),
|
|
457
|
-
startedAt: field4.dateTime({ isOptional: true }),
|
|
458
|
-
completedAt: field4.dateTime({ isOptional: true }),
|
|
459
|
-
workflowInstance: field4.belongsTo("WorkflowInstance", ["workflowInstanceId"], ["id"], { onDelete: "Cascade" }),
|
|
460
|
-
workflowStep: field4.belongsTo("WorkflowStep", ["workflowStepId"], ["id"])
|
|
459
|
+
steps: field4.hasMany("WorkflowStep"),
|
|
460
|
+
instances: field4.hasMany("WorkflowInstance")
|
|
461
461
|
},
|
|
462
462
|
indexes: [
|
|
463
|
-
index4.
|
|
464
|
-
index4.on(["
|
|
465
|
-
index4.on(["
|
|
463
|
+
index4.unique(["organizationId", "key", "version"]),
|
|
464
|
+
index4.on(["organizationId", "status"]),
|
|
465
|
+
index4.on(["key", "version"])
|
|
466
466
|
],
|
|
467
|
-
enums: [
|
|
467
|
+
enums: [WorkflowStatusEnum, WorkflowTriggerTypeEnum]
|
|
468
468
|
});
|
|
469
469
|
// src/entities/index.ts
|
|
470
470
|
var workflowSystemEntities = [
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/handlers/index.js
CHANGED
|
@@ -55,9 +55,19 @@ function rowToApproval(row) {
|
|
|
55
55
|
};
|
|
56
56
|
}
|
|
57
57
|
function createWorkflowHandlers(db) {
|
|
58
|
+
function normalizeSql(sql) {
|
|
59
|
+
let placeholderIndex = 0;
|
|
60
|
+
return sql.replace(/\?/g, () => `$${++placeholderIndex}`);
|
|
61
|
+
}
|
|
62
|
+
async function queryRows(sql, params = []) {
|
|
63
|
+
return (await db.query(normalizeSql(sql), params)).rows;
|
|
64
|
+
}
|
|
65
|
+
async function execute(sql, params = []) {
|
|
66
|
+
await db.execute(normalizeSql(sql), params);
|
|
67
|
+
}
|
|
58
68
|
async function listDefinitions(input) {
|
|
59
69
|
const { projectId, status, search, limit = 20, offset = 0 } = input;
|
|
60
|
-
let whereClause =
|
|
70
|
+
let whereClause = 'WHERE "projectId" = ?';
|
|
61
71
|
const params = [projectId];
|
|
62
72
|
if (status && status !== "all") {
|
|
63
73
|
whereClause += " AND status = ?";
|
|
@@ -67,9 +77,9 @@ function createWorkflowHandlers(db) {
|
|
|
67
77
|
whereClause += " AND name LIKE ?";
|
|
68
78
|
params.push(`%${search}%`);
|
|
69
79
|
}
|
|
70
|
-
const countResult =
|
|
80
|
+
const countResult = await queryRows(`SELECT COUNT(*) as count FROM workflow_definition ${whereClause}`, params);
|
|
71
81
|
const total = countResult[0]?.count ?? 0;
|
|
72
|
-
const rows =
|
|
82
|
+
const rows = await queryRows(`SELECT * FROM workflow_definition ${whereClause} ORDER BY "updatedAt" DESC LIMIT ? OFFSET ?`, [...params, limit, offset]);
|
|
73
83
|
return {
|
|
74
84
|
definitions: rows.map(rowToDefinition),
|
|
75
85
|
total
|
|
@@ -78,7 +88,7 @@ function createWorkflowHandlers(db) {
|
|
|
78
88
|
async function createDefinition(input, context) {
|
|
79
89
|
const id = generateId("wfdef");
|
|
80
90
|
const now = new Date().toISOString();
|
|
81
|
-
await
|
|
91
|
+
await execute(`INSERT INTO workflow_definition (id, "projectId", "organizationId", name, description, type, status, "createdAt", "updatedAt")
|
|
82
92
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, [
|
|
83
93
|
id,
|
|
84
94
|
context.projectId,
|
|
@@ -90,15 +100,15 @@ function createWorkflowHandlers(db) {
|
|
|
90
100
|
now,
|
|
91
101
|
now
|
|
92
102
|
]);
|
|
93
|
-
const rows =
|
|
103
|
+
const rows = await queryRows(`SELECT * FROM workflow_definition WHERE id = ?`, [id]);
|
|
94
104
|
return rowToDefinition(rows[0]);
|
|
95
105
|
}
|
|
96
106
|
async function addStep(input) {
|
|
97
107
|
const id = generateId("wfstep");
|
|
98
108
|
const now = new Date().toISOString();
|
|
99
|
-
const maxOrderResult =
|
|
109
|
+
const maxOrderResult = await queryRows(`SELECT MAX("stepOrder") as maxOrder FROM workflow_step WHERE "definitionId" = ?`, [input.definitionId]);
|
|
100
110
|
const nextOrder = (maxOrderResult[0]?.maxOrder ?? 0) + 1;
|
|
101
|
-
await
|
|
111
|
+
await execute(`INSERT INTO workflow_step (id, "definitionId", name, description, "stepOrder", type, "requiredRoles", "autoApproveCondition", "timeoutHours", "createdAt")
|
|
102
112
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [
|
|
103
113
|
id,
|
|
104
114
|
input.definitionId,
|
|
@@ -111,11 +121,11 @@ function createWorkflowHandlers(db) {
|
|
|
111
121
|
input.timeoutHours ?? null,
|
|
112
122
|
now
|
|
113
123
|
]);
|
|
114
|
-
const rows =
|
|
124
|
+
const rows = await queryRows(`SELECT * FROM workflow_step WHERE id = ?`, [id]);
|
|
115
125
|
return rowToStep(rows[0]);
|
|
116
126
|
}
|
|
117
127
|
async function getSteps(definitionId) {
|
|
118
|
-
const rows =
|
|
128
|
+
const rows = await queryRows(`SELECT * FROM workflow_step WHERE "definitionId" = ? ORDER BY "stepOrder"`, [definitionId]);
|
|
119
129
|
return rows.map(rowToStep);
|
|
120
130
|
}
|
|
121
131
|
async function listInstances(input) {
|
|
@@ -127,10 +137,10 @@ function createWorkflowHandlers(db) {
|
|
|
127
137
|
limit = 20,
|
|
128
138
|
offset = 0
|
|
129
139
|
} = input;
|
|
130
|
-
let whereClause =
|
|
140
|
+
let whereClause = 'WHERE "projectId" = ?';
|
|
131
141
|
const params = [projectId];
|
|
132
142
|
if (definitionId) {
|
|
133
|
-
whereClause +=
|
|
143
|
+
whereClause += ' AND "definitionId" = ?';
|
|
134
144
|
params.push(definitionId);
|
|
135
145
|
}
|
|
136
146
|
if (status && status !== "all") {
|
|
@@ -138,12 +148,12 @@ function createWorkflowHandlers(db) {
|
|
|
138
148
|
params.push(status);
|
|
139
149
|
}
|
|
140
150
|
if (requestedBy) {
|
|
141
|
-
whereClause +=
|
|
151
|
+
whereClause += ' AND "requestedBy" = ?';
|
|
142
152
|
params.push(requestedBy);
|
|
143
153
|
}
|
|
144
|
-
const countResult =
|
|
154
|
+
const countResult = await queryRows(`SELECT COUNT(*) as count FROM workflow_instance ${whereClause}`, params);
|
|
145
155
|
const total = countResult[0]?.count ?? 0;
|
|
146
|
-
const rows =
|
|
156
|
+
const rows = await queryRows(`SELECT * FROM workflow_instance ${whereClause} ORDER BY "startedAt" DESC LIMIT ? OFFSET ?`, [...params, limit, offset]);
|
|
147
157
|
return {
|
|
148
158
|
instances: rows.map(rowToInstance),
|
|
149
159
|
total
|
|
@@ -152,9 +162,9 @@ function createWorkflowHandlers(db) {
|
|
|
152
162
|
async function startInstance(input, context) {
|
|
153
163
|
const id = generateId("wfinst");
|
|
154
164
|
const now = new Date().toISOString();
|
|
155
|
-
const steps =
|
|
165
|
+
const steps = await queryRows(`SELECT * FROM workflow_step WHERE "definitionId" = ? ORDER BY "stepOrder" LIMIT 1`, [input.definitionId]);
|
|
156
166
|
const firstStepId = steps[0]?.id ?? null;
|
|
157
|
-
await
|
|
167
|
+
await execute(`INSERT INTO workflow_instance (id, "projectId", "definitionId", status, "currentStepId", data, "requestedBy", "startedAt")
|
|
158
168
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, [
|
|
159
169
|
id,
|
|
160
170
|
context.projectId,
|
|
@@ -166,36 +176,32 @@ function createWorkflowHandlers(db) {
|
|
|
166
176
|
now
|
|
167
177
|
]);
|
|
168
178
|
if (firstStepId) {
|
|
169
|
-
await
|
|
179
|
+
await execute(`INSERT INTO workflow_approval (id, "instanceId", "stepId", status, "createdAt")
|
|
170
180
|
VALUES (?, ?, ?, ?, ?)`, [generateId("wfappr"), id, firstStepId, "PENDING", now]);
|
|
171
181
|
}
|
|
172
|
-
const rows =
|
|
182
|
+
const rows = await queryRows(`SELECT * FROM workflow_instance WHERE id = ?`, [id]);
|
|
173
183
|
return rowToInstance(rows[0]);
|
|
174
184
|
}
|
|
175
185
|
async function approveStep(input, context) {
|
|
176
186
|
const now = new Date().toISOString();
|
|
177
|
-
const instances =
|
|
178
|
-
input.instanceId
|
|
179
|
-
])).rows;
|
|
187
|
+
const instances = await queryRows(`SELECT * FROM workflow_instance WHERE id = ?`, [input.instanceId]);
|
|
180
188
|
if (!instances[0]) {
|
|
181
189
|
throw new Error("NOT_FOUND");
|
|
182
190
|
}
|
|
183
191
|
const instance = instances[0];
|
|
184
|
-
await
|
|
185
|
-
WHERE instanceId = ? AND stepId = ? AND status = 'PENDING'`, [
|
|
192
|
+
await execute(`UPDATE workflow_approval SET status = 'APPROVED', "actorId" = ?, comment = ?, "decidedAt" = ?
|
|
193
|
+
WHERE "instanceId" = ? AND "stepId" = ? AND status = 'PENDING'`, [
|
|
186
194
|
context.actorId,
|
|
187
195
|
input.comment ?? null,
|
|
188
196
|
now,
|
|
189
197
|
input.instanceId,
|
|
190
198
|
instance.currentStepId
|
|
191
199
|
]);
|
|
192
|
-
const currentStep =
|
|
193
|
-
|
|
194
|
-
])).rows;
|
|
195
|
-
const nextSteps = (await db.query(`SELECT * FROM workflow_step WHERE definitionId = ? AND stepOrder > ? ORDER BY stepOrder LIMIT 1`, [instance.definitionId, currentStep[0]?.stepOrder ?? 0])).rows;
|
|
200
|
+
const currentStep = await queryRows(`SELECT * FROM workflow_step WHERE id = ?`, [instance.currentStepId]);
|
|
201
|
+
const nextSteps = await queryRows(`SELECT * FROM workflow_step WHERE "definitionId" = ? AND "stepOrder" > ? ORDER BY "stepOrder" LIMIT 1`, [instance.definitionId, currentStep[0]?.stepOrder ?? 0]);
|
|
196
202
|
if (nextSteps[0]) {
|
|
197
|
-
await
|
|
198
|
-
await
|
|
203
|
+
await execute(`UPDATE workflow_instance SET "currentStepId" = ? WHERE id = ?`, [nextSteps[0].id, input.instanceId]);
|
|
204
|
+
await execute(`INSERT INTO workflow_approval (id, "instanceId", "stepId", status, "createdAt")
|
|
199
205
|
VALUES (?, ?, ?, ?, ?)`, [
|
|
200
206
|
generateId("wfappr"),
|
|
201
207
|
input.instanceId,
|
|
@@ -204,37 +210,31 @@ function createWorkflowHandlers(db) {
|
|
|
204
210
|
now
|
|
205
211
|
]);
|
|
206
212
|
} else {
|
|
207
|
-
await
|
|
213
|
+
await execute(`UPDATE workflow_instance SET status = 'COMPLETED', "currentStepId" = NULL, "completedAt" = ? WHERE id = ?`, [now, input.instanceId]);
|
|
208
214
|
}
|
|
209
|
-
const updated =
|
|
210
|
-
input.instanceId
|
|
211
|
-
])).rows;
|
|
215
|
+
const updated = await queryRows(`SELECT * FROM workflow_instance WHERE id = ?`, [input.instanceId]);
|
|
212
216
|
return rowToInstance(updated[0]);
|
|
213
217
|
}
|
|
214
218
|
async function rejectStep(input, context) {
|
|
215
219
|
const now = new Date().toISOString();
|
|
216
|
-
const instances =
|
|
217
|
-
input.instanceId
|
|
218
|
-
])).rows;
|
|
220
|
+
const instances = await queryRows(`SELECT * FROM workflow_instance WHERE id = ?`, [input.instanceId]);
|
|
219
221
|
if (!instances[0]) {
|
|
220
222
|
throw new Error("NOT_FOUND");
|
|
221
223
|
}
|
|
222
|
-
await
|
|
223
|
-
WHERE instanceId = ? AND stepId = ? AND status = 'PENDING'`, [
|
|
224
|
+
await execute(`UPDATE workflow_approval SET status = 'REJECTED', "actorId" = ?, comment = ?, "decidedAt" = ?
|
|
225
|
+
WHERE "instanceId" = ? AND "stepId" = ? AND status = 'PENDING'`, [
|
|
224
226
|
context.actorId,
|
|
225
227
|
input.reason,
|
|
226
228
|
now,
|
|
227
229
|
input.instanceId,
|
|
228
230
|
instances[0].currentStepId
|
|
229
231
|
]);
|
|
230
|
-
await
|
|
231
|
-
const updated =
|
|
232
|
-
input.instanceId
|
|
233
|
-
])).rows;
|
|
232
|
+
await execute(`UPDATE workflow_instance SET status = 'REJECTED', "completedAt" = ? WHERE id = ?`, [now, input.instanceId]);
|
|
233
|
+
const updated = await queryRows(`SELECT * FROM workflow_instance WHERE id = ?`, [input.instanceId]);
|
|
234
234
|
return rowToInstance(updated[0]);
|
|
235
235
|
}
|
|
236
236
|
async function getApprovals(instanceId) {
|
|
237
|
-
const rows =
|
|
237
|
+
const rows = await queryRows(`SELECT * FROM workflow_approval WHERE "instanceId" = ? ORDER BY "createdAt"`, [instanceId]);
|
|
238
238
|
return rows.map(rowToApproval);
|
|
239
239
|
}
|
|
240
240
|
return {
|