rez_core 2.1.147 → 2.1.149

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 (44) hide show
  1. package/dist/module/meta/entity/base-entity.entity.js.map +1 -1
  2. package/dist/module/workflow/controller/action.controller.d.ts +1 -1
  3. package/dist/module/workflow/controller/comm-template.controller.d.ts +1 -1
  4. package/dist/module/workflow/controller/workflow-meta.controller.d.ts +9 -3
  5. package/dist/module/workflow/controller/workflow-meta.controller.js +17 -10
  6. package/dist/module/workflow/controller/workflow-meta.controller.js.map +1 -1
  7. package/dist/module/workflow/entity/action.entity.d.ts +1 -1
  8. package/dist/module/workflow/entity/action.entity.js +2 -2
  9. package/dist/module/workflow/entity/action.entity.js.map +1 -1
  10. package/dist/module/workflow/entity/stage-movement-data.entity.d.ts +2 -0
  11. package/dist/module/workflow/entity/stage-movement-data.entity.js +8 -0
  12. package/dist/module/workflow/entity/stage-movement-data.entity.js.map +1 -1
  13. package/dist/module/workflow/entity/workflow-level-mapping.entity.d.ts +7 -0
  14. package/dist/module/workflow/entity/workflow-level-mapping.entity.js +38 -0
  15. package/dist/module/workflow/entity/workflow-level-mapping.entity.js.map +1 -0
  16. package/dist/module/workflow/repository/stage-movement.repository.d.ts +19 -0
  17. package/dist/module/workflow/repository/stage-movement.repository.js +121 -0
  18. package/dist/module/workflow/repository/stage-movement.repository.js.map +1 -0
  19. package/dist/module/workflow/service/action-category.service.js +4 -1
  20. package/dist/module/workflow/service/action-category.service.js.map +1 -1
  21. package/dist/module/workflow/service/action.service.d.ts +1 -1
  22. package/dist/module/workflow/service/action.service.js +57 -53
  23. package/dist/module/workflow/service/action.service.js.map +1 -1
  24. package/dist/module/workflow/service/comm-template.service.d.ts +1 -1
  25. package/dist/module/workflow/service/comm-template.service.js +1 -1
  26. package/dist/module/workflow/service/comm-template.service.js.map +1 -1
  27. package/dist/module/workflow/service/workflow-meta.service.d.ts +10 -5
  28. package/dist/module/workflow/service/workflow-meta.service.js +47 -22
  29. package/dist/module/workflow/service/workflow-meta.service.js.map +1 -1
  30. package/dist/module/workflow/workflow.module.js +4 -0
  31. package/dist/module/workflow/workflow.module.js.map +1 -1
  32. package/dist/tsconfig.build.tsbuildinfo +1 -1
  33. package/package.json +1 -1
  34. package/src/module/meta/entity/base-entity.entity.ts +1 -1
  35. package/src/module/workflow/controller/workflow-meta.controller.ts +29 -5
  36. package/src/module/workflow/entity/action.entity.ts +2 -2
  37. package/src/module/workflow/entity/stage-movement-data.entity.ts +6 -0
  38. package/src/module/workflow/entity/workflow-level-mapping.entity.ts +18 -0
  39. package/src/module/workflow/repository/stage-movement.repository.ts +181 -0
  40. package/src/module/workflow/service/action-category.service.ts +4 -1
  41. package/src/module/workflow/service/action.service.ts +71 -68
  42. package/src/module/workflow/service/comm-template.service.ts +1 -1
  43. package/src/module/workflow/service/workflow-meta.service.ts +79 -24
  44. package/src/module/workflow/workflow.module.ts +4 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rez_core",
3
- "version": "2.1.147",
3
+ "version": "2.1.149",
4
4
  "description": "",
5
5
  "author": "",
6
6
  "private": false,
