rez_core 2.2.253 → 2.2.255

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 (36) hide show
  1. package/dist/module/communication/communication.module.js +2 -0
  2. package/dist/module/communication/communication.module.js.map +1 -1
  3. package/dist/module/communication/factories/telephone.factory.d.ts +3 -1
  4. package/dist/module/communication/factories/telephone.factory.js +9 -3
  5. package/dist/module/communication/factories/telephone.factory.js.map +1 -1
  6. package/dist/module/communication/strategies/telephone/tubelight-voice.strategy.d.ts +15 -0
  7. package/dist/module/communication/strategies/telephone/tubelight-voice.strategy.js +104 -0
  8. package/dist/module/communication/strategies/telephone/tubelight-voice.strategy.js.map +1 -0
  9. package/dist/module/meta/controller/entity.controller.js +11 -6
  10. package/dist/module/meta/controller/entity.controller.js.map +1 -1
  11. package/dist/module/user/service/user.service.d.ts +2 -1
  12. package/dist/module/user/service/user.service.js +2 -0
  13. package/dist/module/user/service/user.service.js.map +1 -1
  14. package/dist/module/workflow/entity/action-data.entity.d.ts +1 -0
  15. package/dist/module/workflow/entity/action-data.entity.js +4 -0
  16. package/dist/module/workflow/entity/action-data.entity.js.map +1 -1
  17. package/dist/module/workflow/entity/action.entity.d.ts +1 -0
  18. package/dist/module/workflow/entity/action.entity.js +4 -0
  19. package/dist/module/workflow/entity/action.entity.js.map +1 -1
  20. package/dist/module/workflow-automation/service/workflow-automation-engine.service.d.ts +1 -1
  21. package/dist/module/workflow-automation/service/workflow-automation-engine.service.js +13 -5
  22. package/dist/module/workflow-automation/service/workflow-automation-engine.service.js.map +1 -1
  23. package/dist/module/workflow-automation/service/workflow-automation.service.d.ts +34 -2
  24. package/dist/module/workflow-automation/service/workflow-automation.service.js +136 -15
  25. package/dist/module/workflow-automation/service/workflow-automation.service.js.map +1 -1
  26. package/dist/tsconfig.build.tsbuildinfo +1 -1
  27. package/package.json +1 -1
  28. package/src/module/communication/communication.module.ts +2 -0
  29. package/src/module/communication/factories/telephone.factory.ts +6 -1
  30. package/src/module/communication/strategies/telephone/tubelight-voice.strategy.ts +122 -0
  31. package/src/module/meta/controller/entity.controller.ts +51 -42
  32. package/src/module/user/service/user.service.ts +2 -1
  33. package/src/module/workflow/entity/action-data.entity.ts +3 -0
  34. package/src/module/workflow/entity/action.entity.ts +3 -0
  35. package/src/module/workflow-automation/service/workflow-automation-engine.service.ts +53 -26
  36. package/src/module/workflow-automation/service/workflow-automation.service.ts +223 -25
@@ -1,14 +1,16 @@
1
- import { Inject, Injectable } from '@nestjs/common';
1
+ import { Inject, Injectable, Logger, NotFoundException } from '@nestjs/common';
2
2
  import { InjectRepository } from '@nestjs/typeorm';
3
- import { Repository } from 'typeorm';
3
+ import { DataSource, Repository } from 'typeorm';
4
4
  import { WorkflowAutomation } from '../entity/workflow-automation.entity';
5
5
  import { WorkflowAutomationActionEntity } from '../entity/workflow-automation-action.entity';
6
6
  import { EntityServiceImpl } from 'src/module/meta/service/entity-service-impl.service';
7
7
  import { UserData } from 'src/module/user/entity/user.entity';
8
8
  import { SavedFilterService } from 'src/module/filter/service/saved-filter.service';
9
+ import { ENTITYTYPE_SAVEDFILTERMASTER } from 'src/constant/global.constant';
9
10
 
10
11
  @Injectable()
