@valentine-efagene/qshelter-common 2.0.75 → 2.0.77

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 (38) hide show
  1. package/dist/generated/client/browser.d.ts +5 -5
  2. package/dist/generated/client/client.d.ts +5 -5
  3. package/dist/generated/client/enums.d.ts +36 -0
  4. package/dist/generated/client/enums.js +33 -0
  5. package/dist/generated/client/internal/class.d.ts +11 -11
  6. package/dist/generated/client/internal/class.js +2 -2
  7. package/dist/generated/client/internal/prismaNamespace.d.ts +95 -95
  8. package/dist/generated/client/internal/prismaNamespace.js +25 -25
  9. package/dist/generated/client/internal/prismaNamespaceBrowser.d.ts +27 -27
  10. package/dist/generated/client/internal/prismaNamespaceBrowser.js +25 -25
  11. package/dist/generated/client/models/Contract.d.ts +155 -155
  12. package/dist/generated/client/models/index.d.ts +3 -0
  13. package/dist/generated/client/models/index.js +3 -0
  14. package/dist/generated/client/models.d.ts +1 -1
  15. package/dist/src/events/bus/event-bus.service.d.ts +84 -0
  16. package/dist/src/events/bus/event-bus.service.js +372 -0
  17. package/dist/src/events/bus/event-bus.types.d.ts +73 -0
  18. package/dist/src/events/bus/event-bus.types.js +22 -0
  19. package/dist/src/events/index.d.ts +5 -6
  20. package/dist/src/events/index.js +7 -8
  21. package/dist/src/events/notifications/event-publisher.d.ts +41 -0
  22. package/dist/src/events/notifications/event-publisher.js +111 -0
  23. package/dist/src/events/notifications/notification-enums.d.ts +46 -0
  24. package/dist/src/events/notifications/notification-enums.js +59 -0
  25. package/dist/src/events/notifications/notification-event.d.ts +76 -0
  26. package/dist/src/events/notifications/notification-event.js +1 -0
  27. package/dist/src/events/unified/unified-event.service.d.ts +157 -0
  28. package/dist/src/events/unified/unified-event.service.js +177 -0
  29. package/dist/src/events/workflow/event-config.service.d.ts +123 -0
  30. package/dist/src/events/workflow/event-config.service.js +416 -0
  31. package/dist/src/events/workflow/event-seeder.d.ts +80 -0
  32. package/dist/src/events/workflow/event-seeder.js +343 -0
  33. package/dist/src/events/workflow/workflow-event.service.d.ts +230 -0
  34. package/dist/src/events/workflow/workflow-event.service.js +682 -0
  35. package/dist/src/events/workflow/workflow-types.d.ts +364 -0
  36. package/dist/src/events/workflow/workflow-types.js +22 -0
  37. package/package.json +4 -1
  38. package/prisma/schema.prisma +123 -79
