@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.
Files changed (57) hide show
  1. package/README.md +72 -119
  2. package/dist/approval/approval.event.js +1 -1
  3. package/dist/approval/approval.handler.d.ts +1 -1
  4. package/dist/approval/index.d.ts +4 -4
  5. package/dist/approval/index.js +1 -1
  6. package/dist/browser/approval/approval.event.js +1 -1
  7. package/dist/browser/approval/index.js +1 -1
  8. package/dist/browser/entities/index.js +166 -166
  9. package/dist/browser/handlers/index.js +43 -43
  10. package/dist/browser/handlers/workflow.handlers.js +43 -43
  11. package/dist/browser/index.js +1935 -1548
  12. package/dist/browser/instance/index.js +210 -210
  13. package/dist/browser/instance/instance.event.js +1 -1
  14. package/dist/browser/shared/demo-scenario.js +213 -0
  15. package/dist/browser/ui/WorkflowDashboard.visualizations.js +239 -0
  16. package/dist/browser/ui/hooks/index.js +0 -47
  17. package/dist/browser/ui/hooks/useWorkflowList.js +6 -4
  18. package/dist/browser/ui/index.js +6 -4
  19. package/dist/browser/ui/renderers/index.js +409 -73
  20. package/dist/browser/ui/renderers/workflow.markdown.js +409 -73
  21. package/dist/browser/visualizations/catalog.js +132 -0
  22. package/dist/browser/visualizations/index.js +133 -0
  23. package/dist/browser/visualizations/selectors.js +195 -0
  24. package/dist/entities/index.d.ts +53 -53
  25. package/dist/entities/index.js +166 -166
  26. package/dist/example.test.d.ts +1 -0
  27. package/dist/handlers/index.js +43 -43
  28. package/dist/handlers/workflow.handlers.js +43 -43
  29. package/dist/index.d.ts +6 -5
  30. package/dist/index.js +1935 -1548
  31. package/dist/instance/index.d.ts +3 -3
  32. package/dist/instance/index.js +210 -210
  33. package/dist/instance/instance.event.js +1 -1
  34. package/dist/instance/instance.handler.d.ts +1 -1
  35. package/dist/shared/demo-scenario.d.ts +43 -0
  36. package/dist/shared/demo-scenario.js +214 -0
  37. package/dist/shared/index.d.ts +1 -1
  38. package/dist/shared/mock-data.d.ts +1 -1
  39. package/dist/ui/WorkflowDashboard.visualizations.d.ts +4 -0
  40. package/dist/ui/WorkflowDashboard.visualizations.js +240 -0
  41. package/dist/ui/hooks/index.js +0 -47
  42. package/dist/ui/hooks/useWorkflowList.d.ts +2 -1
  43. package/dist/ui/hooks/useWorkflowList.js +6 -4
  44. package/dist/ui/index.d.ts +1 -1
  45. package/dist/ui/index.js +6 -4
  46. package/dist/ui/renderers/index.js +409 -73
  47. package/dist/ui/renderers/workflow.markdown.js +409 -73
  48. package/dist/visualizations/catalog.d.ts +11 -0
  49. package/dist/visualizations/catalog.js +133 -0
  50. package/dist/visualizations/index.d.ts +2 -0
  51. package/dist/visualizations/index.js +134 -0
  52. package/dist/visualizations/selectors.d.ts +11 -0
  53. package/dist/visualizations/selectors.js +196 -0
  54. package/dist/visualizations/selectors.test.d.ts +1 -0
  55. package/dist/workflow/index.d.ts +4 -4
  56. package/dist/workflow/workflow.handler.d.ts +1 -1
  57. package/package.json +71 -10
@@ -113,82 +113,160 @@ var ApprovalCommentEntity = defineEntity({
113
113
  indexes: [index.on(["approvalRequestId", "createdAt"])]
114
114
  });
115
115
 
116
- // src/entities/workflow.ts
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 WorkflowStatusEnum = defineEntityEnum2({
124
- name: "WorkflowStatus",
125
- values: ["DRAFT", "ACTIVE", "DEPRECATED", "ARCHIVED"],
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 definition."
136
+ description: "Status of a workflow instance."
128
137
  });
129
- var WorkflowTriggerTypeEnum = defineEntityEnum2({
130
- name: "WorkflowTriggerType",
131
- values: ["MANUAL", "EVENT", "SCHEDULED", "API"],
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: "What triggers workflow instantiation."
149
+ description: "Status of a step execution."
134
150
  });
