rez_core 2.2.17 → 2.2.20

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rez_core",
3
- "version": "2.2.17",
3
+ "version": "2.2.20",
4
4
  "description": "",
5
5
  "author": "",
6
6
  "private": false,
@@ -20,4 +20,7 @@ export class ActionResourcesMapping extends BaseEntity {
20
20
 
21
21
  @Column({ type: 'varchar', nullable: true })
22
22
  form_id: string;
23
+
24
+ @Column({ type: 'int', nullable: true })
25
+ stg_act_mapping_id: number;
23
26
  }
@@ -26,13 +26,23 @@ export class ActionService extends EntityServiceImpl {
26
26
  return null;
27
27
  }
28
28
 
29
- const template = await this.dataSource.query(
30
- `SELECT template_code FROM cr_wf_action_template_mapping WHERE stg_act_mapping_id =
29
+ const result = await this.dataSource.query(
30
+ `SELECT template_code, form_id FROM cr_wf_action_resources_mapping WHERE stg_act_mapping_id =
31
31
  (SELECT id FROM cr_wf_stage_action_mapping WHERE action_id = ? AND entity_type = 'STGM')`,
32
32
  [actionData.id],
33
33
  );
34
34
 
35
- return { ...actionData, template: template.map((t) => t.template_code) };
35
+ const templates = result
36
+ .filter((t) => t.template_code !== null)
37
+ .map((t) => t.template_code);
38
+
39
+ const form = result.find((f) => f.form_id !== null);
40
+
41
+ return {
42
+ ...actionData,
43
+ template: templates,
44
+ form: form?.form_id ?? null,
45
+ };
36
46
  }
37
47
 
38
48
  async createEntity(
@@ -41,49 +51,49 @@ export class ActionService extends EntityServiceImpl {
41
51
  manager?: EntityManager | null,
42
52
  appcode?: string,
43
53
  ): Promise<any> {
44
- let actionData = entityData as any;
54
+ const actionData = entityData as any;
45
55
 
46
- let action = await super.createEntity(
47
- entityData,
48
- loggedInUser,
49
- manager,
50
- appcode,
51
- );
56
+ // Step 1: Create main action entity
57
+ const action = await super.createEntity(entityData, loggedInUser);
52
58
 
53
- // stage mapping
54
- let stageActionMapping = {
59
+ // Step 2: Create stage mapping
60
+ const stageActionMapping = {
55
61
  action_id: action.id,
56
62
  stage_id: actionData.stage_id,
57
63
  entity_type: 'STGM',
58
64
  } as any;
59
65
 
60
- let stageActionMappingData = await super.createEntity(
66
+ const stageActionMappingData = await super.createEntity(
61
67
  stageActionMapping,
62
68
  loggedInUser,
63
- manager,
64
- appcode,
65
69
  );
66
70
 
67
- // template mapping
71
+ // Step 3: Template mapping (existing)
68
72
  if (Array.isArray(actionData.template) && actionData.template.length > 0) {
69
- const templates = actionData.template;
73
+ for (const templateCode of actionData.template) {
74
+ // ➕ Step 4: Resource mapping for template
75
+ const resourceMapping = {
76
+ stg_act_mapping_id: stageActionMappingData.id,
77
+ template_code: templateCode,
78
+ type: 'template',
79
+ entity_type: 'ARMS',
80
+ } as any;
70
81
 
71
- for (let i = 0; i < templates.length; i++) {
72
- const templateCode = templates[i];
82
+ await super.createEntity(resourceMapping, loggedInUser);
83
+ }
84
+ }
73
85
 
74
- // Create Action-Template Mapping
75
- const actionTemplateMapping = {
86
+ // Step 5: Resource mapping for form (if form is sent)
87
+ if (actionData.form && actionData.form.length > 0) {
88
+ for (const formId of actionData.form) {
89
+ const resourceMapping = {
76
90
  stg_act_mapping_id: stageActionMappingData.id,
77
- template_code: templateCode,
78
- entity_type: 'ATMP',
91
+ form_id: formId,
92
+ type: 'form',
93
+ entity_type: 'ARMS',
79
94
  } as any;
80
95
 
81
- await super.createEntity(
82
- actionTemplateMapping,
83
- loggedInUser,
84
- manager,
85
- appcode,
86
- );
96
+ await super.createEntity(resourceMapping, loggedInUser);
87
97
  }
88
98
  }
89
99
 
@@ -97,10 +107,12 @@ export class ActionService extends EntityServiceImpl {
97
107
  console.log('entityData', entityData);
98
108
 
99
109
  // Extract template from entityData, keep the rest for ActionEntity
100
- const { template, stage_id, ...actionData } = entityData as ActionEntity & {
101
- template?: number[];
102
- stage_id?: string;
103
- };
110
+ const { template, stage_id, form, ...actionData } =
111
+ entityData as ActionEntity & {
112
+ template?: number[];
113
+ stage_id?: string;
114
+ form?: number;
115
+ };
104
116
 
105
117
  // Pass only actionData (without template) to super.updateEntity
106
118
  let action = await super.updateEntity({ ...actionData }, loggedInUser);
@@ -139,16 +151,27 @@ export class ActionService extends EntityServiceImpl {
139
151
  const templateCode = template[i];
140
152
 
141
153
  // Create Action-Template Mapping
142
- const actionTemplateMapping = {
154
+ const resourceMapping = {
143
155
  stg_act_mapping_id: stageActionMappingData.id,
144
156
  template_code: templateCode,
145
- entity_type: 'ATMP',
157
+ type: 'template',
158
+ entity_type: 'ARMS',
146
159
  } as any;
147
160
 
148
- await super.createEntity(actionTemplateMapping, loggedInUser);
161
+ await super.createEntity(resourceMapping, loggedInUser);
149
162
  }
150
163
  }
151
164
 
165
+ if (form) {
166
+ const resourceMapping = {
167
+ stg_act_mapping_id: stageActionMappingData.id,
168
+ form_id: form,
169
+ type: 'form',
170
+ entity_type: 'ARMS',
171
+ } as any;
172
+
173
+ await super.createEntity(resourceMapping, loggedInUser);
174
+ }
152
175
  return action; // Return the action entity instead of actionData
153
176
  }
154
177
 
@@ -1,3 +1,4 @@
1
+ import { ActionCategory } from './../entity/action-category.entity';
1
2
  import { DataSource } from 'typeorm';
2
3
  import { PopulateMetaService } from './../../meta/service/populate-meta.service';
3
4
  import { Inject, Injectable } from '@nestjs/common';
@@ -80,6 +81,29 @@ export class PopulateWorkflowService extends EntityServiceImpl {
80
81
  // }
81
82
 
82
83
  async populateWorkflowData(organization_id: number, loggedInUser: UserData) {
84
+ const defaultEntity = await this.dataSource.query(
85
+ `SELECT * FROM eth_entity WHERE organization_id = -1 AND level_type = 'ORG' AND level_id = -1 AND entity_type = 'ENT'`,
86
+ );
87
+
88
+ const defaultEntityData = defaultEntity[0];
89
+
90
+ defaultEntityData.organization_id = organization_id;
91
+ defaultEntityData.level_id = organization_id;
92
+
93
+ await this.dataSource.query(
94
+ `INSERT INTO eth_entity (entity_type, name, status, code, enterprise_id, organization_id, level_id, level_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
95
+ [
96
+ defaultEntityData.entity_type,
97
+ defaultEntityData.name,
98
+ defaultEntityData.status,
99
+ defaultEntityData.code,
100
+ defaultEntityData.enterprise_id,
101
+ organization_id,
102
+ organization_id,
103
+ defaultEntityData.level_type,
104
+ ],
105
+ );
106
+
83
107
  const workflowTables = [
84
108
  { table: 'cr_workflow', entityType: 'WRFW', service: 'workflowService' },
85
109
  {
@@ -99,7 +123,7 @@ export class PopulateWorkflowService extends EntityServiceImpl {
99
123
  const factoryData = await Promise.all(
100
124
  workflowTables.map(getAllFactoryData),
101
125
  );
102
- const [workflows, stageGroups, stages, actions] = factoryData;
126
+ const [workflows, stageGroups, stages] = factoryData;
103
127
 
104
128
  // === 1. Workflow mapping
105
129
  const workflowIdMap: Record<number, number> = {};
@@ -114,6 +138,13 @@ export class PopulateWorkflowService extends EntityServiceImpl {
114
138
  },
115
139
  loggedInUser,
116
140
  );
141
+
142
+ // adding new entry for workflow level mapping
143
+ await this.dataSource.query(
144
+ `INSERT INTO cr_workflow_level_mapping (workflow_id, mapped_level_id, mapped_level_type) VALUES (?, ?, ?)`,
145
+ [newWf.id, organization_id, 'ORG'],
146
+ );
147
+
117
148
  workflowIdMap[id] = newWf.id;
118
149
  }
119
150
 
@@ -157,28 +188,80 @@ export class PopulateWorkflowService extends EntityServiceImpl {
157
188
  );
158
189
  stageIdMap[id] = newStage.id;
159
190
  stageToStageGroupMap[id] = stage_group_id; // maintain
160
- }
161
191
 
162
- // === 4. Action mapping point to stage, stageGroup, workflow correctly
163
- for (const row of actions) {
164
- const { id, stage_id, created_date, modified_date, ...rest } = row;
192
+ // step 1 find old stage_id in stage_action_mapper and get all actions_ids
193
+ // step 2 search action in cr_wf_action table with those action_ids
194
+ // step 3 create new action with new stage_id and new workflow_id
195
+ // step 4 save new action
165
196
 
166
- const newStageId = stageIdMap[stage_id];
167
- const oldStageGroupId = stageToStageGroupMap[stage_id];
168
- const oldWorkflowId = stageGroupToWorkflowMap[oldStageGroupId];
169
- const newWorkflowId = workflowIdMap[oldWorkflowId];
197
+ const stageIds = await this.dataSource.query(
198
+ `SELECT action_id FROM cr_wf_stage_action_mapping WHERE stage_id = ?`,
199
+ [id],
200
+ );
170
201
 
171
- await this.actionService.createEntity(
172
- {
173
- ...rest,
174
- stage_id: newStageId,
175
- workflow_id: newWorkflowId,
176
- organization_id,
177
- level_id: organization_id,
178
- entity_type: 'ACTN',
179
- },
180
- loggedInUser,
202
+ const actionIds = stageIds.map((item) => item.action_id);
203
+
204
+ let actions = [];
205
+ if (actionIds.length > 0) {
206
+ actions = await this.dataSource.query(
207
+ `SELECT * FROM cr_wf_action WHERE id IN (?)`,
208
+ [actionIds],
209
+ );
210
+ } else {
211
+ // No actions to query
212
+ actions = [];
213
+ }
214
+
215
+ console.log(
216
+ `Found ${actions.length} actions for stage ${id} in workflow ${originalWorkflowId}`,
181
217
  );
218
+
219
+ // === 4. Action mapping — point to stage, stageGroup, workflow correctly
220
+ for (const row of actions as any[]) {
221
+ const { id, created_date, modified_date, workflow_id, ...rest } = row;
222
+
223
+ const newWorkflowId = workflowIdMap[workflow_id];
224
+
225
+ // Step 1: Get the code of the old is_mandatory field
226
+ const [oldIsMandatory] = await this.dataSource.query(
227
+ `SELECT code FROM cr_list_master_items WHERE id = ?`,
228
+ [rest.action_requirement],
229
+ );
230
+
231
+ // Step 2: Get all new 'ACRQ' list master items for the organization
232
+ const newIsMandatoryList = await this.dataSource.query(
233
+ `SELECT id, code FROM cr_list_master_items WHERE organization_id = ? AND listtype = 'ACRQ'`,
234
+ [organization_id],
235
+ );
236
+
237
+ console.log(
238
+ `Populating action for stage ${newStage.id} in workflow ${newWorkflowId}`,
239
+ );
240
+
241
+ console.log(`Old is_mandatory code: ${oldIsMandatory?.code}`);
242
+ console.log(`New is_mandatory list:`, newIsMandatoryList);
243
+
244
+ // Step 3: Find the matching new item with the same code
245
+ const matchedNewMandatory = newIsMandatoryList.find(
246
+ (item) => item.code === oldIsMandatory?.code,
247
+ );
248
+
249
+ console.log(`Matched new mandatory item:`, matchedNewMandatory);
250
+
251
+ // Step 4: Use the matched new item's id (if found)
252
+ await this.actionService.createEntity(
253
+ {
254
+ ...rest,
255
+ stage_id: newStage.id,
256
+ workflow_id: newWorkflowId,
257
+ organization_id,
258
+ action_requirement: matchedNewMandatory?.id ?? null,
259
+ level_id: organization_id,
260
+ entity_type: 'ACTN',
261
+ },
262
+ loggedInUser,
263
+ );
264
+ }
182
265
  }
183
266
 
184
267
  return {
@@ -86,38 +86,32 @@ export class WorkflowService extends EntityServiceImpl {
86
86
  }
87
87
  }
88
88
 
89
+ const isDefault = [true, 1, 'true'].includes(
90
+ (entityData as any).is_default,
91
+ );
89
92
  // Handle is_default uniqueness logic within same level
90
- if ((entityData as Workflow).is_default == true) {
91
- // Step 1: Get previous default workflow for same level_id and level_type
92
- const oldDefault = await this.dataSource.query(
93
- `
94
- SELECT id FROM cr_workflow
95
- WHERE level_id = ? AND level_type = ?
96
- `,
97
- [level_id, level_type],
93
+ if (isDefault) {
94
+ await this.dataSource
95
+ .createQueryBuilder()
96
+ .update(Workflow)
97
+ .set({ is_default: 0 })
98
+ .where(
99
+ 'level_id = :level_id AND level_type = :level_type AND id != :currentId',
100
+ {
101
+ level_id,
102
+ level_type,
103
+ currentId: workflowId,
104
+ },
105
+ )
106
+ .execute();
107
+
108
+ // Update cr_workflow_level_mapping to point to the new workflow
109
+ await this.dataSource.query(
110
+ `UPDATE cr_workflow_level_mapping
111
+ SET workflow_id = ?
112
+ WHERE mapped_level_id = ? AND mapped_level_type = ?`,
113
+ [workflowId, level_id, level_type],
98
114
  );
99
-
100
- if (oldDefault?.length) {
101
- const oldWorkflowId = oldDefault[0].id;
102
-
103
- // Step 2: Update old workflow to is_default = 0
104
- await this.dataSource
105
- .createQueryBuilder()
106
- .update(Workflow)
107
- .set({ is_default: 0 })
108
- .where('id = :id', { id: oldWorkflowId })
109
- .execute();
110
-
111
- // Step 3: Update cr_workflow_level_mapping to point to new workflowId
112
- await this.dataSource.query(
113
- `
114
- UPDATE cr_workflow_level_mapping
115
- SET workflow_id = ?
116
- WHERE mapped_level_id = ? AND mapped_level_type = ?
117
- `,
118
- [workflowId, level_id, level_type],
119
- );
120
- }
121
115
  }
122
116
 
123
117
  // Final update
@@ -53,6 +53,9 @@ import { FormMasterDataEntity } from './entity/form-master.entity';
53
53
  import { FormMasterController } from './controller/form-master.controller';
54
54
  import { FormMasterService } from './service/form-master.service';
55
55
  import { FormMasterRepository } from './repository/form-master.repository';
56
+ import { ActionResourcesMapping } from './entity/action-resources-mapping.entity';
57
+ import { ActionResourcesMappingService } from './service/action-resources-mapping.service';
58
+ import { ActionResourcesMappingController } from './controller/action-resource-mapping.controller';
56
59
 
57
60
  @Module({
58
61
  imports: [
@@ -72,6 +75,7 @@ import { FormMasterRepository } from './repository/form-master.repository';
72
75
  TaskDataEntity,
73
76
  ActionDataEntity,
74
77
  FormMasterDataEntity,
78
+ ActionResourcesMapping,
75
79
  ]),
76
80
  EntityModule,
77
81
  ListMasterModule,
@@ -93,6 +97,10 @@ import { FormMasterRepository } from './repository/form-master.repository';
93
97
  useClass: EntityModificationService,
94
98
  },
95
99
  { provide: 'FormMasterService', useClass: FormMasterService },
100
+ {
101
+ provide: 'ActionResourcesMappingService',
102
+ useClass: ActionResourcesMappingService,
103
+ },
96
104
  WorkflowListMasterService,
97
105
  ActionTemplateMappingService,
98
106
  WorkflowRepository,
@@ -119,6 +127,7 @@ import { FormMasterRepository } from './repository/form-master.repository';
119
127
  'StageGroupService',
120
128
  'EntityModificationService',
121
129
  'FormMasterService',
130
+ 'ActionResourcesMappingService',
122
131
  WorkflowRepository,
123
132
  StageGroupRepository,
124
133
  StageRepository,
@@ -138,6 +147,7 @@ import { FormMasterRepository } from './repository/form-master.repository';
138
147
  ActionCategoryController,
139
148
  TaskController,
140
149
  FormMasterController,
150
+ ActionResourcesMappingController,
141
151
  ],
142
152
  })
143
153
  export class WorkflowModule {}