11
12
  export class WorkflowAutomationService extends EntityServiceImpl {
13
+ private readonly logger = new Logger(WorkflowAutomationService.name);
12
14
  constructor(
13
15
  @InjectRepository(WorkflowAutomation)
14
16
  private readonly wfRepo: Repository<WorkflowAutomation>,
@@ -16,6 +18,7 @@ export class WorkflowAutomationService extends EntityServiceImpl {
16
18
  private readonly actionRepo: Repository<WorkflowAutomationActionEntity>,
17
19
  @Inject('SavedFilterService')
18
20
  private readonly savedFilterService: SavedFilterService,
21
+ private readonly dataSource: DataSource,
19
22
  ) {
20
23
  super();
21
24
  }
@@ -64,66 +67,261 @@ export class WorkflowAutomationService extends EntityServiceImpl {
64
67
  }
65
68
 
66
69
  async updateEntity(entityData: any, loggedInUser: UserData) {
67
- // 1. Extract "core" workflow fields
70
+ this.logger.debug(
71
+ `Updating WorkflowAutomation with data: ${JSON.stringify(entityData)}`,
72
+ );
73
+
68
74
  const { event, criteria, action, ...workflowData } = entityData;
69
75
 
70
- // 2. Update only valid workflow columns
76
+ // 1. Update core workflow columns first
77
+ this.logger.log(`Updating core workflow fields for workflow...`);
71
78
  let workflow = await super.updateEntity(workflowData, loggedInUser);
79
+ this.logger.debug(`Workflow updated (id=${workflow.id})`);
72
80
 
73
- // 3. EVENT
81
+ // 2. EVENT FILTER
74
82
  if (event?.eventFilterJson || event?.triggerType || event?.event) {
83
+ this.logger.log(`Processing EVENT filter for workflow ${workflow.id}`);
84
+
75
85
  const eventFilterMaster = {
76
86
  entity_type: 'SFM',
77
87
  name: `Event_Filter_${workflow.id}`,
78
88
  filterDetails: event?.eventFilterJson ?? [],
79
89
  filter_scope: 'RULE',
90
+ organization_id: loggedInUser.organization_id,
91
+ enterprise_id: loggedInUser.enterprise_id,
80
92
  };
81
93
 
82
- const savedEvent = await this.savedFilterService.createEntity(
83
- eventFilterMaster,
84
- loggedInUser,
85
- );
94
+ if (workflow.condition_filter_code) {
95
+ this.logger.debug(
96
+ `Existing event filter found: ${workflow.condition_filter_code}`,
97
+ );
98
+ const existing = await this.savedFilterService.getEntityDataByCode(
99
+ ENTITYTYPE_SAVEDFILTERMASTER,
100
+ workflow.condition_filter_code,
101
+ loggedInUser,
102
+ );
103
+
104
+ if (existing) {
105
+ eventFilterMaster['id'] = existing.id;
106
+ const updatedEvent = await this.savedFilterService.updateEntity(
107
+ eventFilterMaster as any,
108
+ loggedInUser,
109
+ );
110
+ workflow.condition_filter_code = updatedEvent.code;
111
+ this.logger.log(
112
+ `Updated existing event filter: ${updatedEvent.code}`,
113
+ );
114
+ }
115
+ } else {
116
+ const savedEvent = await this.savedFilterService.createEntity(
117
+ eventFilterMaster,
118
+ loggedInUser,
119
+ );
120
+ workflow.condition_filter_code = savedEvent.code;
121
+ this.logger.log(`Created new event filter: ${savedEvent.code}`);
122
+ }
86
123
 
87
124
  workflow.trigger_type = event?.triggerType ?? null;
88
125
  workflow.trigger_event = event?.event ?? null;
89
- workflow.condition_filter_code = savedEvent.code;
90
126
  }
91
127
 
92
- // 4. CRITERIA
128
+ // 3. CRITERIA FILTER
93
129
  if (criteria?.criteriaFilterJson?.length) {
130
+ this.logger.log(`Processing CRITERIA filter for workflow ${workflow.id}`);
131
+
94
132
  const criteriaFilterMaster = {
95
133
  entity_type: 'SFM',
96
134
  name: `Criteria_Filter_${workflow.id}`,
97
135
  filterDetails: criteria?.criteriaFilterJson ?? [],
98
136
  filter_scope: 'RULE',
137
+ organization_id: loggedInUser.organization_id,
138
+ enterprise_id: loggedInUser.enterprise_id,
99
139
  };
100
140
 
101
- const savedCriteria = await this.savedFilterService.createEntity(
102
- criteriaFilterMaster,
103
- loggedInUser,
104
- );
141
+ if (workflow.criteria_filter_code) {
142
+ this.logger.debug(
143
+ `Existing criteria filter found: ${workflow.criteria_filter_code}`,
144
+ );
145
+ const existing = await this.savedFilterService.getEntityDataByCode(
146
+ ENTITYTYPE_SAVEDFILTERMASTER,
147
+ workflow.criteria_filter_code,
148
+ loggedInUser,
149
+ );
105
150
 
106
- workflow.criteria_filter_code = savedCriteria.code;
151
+ if (existing) {
152
+ criteriaFilterMaster['id'] = existing.id;
153
+ const updatedCriteria = await this.savedFilterService.updateEntity(
154
+ criteriaFilterMaster as any,
155
+ loggedInUser,
156
+ );
157
+ workflow.criteria_filter_code = updatedCriteria.code;
158
+ this.logger.log(
159
+ `Updated existing criteria filter: ${updatedCriteria.code}`,
160
+ );
161
+ }
162
+ } else {
163
+ const savedCriteria = await this.savedFilterService.createEntity(
164
+ criteriaFilterMaster,
165
+ loggedInUser,
166
+ );
167
+ workflow.criteria_filter_code = savedCriteria.code;
168
+ this.logger.log(`Created new criteria filter: ${savedCriteria.code}`);
169
+ }
107
170
  }
108
171
 
109
- // 5. ACTIONS
172
+ // 4. ACTIONS
110
173
  if (Array.isArray(action) && action.length) {
174
+ this.logger.log(
175
+ `Processing ${action.length} actions for workflow ${workflow.id}`,
176
+ );
177
+
178
+ const existingActions = await this.dataSource
179
+ .getRepository(WorkflowAutomationActionEntity)
180
+ .find({
181
+ where: { workflow_automation_id: workflow.id },
182
+ });
183
+
184
+ const existingMap = new Map(
185
+ existingActions.map((a) => [a.entity_method, a]),
186
+ );
187
+
188
+ // 2. Upsert actions
111
189
  for (const a of action) {
112
- if (!a?.actionCategory) continue; // skip invalid
190
+ if (!a?.actionCategory) {
191
+ this.logger.warn(
192
+ `Skipping action without category: ${JSON.stringify(a)}`,
193
+ );
194
+ continue;
195
+ }
196
+
197
+ const existing = existingMap.get(a.actionCategory);
113
198
 
114
- const actionEntity = new WorkflowAutomationActionEntity();
115
- actionEntity.entity_type = 'WFAA';
116
- actionEntity.workflow_automation_id = workflow.id;
117
- actionEntity.entity_method = a.actionCategory;
118
- actionEntity.payload = a.actionPayload ?? {};
199
+ if (existing) {
200
+ // update
201
+ existing.payload = a.actionPayload ?? {};
202
+ await this.dataSource
203
+ .getRepository(WorkflowAutomationActionEntity)
204
+ .save(existing);
205
+ this.logger.log(`Updated action: ${a.actionCategory}`);
206
+ } else {
207
+ // insert
208
+ const actionEntity = new WorkflowAutomationActionEntity();
209
+ actionEntity.entity_type = 'WFAA';
210
+ actionEntity.workflow_automation_id = workflow.id;
211
+ actionEntity.entity_method = a.actionCategory;
212
+ actionEntity.payload = a.actionPayload ?? {};
119
213
 
120
- await super.createEntity(actionEntity, loggedInUser);
214
+ await this.dataSource
215
+ .getRepository(WorkflowAutomationActionEntity)
216
+ .save(actionEntity);
217
+ this.logger.log(`Created new action: ${a.actionCategory}`);
218
+ }
219
+ }
220
+ const incomingCategories = action.map((a) => a.actionCategory);
221
+ const toDelete = existingActions.filter(
222
+ (a) => !incomingCategories.includes(a.entity_method),
223
+ );
224
+
225
+ if (toDelete.length) {
226
+ await this.dataSource
227
+ .getRepository(WorkflowAutomationActionEntity)
228
+ .remove(toDelete);
229
+ this.logger.log(
230
+ `Deleted ${toDelete.length} actions not present in request.`,
231
+ );
121
232
  }
122
233
  }
123
234
 
124
- // 6. Save workflow with updated trigger/criteria codes
235
+ // 5. Save workflow again with updated codes
125
236
  workflow = await super.updateEntity(workflow, loggedInUser);
237
+ this.logger.debug(`Final workflow updated (id=${workflow.id})`);
126
238
 
127
239
  return workflow;
128
240
  }
241
+
242
+ async getEntityData(entity_type: string, id: number, loggedInUser: UserData) {
243
+ this.logger.log(`Fetching WorkflowAutomation by id=${id}`);
244
+
245
+ // 1. Get workflow automation entity
246
+ const workflowAutomation = await this.dataSource
247
+ .getRepository(WorkflowAutomation)
248
+ .findOne({
249
+ where: { id },
250
+ });
251
+
252
+ if (!workflowAutomation) {
253
+ throw new NotFoundException(`WorkflowAutomation not found with id=${id}`);
254
+ }
255
+
256
+ // 2. EVENT FILTER
257
+ let event: any = null;
258
+ if (workflowAutomation.condition_filter_code) {
259
+ const eventFilterMaster: any =
260
+ await this.savedFilterService.getEntityDataByCode(
261
+ ENTITYTYPE_SAVEDFILTERMASTER,
262
+ workflowAutomation.condition_filter_code,
263
+ loggedInUser,
264
+ );
265
+
266
+ if (eventFilterMaster) {
267
+ // 🔑 fetch filter details using mapped_filter_code
268
+ const eventFilterDetails =
269
+ await this.savedFilterService.getDetailsByCode(
270
+ eventFilterMaster.code,
271
+ );
272
+
273
+ event = {
274
+ eventFilterJson: eventFilterDetails ?? [],
275
+ triggerType: workflowAutomation.trigger_type,
276
+ event: workflowAutomation.trigger_event,
277
+ };
278
+ }
279
+ }
280
+
281
+ // 3. CRITERIA FILTER
282
+ let criteria: any = null;
283
+ if (workflowAutomation.criteria_filter_code) {
284
+ const criteriaFilterMaster: any =
285
+ await this.savedFilterService.getEntityDataByCode(
286
+ ENTITYTYPE_SAVEDFILTERMASTER,
287
+ workflowAutomation.criteria_filter_code,
288
+ loggedInUser,
289
+ );
290
+
291
+ if (criteriaFilterMaster) {
292
+ // 🔑 fetch filter details using mapped_filter_code
293
+ const criteriaFilterDetails =
294
+ await this.savedFilterService.getDetailsByCode(
295
+ criteriaFilterMaster.code,
296
+ );
297
+
298
+ criteria = {
299
+ criteriaFilterJson: criteriaFilterDetails ?? [],
300
+ };
301
+ }
302
+ }
303
+
304
+ // 4. ACTIONS
305
+ const actions = await this.dataSource
306
+ .getRepository(WorkflowAutomationActionEntity)
307
+ .find({
308
+ where: { workflow_automation_id: workflowAutomation.id },
309
+ });
310
+
311
+ const action = actions.map((a) => ({
312
+ actionCategory: a.entity_method,
313
+ actionPayload: a.payload,
314
+ }));
315
+
316
+ // 5. Final response
317
+ const response = {
318
+ ...workflowAutomation,
319
+ event,
320
+ criteria,
321
+ action,
322
+ };
323
+
324
+ this.logger.debug(`Fetched WorkflowAutomation (id=${id}) with relations`);
325
+ return response;
326
+ }
129
327
  }