135
- var WorkflowDefinitionEntity = defineEntity2({
136
- name: "WorkflowDefinition",
137
- description: "A workflow blueprint that defines the process structure.",
151
+ var WorkflowInstanceEntity = defineEntity2({
152
+ name: "WorkflowInstance",
153
+ description: "A running instance of a workflow definition.",
138
154
  schema: "workflow",
139
- map: "workflow_definition",
155
+ map: "workflow_instance",
140
156
  fields: {
141
- id: field2.id({ description: "Unique workflow definition ID" }),
142
- name: field2.string({ description: "Human-readable workflow name" }),
143
- key: field2.string({
144
- description: 'Unique key for referencing (e.g., "purchase_approval")'
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
- description: field2.string({
163
+ referenceType: field2.string({
147
164
  isOptional: true,
148
- description: "Detailed description"
165
+ description: 'Type of reference (e.g., "Order")'
149
166
  }),
150
- version: field2.int({
151
- default: 1,
152
- description: "Version number for versioning definitions"
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
- status: field2.enum("WorkflowStatus", { default: "DRAFT" }),
155
- triggerType: field2.enum("WorkflowTriggerType", { default: "MANUAL" }),
156
- triggerConfig: field2.json({
176
+ triggerSource: field2.string({
157
177
  isOptional: true,
158
- description: "Trigger-specific configuration"
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
- initialStepId: field2.string({
185
+ dueAt: field2.dateTime({
161
186
  isOptional: true,
162
- description: "First step when workflow starts"
187
+ description: "When this instance should complete"
163
188
  }),
164
- featureFlagKey: field2.string({
189
+ outcome: field2.string({
165
190
  isOptional: true,
166
- description: "Feature flag to control availability"
191
+ description: 'Final outcome (e.g., "approved", "rejected")'
167
192
  }),
168
- settings: field2.json({
193
+ resultData: field2.json({
169
194
  isOptional: true,
170
- description: "Workflow-wide settings"
195
+ description: "Final result data"
171
196
  }),
172
- metadata: field2.json({ isOptional: true, description: "Custom metadata" }),
173
- organizationId: field2.foreignKey({ description: "Owning organization" }),
174
- createdBy: field2.foreignKey({
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
- publishedAt: field2.dateTime({
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: "When workflow was activated"
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
- steps: field2.hasMany("WorkflowStep"),
184
- instances: field2.hasMany("WorkflowInstance")
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.unique(["organizationId", "key", "version"]),
188
- index2.on(["organizationId", "status"]),
189
- index2.on(["key", "version"])
265
+ index2.on(["workflowInstanceId", "executionOrder"]),
266
+ index2.on(["workflowInstanceId", "workflowStepId"]),
267
+ index2.on(["status"])
190
268
  ],
191
- enums: [WorkflowStatusEnum, WorkflowTriggerTypeEnum]
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/instance.ts
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 InstanceStatusEnum = defineEntityEnum4({
322
- name: "InstanceStatus",
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 instance."
403
+ description: "Status of a workflow definition."
335
404
  });
336
- var StepExecutionStatusEnum = defineEntityEnum4({
337
- name: "StepExecutionStatus",
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: "Status of a step execution."
409
+ description: "What triggers workflow instantiation."
348
410
  });
349
- var WorkflowInstanceEntity = defineEntity4({
350
- name: "WorkflowInstance",
351
- description: "A running instance of a workflow definition.",
411
+ var WorkflowDefinitionEntity = defineEntity4({
412
+ name: "WorkflowDefinition",
413
+ description: "A workflow blueprint that defines the process structure.",
352
414
  schema: "workflow",
353
- map: "workflow_instance",
415
+ map: "workflow_definition",
354
416
  fields: {
355
- id: field4.id({ description: "Unique instance ID" }),
356
- workflowDefinitionId: field4.foreignKey(),
357
- referenceId: field4.string({
358
- isOptional: true,
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
- status: field4.enum("InstanceStatus", { default: "PENDING" }),
366
- currentStepId: field4.string({
422
+ description: field4.string({
367
423
  isOptional: true,
368
- description: "Current step being executed"
424
+ description: "Detailed description"
369
425
  }),
370
- contextData: field4.json({ description: "Data context for this instance" }),
371
- triggeredBy: field4.foreignKey({
372
- description: "User who triggered the workflow"
426
+ version: field4.int({
427
+ default: 1,
428
+ description: "Version number for versioning definitions"
373
429
  }),
374
- triggerSource: field4.string({
430
+ status: field4.enum("WorkflowStatus", { default: "DRAFT" }),
431
+ triggerType: field4.enum("WorkflowTriggerType", { default: "MANUAL" }),
432
+ triggerConfig: field4.json({
375
433
  isOptional: true,
376
- description: 'Source of trigger (e.g., "api", "ui")'
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
- dueAt: field4.dateTime({
436
+ initialStepId: field4.string({
384
437
  isOptional: true,
385
- description: "When this instance should complete"
438
+ description: "First step when workflow starts"
386
439
  }),
387
- outcome: field4.string({
440
+ featureFlagKey: field4.string({
388
441
  isOptional: true,
389
- description: 'Final outcome (e.g., "approved", "rejected")'
442
+ description: "Feature flag to control availability"
390
443
  }),
391
- resultData: field4.json({
444
+ settings: field4.json({
392
445
  isOptional: true,
393
- description: "Final result data"
446
+ description: "Workflow-wide settings"
394
447
  }),
395
- errorMessage: field4.string({
396
- isOptional: true,
397
- description: "Error message if failed"
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
- startedAt: field4.dateTime({ isOptional: true }),
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: 'Action that caused transition (e.g., "approve")'
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
- errorMessage: field4.string({ isOptional: true }),
453
- errorDetails: field4.json({ isOptional: true }),
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.on(["workflowInstanceId", "executionOrder"]),
464
- index4.on(["workflowInstanceId", "workflowStepId"]),
465
- index4.on(["status"])
463
+ index4.unique(["organizationId", "key", "version"]),
464
+ index4.on(["organizationId", "status"]),
465
+ index4.on(["key", "version"])
466
466
  ],
467
- enums: [StepExecutionStatusEnum]
467
+ enums: [WorkflowStatusEnum, WorkflowTriggerTypeEnum]
468
468
  });
469
469
  // src/entities/index.ts
470
470
  var workflowSystemEntities = [
@@ -0,0 +1 @@
1
+ export {};
@@ -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 = "WHERE projectId = ?";
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 = (await db.query(`SELECT COUNT(*) as count FROM workflow_definition ${whereClause}`, params)).rows;
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 = (await db.query(`SELECT * FROM workflow_definition ${whereClause} ORDER BY updatedAt DESC LIMIT ? OFFSET ?`, [...params, limit, offset])).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 db.execute(`INSERT INTO workflow_definition (id, projectId, organizationId, name, description, type, status, createdAt, updatedAt)
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 = (await db.query(`SELECT * FROM workflow_definition WHERE id = ?`, [id])).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 = (await db.query(`SELECT MAX(stepOrder) as maxOrder FROM workflow_step WHERE definitionId = ?`, [input.definitionId])).rows;
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 db.execute(`INSERT INTO workflow_step (id, definitionId, name, description, stepOrder, type, requiredRoles, autoApproveCondition, timeoutHours, createdAt)
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 = (await db.query(`SELECT * FROM workflow_step WHERE id = ?`, [id])).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 = (await db.query(`SELECT * FROM workflow_step WHERE definitionId = ? ORDER BY stepOrder`, [definitionId])).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 = "WHERE projectId = ?";
140
+ let whereClause = 'WHERE "projectId" = ?';
131
141
  const params = [projectId];
132
142
  if (definitionId) {
133
- whereClause += " AND definitionId = ?";
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 += " AND requestedBy = ?";
151
+ whereClause += ' AND "requestedBy" = ?';
142
152
  params.push(requestedBy);
143
153
  }
144
- const countResult = (await db.query(`SELECT COUNT(*) as count FROM workflow_instance ${whereClause}`, params)).rows;
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 = (await db.query(`SELECT * FROM workflow_instance ${whereClause} ORDER BY startedAt DESC LIMIT ? OFFSET ?`, [...params, limit, offset])).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 = (await db.query(`SELECT * FROM workflow_step WHERE definitionId = ? ORDER BY stepOrder LIMIT 1`, [input.definitionId])).rows;
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 db.execute(`INSERT INTO workflow_instance (id, projectId, definitionId, status, currentStepId, data, requestedBy, startedAt)
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 db.execute(`INSERT INTO workflow_approval (id, instanceId, stepId, status, createdAt)
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 = (await db.query(`SELECT * FROM workflow_instance WHERE id = ?`, [id])).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 = (await db.query(`SELECT * FROM workflow_instance WHERE id = ?`, [
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 db.execute(`UPDATE workflow_approval SET status = 'APPROVED', actorId = ?, comment = ?, decidedAt = ?
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 = (await db.query(`SELECT * FROM workflow_step WHERE id = ?`, [
193
- instance.currentStepId
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 db.execute(`UPDATE workflow_instance SET currentStepId = ? WHERE id = ?`, [nextSteps[0].id, input.instanceId]);
198
- await db.execute(`INSERT INTO workflow_approval (id, instanceId, stepId, status, createdAt)
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 db.execute(`UPDATE workflow_instance SET status = 'COMPLETED', currentStepId = NULL, completedAt = ? WHERE id = ?`, [now, input.instanceId]);
213
+ await execute(`UPDATE workflow_instance SET status = 'COMPLETED', "currentStepId" = NULL, "completedAt" = ? WHERE id = ?`, [now, input.instanceId]);
208
214
  }
209
- const updated = (await db.query(`SELECT * FROM workflow_instance WHERE id = ?`, [
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 = (await db.query(`SELECT * FROM workflow_instance WHERE id = ?`, [
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 db.execute(`UPDATE workflow_approval SET status = 'REJECTED', actorId = ?, comment = ?, decidedAt = ?
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 db.execute(`UPDATE workflow_instance SET status = 'REJECTED', completedAt = ? WHERE id = ?`, [now, input.instanceId]);
231
- const updated = (await db.query(`SELECT * FROM workflow_instance WHERE id = ?`, [
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 = (await db.query(`SELECT * FROM workflow_approval WHERE instanceId = ? ORDER BY createdAt`, [instanceId])).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 {