@@ -0,0 +1,364 @@
1
+ /**
2
+ * Event-Driven Workflow Types
3
+ *
4
+ * These types define the structure for a configurable event system
5
+ * where admins can define event types, channels, and handlers.
6
+ *
7
+ * Architecture:
8
+ * 1. EventChannel - Logical grouping of events (e.g., "CONTRACTS", "PAYMENTS")
9
+ * 2. EventType - Specific event types (e.g., "DOCUMENT_UPLOADED", "STEP_COMPLETED")
10
+ * 3. EventHandler - What to do when an event fires (send email, call webhook, etc.)
11
+ * 4. WorkflowEvent - Actual event instances (audit log)
12
+ * 5. EventHandlerExecution - Log of handler executions
13
+ *
14
+ * Handler types are business-friendly so non-technical admins can configure them:
15
+ * - SEND_EMAIL: Send an email to someone
16
+ * - SEND_SMS: Send a text message
17
+ * - SEND_PUSH: Send a push notification
18
+ * - CALL_WEBHOOK: Call an external API
19
+ * - ADVANCE_WORKFLOW: Move workflow forward
20
+ * - RUN_AUTOMATION: Execute business logic
21
+ */
22
+ export type { EventHandlerType, ActorType, WorkflowEventStatus, ExecutionStatus, } from '../../../generated/client/enums';
23
+ /**
24
+ * Configuration for SEND_EMAIL handler type
25
+ * Sends an email notification to specified recipients
26
+ */
27
+ export interface SendEmailHandlerConfig {
28
+ type: 'SEND_EMAIL';
29
+ /** Email template name (e.g., "documentApproved", "paymentReminder") */
30
+ template: string;
31
+ /**
32
+ * Notification type for the notification service.
33
+ * This maps to templates in the notification service.
34
+ */
35
+ notificationType: string;
36
+ /**
37
+ * Who to send the email to. Use JSONPath to extract from event payload.
38
+ * Examples: "$.buyer.email", "$.user.email"
39
+ */
40
+ recipientPath: string;
41
+ /**
42
+ * Map event payload fields to template variables
43
+ * Example: { "userName": "$.buyer.firstName", "amount": "$.payment.amount" }
44
+ */
45
+ templateData?: Record<string, string>;
46
+ /** Static data to always include in template */
47
+ staticData?: Record<string, unknown>;
48
+ /** Priority level */
49
+ priority?: 'low' | 'normal' | 'high' | 'urgent';
50
+ }
51
+ /**
52
+ * Configuration for SEND_SMS handler type
53
+ * Sends an SMS text message
54
+ */
55
+ export interface SendSmsHandlerConfig {
56
+ type: 'SEND_SMS';
57
+ /** SMS template name */
58
+ template: string;
59
+ /**
60
+ * Notification type for the notification service.
61
+ */
62
+ notificationType: string;
63
+ /**
64
+ * Phone number path in event payload
65
+ * Example: "$.buyer.phone"
66
+ */
67
+ recipientPath: string;
68
+ /** Map event payload fields to template variables */
69
+ templateData?: Record<string, string>;
70
+ /** Static data to always include in template */
71
+ staticData?: Record<string, unknown>;
72
+ }
73
+ /**
74
+ * Configuration for SEND_PUSH handler type
75
+ * Sends a push notification to user's device
76
+ */
77
+ export interface SendPushHandlerConfig {
78
+ type: 'SEND_PUSH';
79
+ /** Push notification title */
80
+ title: string;
81
+ /** Push notification body (can use {{variables}}) */
82
+ body: string;
83
+ /**
84
+ * Notification type for the notification service.
85
+ */
86
+ notificationType: string;
87
+ /**
88
+ * User ID path in event payload (to find their device)
89
+ * Example: "$.buyer.id"
90
+ */
91
+ recipientPath: string;
92
+ /** Deep link to open in app */
93
+ deepLink?: string;
94
+ /** Map event payload fields to notification variables */
95
+ templateData?: Record<string, string>;
96
+ /** Static data to always include in notification */
97
+ staticData?: Record<string, unknown>;
98
+ }
99
+ /**
100
+ * Configuration for CALL_WEBHOOK handler type
101
+ * Calls an external API endpoint
102
+ */
103
+ export interface CallWebhookHandlerConfig {
104
+ type: 'CALL_WEBHOOK';
105
+ /** The URL to call */
106
+ url: string;
107
+ /** HTTP method */
108
+ method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
109
+ /** Optional headers to include */
110
+ headers?: Record<string, string>;
111
+ /**
112
+ * Map event payload fields to request body
113
+ * Example: { "orderId": "$.contract.id", "status": "$.status" }
114
+ */
115
+ bodyMapping?: Record<string, string>;
116
+ /** Timeout in milliseconds (default: 30000) */
117
+ timeoutMs?: number;
118
+ }
119
+ /**
120
+ * Configuration for ADVANCE_WORKFLOW handler type
121
+ * Advances or modifies a workflow step
122
+ */
123
+ export interface AdvanceWorkflowHandlerConfig {
124
+ type: 'ADVANCE_WORKFLOW';
125
+ /** What action to take */
126
+ action: 'complete_step' | 'skip_step' | 'fail_step' | 'activate_phase';
127
+ /**
128
+ * Step ID path in event payload (if action targets a specific step)
129
+ * Example: "$.stepId"
130
+ */
131
+ stepIdPath?: string;
132
+ /**
133
+ * Phase ID path in event payload (if action targets a phase)
134
+ * Example: "$.phaseId"
135
+ */
136
+ phaseIdPath?: string;
137
+ /** Static step ID (if not using path) */
138
+ stepId?: string;
139
+ /** Static workflow ID */
140
+ workflowId?: string;
141
+ /** Static phase ID */
142
+ phaseId?: string;
143
+ /** Additional data to pass to the action */
144
+ data?: Record<string, unknown>;
145
+ /** Reason to record for the action */
146
+ reason?: string;
147
+ }
148
+ /**
149
+ * Configuration for RUN_AUTOMATION handler type
150
+ * Executes internal business logic
151
+ */
152
+ export interface RunAutomationHandlerConfig {
153
+ type: 'RUN_AUTOMATION';
154
+ /** The automation to run (registered automation name) */
155
+ automation: string;
156
+ /**
157
+ * Map event payload fields to automation inputs
158
+ * Example: { "contractId": "$.contract.id", "amount": "$.payment.amount" }
159
+ */
160
+ inputMapping?: Record<string, string>;
161
+ }
162
+ /**
163
+ * Union type for all handler configurations
164
+ */
165
+ export type HandlerConfig = SendEmailHandlerConfig | SendSmsHandlerConfig | SendPushHandlerConfig | CallWebhookHandlerConfig | AdvanceWorkflowHandlerConfig | RunAutomationHandlerConfig;
166
+ /**
167
+ * Input for emitting an event
168
+ */
169
+ export interface EmitEventInput {
170
+ /** Event type code (e.g., "DOCUMENT_UPLOADED") */
171
+ eventType: string;
172
+ /** Event payload */
173
+ payload: Record<string, unknown>;
174
+ /** Source of the event (service name) */
175
+ source: string;
176
+ /** Actor information */
177
+ actor?: {
178
+ id: string;
179
+ type: 'USER' | 'API_KEY' | 'SYSTEM' | 'WEBHOOK';
180
+ };
181
+ /** Correlation ID for tracing related events */
182
+ correlationId?: string;
183
+ /** Causation ID (which event caused this one) */
184
+ causationId?: string;
185
+ }
186
+ /**
187
+ * Event with full metadata (returned from queries)
188
+ */
189
+ export interface WorkflowEventData {
190
+ id: string;
191
+ tenantId: string;
192
+ eventTypeId: string;
193
+ eventTypeCode: string;
194
+ channelCode: string;
195
+ payload: Record<string, unknown>;
196
+ source: string;
197
+ actorId: string | null;
198
+ actorType: 'USER' | 'API_KEY' | 'SYSTEM' | 'WEBHOOK';
199
+ status: 'PENDING' | 'PROCESSING' | 'COMPLETED' | 'FAILED' | 'SKIPPED';
200
+ correlationId: string | null;
201
+ causationId: string | null;
202
+ error: string | null;
203
+ processedAt: Date | null;
204
+ createdAt: Date;
205
+ }
206
+ /**
207
+ * Result of processing an event
208
+ */
209
+ export interface ProcessEventResult {
210
+ eventId: string;
211
+ status: 'PENDING' | 'PROCESSING' | 'COMPLETED' | 'FAILED' | 'SKIPPED';
212
+ handlersExecuted: number;
213
+ handlersSucceeded: number;
214
+ handlersFailed: number;
215
+ errors: Array<{
216
+ handlerId: string;
217
+ handlerName: string;
218
+ error: string;
219
+ }>;
220
+ }
221
+ /**
222
+ * Input for creating an event channel
223
+ */
224
+ export interface CreateEventChannelInput {
225
+ code: string;
226
+ name: string;
227
+ description?: string;
228
+ enabled?: boolean;
229
+ }
230
+ /**
231
+ * Input for updating an event channel
232
+ */
233
+ export interface UpdateEventChannelInput {
234
+ name?: string;
235
+ description?: string;
236
+ enabled?: boolean;
237
+ }
238
+ /**
239
+ * Input for creating an event type
240
+ */
241
+ export interface CreateEventTypeInput {
242
+ channelId: string;
243
+ code: string;
244
+ name: string;
245
+ description?: string;
246
+ payloadSchema?: Record<string, unknown>;
247
+ enabled?: boolean;
248
+ }
249
+ /**
250
+ * Input for updating an event type
251
+ */
252
+ export interface UpdateEventTypeInput {
253
+ name?: string;
254
+ description?: string;
255
+ payloadSchema?: Record<string, unknown>;
256
+ enabled?: boolean;
257
+ }
258
+ /**
259
+ * Input for creating an event handler
260
+ *
261
+ * Handler types are business-friendly names:
262
+ * - SEND_EMAIL: Send email notification
263
+ * - SEND_SMS: Send SMS text message
264
+ * - SEND_PUSH: Send push notification
265
+ * - CALL_WEBHOOK: Call external API
266
+ * - ADVANCE_WORKFLOW: Move workflow forward
267
+ * - RUN_AUTOMATION: Execute business logic
268
+ */
269
+ export interface CreateEventHandlerInput {
270
+ eventTypeId: string;
271
+ name: string;
272
+ description?: string;
273
+ handlerType: 'SEND_EMAIL' | 'SEND_SMS' | 'SEND_PUSH' | 'CALL_WEBHOOK' | 'ADVANCE_WORKFLOW' | 'RUN_AUTOMATION';
274
+ config: HandlerConfig;
275
+ priority?: number;
276
+ enabled?: boolean;
277
+ maxRetries?: number;
278
+ retryDelayMs?: number;
279
+ filterCondition?: string;
280
+ }
281
+ /**
282
+ * Input for updating an event handler
283
+ */
284
+ export interface UpdateEventHandlerInput {
285
+ name?: string;
286
+ description?: string;
287
+ config?: HandlerConfig;
288
+ priority?: number;
289
+ enabled?: boolean;
290
+ maxRetries?: number;
291
+ retryDelayMs?: number;
292
+ filterCondition?: string;
293
+ }
294
+ /**
295
+ * Event channel with related data
296
+ */
297
+ export interface EventChannelWithTypes {
298
+ id: string;
299
+ tenantId: string;
300
+ code: string;
301
+ name: string;
302
+ description: string | null;
303
+ enabled: boolean;
304
+ eventTypes: Array<{
305
+ id: string;
306
+ code: string;
307
+ name: string;
308
+ enabled: boolean;
309
+ }>;
310
+ createdAt: Date;
311
+ updatedAt: Date;
312
+ }
313
+ /**
314
+ * Event type with related data
315
+ */
316
+ export interface EventTypeWithHandlers {
317
+ id: string;
318
+ tenantId: string;
319
+ channelId: string;
320
+ channel: {
321
+ code: string;
322
+ name: string;
323
+ };
324
+ code: string;
325
+ name: string;
326
+ description: string | null;
327
+ payloadSchema: Record<string, unknown> | null;
328
+ enabled: boolean;
329
+ handlers: Array<{
330
+ id: string;
331
+ name: string;
332
+ handlerType: string;
333
+ enabled: boolean;
334
+ }>;
335
+ createdAt: Date;
336
+ updatedAt: Date;
337
+ }
338
+ /**
339
+ * Event handler with related data
340
+ */
341
+ export interface EventHandlerWithType {
342
+ id: string;
343
+ tenantId: string;
344
+ eventTypeId: string;
345
+ eventType: {
346
+ code: string;
347
+ name: string;
348
+ channel: {
349
+ code: string;
350
+ name: string;
351
+ };
352
+ };
353
+ name: string;
354
+ description: string | null;
355
+ handlerType: string;
356
+ config: HandlerConfig;
357
+ priority: number;
358
+ enabled: boolean;
359
+ maxRetries: number;
360
+ retryDelayMs: number;
361
+ filterCondition: string | null;
362
+ createdAt: Date;
363
+ updatedAt: Date;
364
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Event-Driven Workflow Types
3
+ *
4
+ * These types define the structure for a configurable event system
5
+ * where admins can define event types, channels, and handlers.
6
+ *
7
+ * Architecture:
8
+ * 1. EventChannel - Logical grouping of events (e.g., "CONTRACTS", "PAYMENTS")
9
+ * 2. EventType - Specific event types (e.g., "DOCUMENT_UPLOADED", "STEP_COMPLETED")
10
+ * 3. EventHandler - What to do when an event fires (send email, call webhook, etc.)
11
+ * 4. WorkflowEvent - Actual event instances (audit log)
12
+ * 5. EventHandlerExecution - Log of handler executions
13
+ *
14
+ * Handler types are business-friendly so non-technical admins can configure them:
15
+ * - SEND_EMAIL: Send an email to someone
16
+ * - SEND_SMS: Send a text message
17
+ * - SEND_PUSH: Send a push notification
18
+ * - CALL_WEBHOOK: Call an external API
19
+ * - ADVANCE_WORKFLOW: Move workflow forward
20
+ * - RUN_AUTOMATION: Execute business logic
21
+ */
22
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@valentine-efagene/qshelter-common",
3
- "version": "2.0.75",
3
+ "version": "2.0.77",
4
4
  "description": "Shared database schemas and utilities for QShelter services",
5
5
  "main": "dist/src/index.js",
6
6
  "types": "dist/src/index.d.ts",
@@ -29,11 +29,14 @@
29
29
  ],
30
30
  "dependencies": {
31
31
  "@aws-sdk/client-dynamodb": "^3.962.0",
32
+ "@aws-sdk/client-eventbridge": "^3.700.0",
32
33
  "@aws-sdk/client-secrets-manager": "^3.500.0",
33
34
  "@aws-sdk/client-sns": "^3.500.0",
35
+ "@aws-sdk/client-sqs": "^3.700.0",
34
36
  "@aws-sdk/client-ssm": "^3.500.0",
35
37
  "@aws-sdk/util-dynamodb": "^3.962.0",
36
38
  "@prisma/client": "^7.0.0",
39
+ "axios": "^1.13.2",
37
40
  "dotenv": "^17.2.3",
38
41
  "prisma": "^7.0.0"
39
42
  },