@@ -49,7 +49,7 @@ export class BaseEntity {
49
49
  @Expose()
50
50
  modified_by: number;
51
51
 
52
- @Column({ name: 'modified_date', nullable: true })
52
+ @Column({ name: 'modified_date', nullable: true })
53
53
  @Expose()
54
54
  modified_date: Date;
55
55
 
@@ -1,8 +1,18 @@
1
- import { Controller, Get, Query, Post, Body } from '@nestjs/common';
1
+ import {
2
+ Controller,
3
+ Get,
4
+ Query,
5
+ Post,
6
+ Body,
7
+ Req,
8
+ UseGuards,
9
+ } from '@nestjs/common';
2
10
  import { WorkflowMetaService } from '../service/workflow-meta.service';
3
11
  import { StageMovementData } from '../entity/stage-movement-data.entity';
12
+ import { JwtAuthGuard } from 'src/module/auth/guards/jwt.guard';
4
13
 
5
14
  @Controller('workflow-meta')
15
+ @UseGuards(JwtAuthGuard)
6
16
  export class WorkflowMetaController {
7
17
  constructor(private readonly workflowMetaService: WorkflowMetaService) {}
8
18
 
@@ -13,8 +23,15 @@ export class WorkflowMetaController {
13
23
  async getCurrentStage(
14
24
  @Query('entityType') mapped_entity_type: string,
15
25
  @Query('entityId') mapped_entity_id: number,
26
+ @Req() req: Request & { user: any },
16
27
  ): Promise<StageMovementData | null> {
17
- return this.workflowMetaService.getCurrentStage(mapped_entity_type, Number(mapped_entity_id));
28
+ let loggedInUser = req.user.userData;
29
+
30
+ return this.workflowMetaService.getCurrentStage(
31
+ mapped_entity_type,
32
+ Number(mapped_entity_id),
33
+ loggedInUser,
34
+ );
18
35
  }
19
36
 
20
37
  /**
@@ -24,8 +41,14 @@ export class WorkflowMetaController {
24
41
  async getNextStage(
25
42
  @Query('entityType') mapped_entity_type: string,
26
43
  @Query('entityId') mapped_entity_id: number,
44
+ @Req() req: Request & { user: any },
27
45
  ): Promise<number | null> {
28
- const current = await this.workflowMetaService.getCurrentStage(mapped_entity_type, Number(mapped_entity_id));
46
+ const loggedInUser = req.user.userData;
47
+ const current = await this.workflowMetaService.getCurrentStage(
48
+ mapped_entity_type,
49
+ Number(mapped_entity_id),
50
+ loggedInUser,
51
+ );
29
52
  return current ? this.workflowMetaService.getNextStage(current) : null;
30
53
  }
31
54
 
@@ -41,12 +64,13 @@ export class WorkflowMetaController {
41
64
  async moveToNextStage(
42
65
  @Body('entityType') mapped_entity_type: string,
43
66
  @Body('entityId') mapped_entity_id: number,
44
- @Body('currentUserId') current_user_id: number,
67
+ @Req() req: Request & { user: any },
45
68
  ): Promise<string> {
69
+ const loggedInUser = req.user.userData;
46
70
  return this.workflowMetaService.moveToNextStage(
47
71
  mapped_entity_type,
48
72
  Number(mapped_entity_id),
49
- current_user_id,
73
+ loggedInUser,
50
74
  );
51
75
  }
52
76
  }
@@ -24,8 +24,8 @@ export class ActionEntity extends BaseEntity {
24
24
  @Column({ type: 'varchar', nullable: true })
25
25
  reason_code: string;
26
26
 
27
- @Column({ type: 'varchar', nullable: true })
28
- default_reason_code: string;
27
+ @Column({ type: 'int', nullable: true })
28
+ default_reason_code: number;
29
29
 
30
30
  @Column({ default: 0, type: 'boolean' })
31
31
  default_value: boolean;
@@ -15,6 +15,12 @@ export class StageMovementData extends BaseEntity {
15
15
  @Column({ type: 'int', nullable: true })
16
16
  stage_action_mapping_id: number;
17
17
 
18
+ @Column({ type: 'int', nullable: true })
19
+ stage_group_id: number;
20
+
21
+ @Column({ type: 'int', nullable: true })
22
+ stage_id: number;
23
+
18
24
  @Column({ type: 'date', nullable: true })
19
25
  start_date: Date;
20
26
 
@@ -0,0 +1,18 @@
1
+ import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
2
+
3
+ @Entity({ name: 'cr_workflow_level_mapping' })
4
+ export class WorkflowLevelMappingEntity {
5
+ constructor() {}
6
+
7
+ @PrimaryGeneratedColumn()
8
+ id: number;
9
+
10
+ @Column({ nullable: true })
11
+ workflow_id: number;
12
+
13
+ @Column({ nullable: true })
14
+ mapped_level_id: string;
15
+
16
+ @Column({ nullable: true })
17
+ mapped_level_type: string;
18
+ }
@@ -0,0 +1,181 @@
1
+ import { get } from 'http';
2
+ import { Injectable } from '@nestjs/common';
3
+ import { InjectRepository } from '@nestjs/typeorm';
4
+ import { Repository, DataSource, MoreThan } from 'typeorm';
5
+ import { WorkflowLevelMappingEntity } from '../entity/workflow-level-mapping.entity';
6
+ import { StageGroup } from '../entity/stage-group.entity';
7
+ import { Stage } from '../entity/stage.entity';
8
+ import { log } from 'console';
9
+
10
+ @Injectable()
11
+ export class StageMovementRepository {
12
+ constructor(
13
+ @InjectRepository(WorkflowLevelMappingEntity)
14
+ private readonly workflowLevelMappingRepo: Repository<WorkflowLevelMappingEntity>,
15
+ @InjectRepository(StageGroup)
16
+ private readonly stageGroupRepo: Repository<StageGroup>,
17
+ @InjectRepository(Stage)
18
+ private readonly stageRepo: Repository<Stage>,
19
+ private readonly dataSource: DataSource,
20
+ ) {}
21
+
22
+ async getFirstStage({
23
+ loggedInUser,
24
+ mapped_entity_type,
25
+ mapped_entity_id,
26
+ }: {
27
+ loggedInUser: any;
28
+ mapped_entity_type: string;
29
+ mapped_entity_id: string | number;
30
+ }): Promise<any | null> {
31
+ // Try finding mapping at user's level
32
+ let workflowLevelMapping = await this.workflowLevelMappingRepo.findOne({
33
+ where: {
34
+ mapped_level_type: loggedInUser.level_type,
35
+ mapped_level_id: loggedInUser.level_id,
36
+ },
37
+ });
38
+
39
+ // If not found, fallback to organization-level mapping
40
+ if (!workflowLevelMapping) {
41
+ workflowLevelMapping = await this.workflowLevelMappingRepo.findOne({
42
+ where: {
43
+ mapped_level_type: 'ORG',
44
+ mapped_level_id: loggedInUser.organization_id,
45
+ },
46
+ });
47
+ }
48
+
49
+ // If still not found, return null
50
+ if (!workflowLevelMapping) return null;
51
+
52
+ // Find the stage group for this workflow
53
+ const stageGroup = await this.stageGroupRepo.findOne({
54
+ where: {
55
+ workflow_id: workflowLevelMapping.workflow_id,
56
+ },
57
+ order: { id: 'ASC' }, // Ensure you get the earliest/first group if needed
58
+ });
59
+
60
+ if (!stageGroup) return null;
61
+
62
+ // Find the first stage within this group
63
+ const firstStage = await this.stageRepo.findOne({
64
+ where: {
65
+ stage_group_id: stageGroup.id,
66
+ },
67
+ order: { id: 'ASC' }, // Change ordering as per your "first" logic
68
+ });
69
+
70
+ if (!firstStage) return null;
71
+
72
+ // Return comprehensive result
73
+ return {
74
+ mappingUsed: workflowLevelMapping,
75
+ stageGroup,
76
+ firstStage,
77
+ };
78
+ }
79
+
80
+ // async getNextStage(
81
+ // stageGroupId: number,
82
+ // currentStageId: number,
83
+ // ): Promise<any | null> {
84
+ // // 1. Get current stage to find its sequence number
85
+ // const currentStage = await this.stageRepo.findOne({
86
+ // where: { id: currentStageId, stage_group_id: stageGroupId },
87
+ // });
88
+ // if (!currentStage) return null;
89
+
90
+ // // 2. Find the next stage with sequence just after the current one
91
+ // const nextStage = await this.stageRepo.findOne({
92
+ // where: {
93
+ // stage_group_id: stageGroupId,
94
+ // sequence: MoreThan(currentStage.sequence),
95
+ // },
96
+ // order: { sequence: 'ASC' },
97
+ // });
98
+
99
+ // return nextStage || null;
100
+ // }
101
+
102
+ // async getNextStageGroup(
103
+ // currentStageGroupId: number,
104
+ // ): Promise<StageGroup | null> {
105
+ // // 1. Get current stage group
106
+ // const currentStageGroup = await this.stageGroupRepo.findOne({
107
+ // where: { id: currentStageGroupId },
108
+ // });
109
+ // if (!currentStageGroup) return null;
110
+ // // 2. Find the next stage group with a higher sequence
111
+ // const nextStageGroup = await this.stageGroupRepo.findOne({
112
+ // where: {
113
+ // sequence: MoreThan(currentStageGroup.sequence),
114
+ // },
115
+ // order: { sequence: 'ASC' }, // Get the immediate next
116
+ // });
117
+ // return nextStageGroup || null;
118
+ // }
119
+
120
+ async getNextStage(
121
+ stageGroupId: number,
122
+ currentStageId: number,
123
+ ): Promise<any | null> {
124
+ const allStages = await this.stageRepo.find({
125
+ where: { stage_group_id: stageGroupId },
126
+ order: { sequence: 'ASC' },
127
+ });
128
+
129
+ if (currentStageId === 0) return allStages.length > 0 ? allStages[0] : null;
130
+
131
+ const currentStage = allStages.find((s) => s.id == currentStageId);
132
+ if (!currentStage) return null;
133
+
134
+ // Filter for stages with a greater sequence, then pick the smallest one
135
+ const higherStages = allStages
136
+ .filter((s) => s.sequence > currentStage.sequence)
137
+ .sort((a, b) => a.sequence - b.sequence);
138
+
139
+ return higherStages.length > 0 ? higherStages[0] : null;
140
+ }
141
+
142
+ async getNextStageGroup(currentStageGroupId: number): Promise<any | null> {
143
+ const currentStageGroup = await this.stageGroupRepo.findOne({
144
+ where: { id: currentStageGroupId },
145
+ });
146
+ if (!currentStageGroup) return null;
147
+
148
+ //get workflow id of current stage group
149
+ const workflowId = currentStageGroup.workflow_id;
150
+
151
+ // Find the next stage group with a higher sequence
152
+ const nextStageGroup = await this.stageGroupRepo.findOne({
153
+ where: {
154
+ workflow_id: workflowId,
155
+ sequence: MoreThan(currentStageGroup.sequence),
156
+ },
157
+ order: { sequence: 'ASC' }, // Get the immediate next
158
+ });
159
+
160
+ return nextStageGroup || null;
161
+ }
162
+
163
+ async getNextStageOrFirstOfNextGroup(
164
+ stageGroupId: number,
165
+ currentStageId: number,
166
+ ): Promise<any | null> {
167
+ // 1. Try to get the next stage in same group
168
+ const nextStage = await this.getNextStage(stageGroupId, currentStageId);
169
+ if (nextStage) return nextStage;
170
+
171
+ // 2. No next stage. Try to get next stage group
172
+ const nextStageGroup = await this.getNextStageGroup(stageGroupId);
173
+ if (!nextStageGroup) return null;
174
+
175
+ const nextStageGroupFirstStage = await this.getNextStage(
176
+ nextStageGroup.id,
177
+ 0,
178
+ );
179
+ return nextStageGroupFirstStage || null;
180
+ }
181
+ }
@@ -22,6 +22,9 @@ export class ActionCategoryService extends EntityServiceImpl {
22
22
  [entity_type, organization_id],
23
23
  );
24
24
 
25
- return result;
25
+ return result.map((row: any) => ({
26
+ label: row.name,
27
+ value: row.id,
28
+ }));
26
29
  }
27
30
  }
@@ -37,8 +37,6 @@ export class ActionService extends EntityServiceImpl {
37
37
  manager?: EntityManager | null,
38
38
  appcode?: string,
39
39
  ): Promise<any> {
40
- console.log('entityData', entityData);
41
-
42
40
  let actionData = entityData as any;
43
41
 
44
42
  let action = await super.createEntity(
@@ -196,7 +194,7 @@ export class ActionService extends EntityServiceImpl {
196
194
  }
197
195
  const result = await this.dataSource.query(
198
196
  `
199
- SELECT name, value
197
+ SELECT name, id
200
198
  FROM cr_list_master_items
201
199
  WHERE listtype = ? AND organization_id = ?
202
200
  `,
@@ -205,8 +203,8 @@ export class ActionService extends EntityServiceImpl {
205
203
 
206
204
  // Format as array of key-value pairs
207
205
  return result.map((row: any) => ({
208
- label: row.value,
209
- value: row.name,
206
+ label: row.name,
207
+ value: row.id,
210
208
  }));
211
209
  }
212
210
 
@@ -228,88 +226,93 @@ export class ActionService extends EntityServiceImpl {
228
226
  const actionIds = stageActions.map((sa) => sa.action_id);
229
227
  const mappingIds = stageActions.map((sa) => sa.id);
230
228
 
229
+ // Step 2: Get template codes with mapping IDs
231
230
  const templateMappings = await this.dataSource.query(
232
231
  `
233
- SELECT template_code
232
+ SELECT stg_act_mapping_id, template_code
234
233
  FROM cr_wf_action_template_mapping
235
- WHERE stg_act_mapping_id IN (?)
234
+ WHERE stg_act_mapping_id IN (${mappingIds.map(() => '?').join(',')})
236
235
  `,
237
- [mappingIds],
236
+ mappingIds,
238
237
  );
239
238
 
240
239
  const templateCodes = templateMappings.map((tm) => tm.template_code);
241
240
 
242
241
  // Step 3: Fetch template names from cr_wf_comm_template
243
- let templateNameMap: Record<number, string> = {};
242
+ const templateCodeToName: Record<string, string> = {};
244
243
 
245
244
  if (templateCodes.length > 0) {
246
- const templates = await this.dataSource.query(
247
- `
248
- SELECT code, name
249
- FROM cr_wf_comm_template
250
- WHERE code IN (?)
251
- AND organization_id = ?
252
- `,
253
- [templateCodes, organization_id],
254
- );
255
-
256
- // Map template_code to name
257
- const codeToNameMap = templates.reduce((acc, curr) => {
258
- acc[curr.code] = curr.name;
259
- return acc;
260
- }, {});
261
-
262
- // Group template names by mapping_id
263
- templateMappings.forEach((tm) => {
264
- const mappingId = tm.stg_act_mapping_id;
265
- const name = codeToNameMap[tm.template_code];
266
- if (name) {
267
- if (!templateNameMap[mappingId]) {
268
- templateNameMap[mappingId] = name;
269
- } else {
270
- templateNameMap[mappingId] += `, ${name}`;
271
- }
272
- }
245
+ const templates = await this.dataSource
246
+ .createQueryBuilder()
247
+ .select(['t.code AS code', 't.name AS name'])
248
+ .from('cr_wf_comm_template', 't')
249
+ .where('t.code IN (:...codes)', { codes: templateCodes })
250
+ .andWhere('t.organization_id = :orgId', { orgId: organization_id })
251
+ .getRawMany();
252
+
253
+ templates.forEach((tpl) => {
254
+ templateCodeToName[tpl.code] = tpl.name;
273
255
  });
274
256
  }
275
257
 
276
- // Step 2: Fetch action details based on those action_ids
277
- // Step 4: Fetch action details
278
- const result = await this.dataSource.query(
279
- `
280
- SELECT
281
- a.id,
282
- a.name AS action_name,
283
- ac.name AS action_category,
284
- ar.name AS action_requirement
285
- FROM cr_wf_action a
286
- LEFT JOIN cr_wf_action_category ac
287
- ON ac.id = a.action_cat_id
288
- LEFT JOIN cr_list_master_items ar
289
- ON ar.id = a.action_requirement
290
- AND ar.listtype = 'ACRQ'
291
- AND ar.organization_id = ?
292
- WHERE a.organization_id = ?
293
- AND a.id IN (?)
294
- `,
295
- [organization_id, organization_id, actionIds],
296
- );
258
+ // Step 4: Build map of mapping_id template names
259
+ const mappingIdToTemplates: Record<number, string[]> = {};
297
260
 
298
- // Step 5: Attach templates (comma-separated) to each result row
299
- const enrichedResult = result.map((row) => {
300
- // Find all mapping_ids where action_id == current row.id
301
- const relatedMappingIds = stageActions
302
- .filter((sa) => sa.action_id === row.id)
303
- .map((sa) => sa.id);
261
+ for (const tm of templateMappings) {
262
+ const mappingId = tm.stg_act_mapping_id;
263
+ const name = templateCodeToName[tm.template_code];
264
+ if (name) {
265
+ if (!mappingIdToTemplates[mappingId]) {
266
+ mappingIdToTemplates[mappingId] = [];
267
+ }
268
+ mappingIdToTemplates[mappingId].push(name);
269
+ }
270
+ }
271
+
272
+ // Step 5: Build action_id → template names using mappingIdToTemplates + stageActions
273
+ const actionIdToTemplates: Record<number, string[]> = {};
274
+
275
+ for (const sa of stageActions) {
276
+ const mappingId = sa.id;
277
+ const templates = mappingIdToTemplates[mappingId];
278
+ if (templates?.length) {
279
+ if (!actionIdToTemplates[sa.action_id]) {
280
+ actionIdToTemplates[sa.action_id] = [];
281
+ }
282
+ actionIdToTemplates[sa.action_id].push(...templates);
283
+ }
284
+ }
304
285
 
305
- // Collect all template names across those mapping ids
306
- const allNames = relatedMappingIds
307
- .map((mid) => templateNameMap[mid])
308
- .filter(Boolean);
286
+ // Step 6: Fetch action details
287
+ const actionResults = await this.dataSource
288
+ .createQueryBuilder()
289
+ .select([
290
+ 'a.id AS id',
291
+ 'a.name AS name',
292
+ 'ac.name AS action_category',
293
+ 'ar.name AS action_requirement',
294
+ ])
295
+ .from('cr_wf_action', 'a')
296
+ .leftJoin('cr_wf_action_category', 'ac', 'ac.id = a.action_cat_id')
297
+ .leftJoin(
298
+ 'cr_list_master_items',
299
+ 'ar',
300
+ `ar.id = a.action_requirement AND ar.listtype = 'ACRQ' AND ar.organization_id = :orgId`,
301
+ { orgId: organization_id },
302
+ )
303
+ .where('a.organization_id = :orgId', { orgId: organization_id })
304
+ .andWhere('a.id IN (:...actionIds)', { actionIds })
305
+ .getRawMany();
306
+
307
+ // Step 7: Enrich result with template field
308
+ const enrichedResult = actionResults.map((row) => {
309
+ const actionId = Number(row.id); // Ensure numeric ID match
310
+ const templates = actionIdToTemplates[actionId] || [];
311
+ const uniqueTemplates = [...new Set(templates)]; // remove duplicates
309
312
 
310
313
  return {
311
314
  ...row,
312
- template: allNames.length ? allNames.join(', ') : '',
315
+ template: uniqueTemplates.join(', '),
313
316
  };
314
317
  });
315
318
 
@@ -86,7 +86,7 @@ export class CommTemplateService extends EntityServiceImpl {
86
86
 
87
87
  const response = commTemplateData.map((item) => ({
88
88
  value: item.code,
89
- name: item.name,
89
+ label: item.name,
90
90
  }));
91
91
 
92
92
  return response;