@geniehr/utilities 1.0.13 → 1.0.14

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.
@@ -0,0 +1,11 @@
1
+ export declare class ConductorClient {
2
+ private client;
3
+ private workers;
4
+ constructor(serviceName?: string);
5
+ startWorkflow(name: string, version: number, input: any, correlationId?: string): Promise<any>;
6
+ registerWorker(taskType: string, execute: (input: any) => Promise<any>, pollInterval?: number, domain?: string): Promise<void>;
7
+ private poll;
8
+ updateTask(taskId: string, workflowInstanceId: string, status: string, outputData: any, reasonForIncompletion?: string): Promise<void>;
9
+ getWorkflowByCorrelationId(name: string, correlationId: string): Promise<any>;
10
+ getTask(workflowInstanceId: string, taskReferenceName: string): Promise<any>;
11
+ }
@@ -0,0 +1,126 @@
1
+ import axios from 'axios';
2
+ const WORKFLOW_API_URL = process.env.CONDUCTOR_SERVER_URL || 'http://localhost:8080/api';
3
+ export class ConductorClient {
4
+ client;
5
+ workers = {};
6
+ constructor(serviceName = 'claims-service') {
7
+ this.client = axios.create({
8
+ baseURL: WORKFLOW_API_URL,
9
+ headers: {
10
+ 'Content-Type': 'application/json',
11
+ }
12
+ });
13
+ console.log(`ConductorClient initialized for ${serviceName}`);
14
+ }
15
+ async startWorkflow(name, version, input, correlationId) {
16
+ try {
17
+ const response = await this.client.post('/workflow', {
18
+ name,
19
+ version,
20
+ input,
21
+ correlationId
22
+ });
23
+ console.log(`Started workflow ${name} v${version}: ${JSON.stringify(response.data)}`);
24
+ return response.data;
25
+ }
26
+ catch (error) {
27
+ console.error(`Error starting workflow ${name}:`, error.message);
28
+ throw error;
29
+ }
30
+ }
31
+ async registerWorker(taskType, execute, pollInterval = 1000, domain) {
32
+ if (this.workers[taskType]) {
33
+ console.warn(`Worker for ${taskType} already registered.`);
34
+ return;
35
+ }
36
+ this.workers[taskType] = true;
37
+ console.log(`Registered worker for ${taskType}`);
38
+ this.poll(taskType, execute, pollInterval, domain);
39
+ }
40
+ async poll(taskType, execute, interval, domain) {
41
+ while (this.workers[taskType]) {
42
+ try {
43
+ // Poll for a task
44
+ // GET /tasks/poll/{taskType}?workerid=...&domain=...
45
+ const workerId = `worker-${process.pid}`;
46
+ const response = await this.client.get(`/tasks/poll/${taskType}`, {
47
+ params: {
48
+ workerid: workerId,
49
+ domain
50
+ }
51
+ });
52
+ const task = response.data;
53
+ if (task && task.taskId) {
54
+ console.log(`Received task ${taskType} (ID: ${task.taskId})`);
55
+ let status = 'COMPLETED';
56
+ let outputData = {};
57
+ let reasonForIncompletion = '';
58
+ try {
59
+ outputData = await execute(task.inputData);
60
+ }
61
+ catch (err) {
62
+ console.error(`Error executing task ${taskType}:`, err.message);
63
+ status = 'FAILED';
64
+ reasonForIncompletion = err.message || 'Unknown error';
65
+ }
66
+ // Update task status
67
+ await this.updateTask(task.taskId, task.workflowInstanceId, status, outputData, reasonForIncompletion);
68
+ }
69
+ }
70
+ catch (error) {
71
+ // If polling fails (e.g. 204 No Content or connection error), just log debug/warn and wait
72
+ // console.debug(`Polling error for ${taskType}: ${error}`);
73
+ }
74
+ // Wait before next poll
75
+ await new Promise(resolve => setTimeout(resolve, interval));
76
+ }
77
+ }
78
+ async updateTask(taskId, workflowInstanceId, status, outputData, reasonForIncompletion) {
79
+ try {
80
+ await this.client.post('/tasks', {
81
+ taskId,
82
+ workflowInstanceId,
83
+ status,
84
+ outputData,
85
+ reasonForIncompletion
86
+ });
87
+ console.log(`Updated task ${taskId} to ${status}`);
88
+ }
89
+ catch (error) {
90
+ console.error(`Failed to update task ${taskId}:`, error.message);
91
+ }
92
+ }
93
+ async getWorkflowByCorrelationId(name, correlationId) {
94
+ try {
95
+ // Search for running workflows with the given correlationId
96
+ const response = await this.client.get('/workflow/search', {
97
+ params: {
98
+ query: `correlationId='${correlationId}' AND status='RUNNING'`
99
+ }
100
+ });
101
+ if (response.data.results && response.data.results.length > 0) {
102
+ // Return the first match (assuming one active workflow per claim)
103
+ return response.data.results[0];
104
+ }
105
+ return null;
106
+ }
107
+ catch (error) {
108
+ console.error(`Error searching workflow:`, error.message);
109
+ return null;
110
+ }
111
+ }
112
+ async getTask(workflowInstanceId, taskReferenceName) {
113
+ try {
114
+ const response = await this.client.get(`/workflow/${workflowInstanceId}`);
115
+ const workflow = response.data;
116
+ if (workflow && workflow.tasks) {
117
+ return workflow.tasks.find((t) => t.referenceTaskName === taskReferenceName && t.status === 'IN_PROGRESS');
118
+ }
119
+ return null;
120
+ }
121
+ catch (error) {
122
+ console.error(`Error getting workflow tasks:`, error.message);
123
+ return null;
124
+ }
125
+ }
126
+ }
package/dist/index.d.ts CHANGED
@@ -10,3 +10,4 @@ export { HttpClient, GraphQLClient } from './apiUtils/httpUtils.js';
10
10
  export { sendResponse } from './apiUtils/api.js';
11
11
  export { services } from './shared/services.js';
12
12
  export { ImageCompressor } from './shared/helper/ImageCompressor.js';
13
+ export { ConductorClient } from './conductor/ConductorClient.js';
package/dist/index.js CHANGED
@@ -10,3 +10,4 @@ export { HttpClient, GraphQLClient } from './apiUtils/httpUtils.js';
10
10
  export { sendResponse } from './apiUtils/api.js';
11
11
  export { services } from './shared/services.js';
12
12
  export { ImageCompressor } from './shared/helper/ImageCompressor.js';
13
+ export { ConductorClient } from './conductor/ConductorClient.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geniehr/utilities",
3
- "version": "1.0.13",
3
+ "version": "1.0.14",
4
4
  "description": "",
5
5
  "homepage": "https://github.com/Genie-HR/ghr-utilities#readme",
6
6
  "bugs": {