rez_core 2.1.70 → 2.1.72

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.1.70",
3
+ "version": "2.1.72",
4
4
  "description": "",
5
5
  "author": "",
6
6
  "private": false,
@@ -26,7 +26,12 @@ export class OrganizationController {
26
26
  async getOrganizationHierarchy(@Req() req: Request & { user: any }) {
27
27
  const userId = req.user.userData?.id;
28
28
  const appcode = req.user.userData?.appcode;
29
+ const org_id = req.user.userData?.organization_id;
29
30
 
30
- return await this.orgService.getOrganizationHierarchy(userId, appcode);
31
+ return await this.orgService.getOrganizationHierarchy(
32
+ userId,
33
+ appcode,
34
+ org_id,
35
+ );
31
36
  }
32
37
  }
@@ -24,7 +24,7 @@ export class SchoolRepository {
24
24
  });
25
25
  }
26
26
 
27
- async getUserContextDropdown(userId: number, appCode: string) {
27
+ async getUserContextDropdown(userId: number, appCode: string, org_id) {
28
28
  const userRoleRepo = this.dataSource.getRepository(UserRoleMapping);
29
29
 
30
30
  const hasOrgAccess = await userRoleRepo
@@ -34,6 +34,11 @@ export class SchoolRepository {
34
34
  .andWhere('urm.level_type = :levelType', { levelType: 'ORG' })
35
35
  .getRawMany();
36
36
 
37
+ let currentORGLevel_id = hasOrgAccess[0]?.urm_level_id;
38
+ if (hasOrgAccess[0].urm_level_id == 1) {
39
+ currentORGLevel_id = org_id;
40
+ }
41
+
37
42
  if (hasOrgAccess.length > 0) {
38
43
  const schools = await this.dataSource
39
44
  .createQueryBuilder()
@@ -53,7 +58,7 @@ export class SchoolRepository {
53
58
  .from('cr_school', 'sch')
54
59
  .innerJoin('cr_brand', 'br', 'sch.brand_id = br.id')
55
60
  .innerJoin('cr_organization', 'org', 'br.organization_id = org.id')
56
- .where('org.id = :orgId', { orgId: hasOrgAccess[0].urm_level_id })
61
+ .where('org.id = :orgId', { orgId: currentORGLevel_id })
57
62
  .getRawMany();
58
63
 
59
64
  const result = {};
@@ -78,7 +78,15 @@ export class OrganizationService {
78
78
  await repo.delete(id);
79
79
  }
80
80
 
81
- async getOrganizationHierarchy(userId: number, appCode: string) {
82
- return await this.schoolRepository.getUserContextDropdown(userId, appCode);
81
+ async getOrganizationHierarchy(
82
+ userId: number,
83
+ appCode: string,
84
+ orgId: number,
85
+ ) {
86
+ return await this.schoolRepository.getUserContextDropdown(
87
+ userId,
88
+ appCode,
89
+ orgId,
90
+ );
83
91
  }
84
92
  }
@@ -61,54 +61,93 @@ export class ListMasterItemService {
61
61
  ): Promise<any> {
62
62
  const orgId = loggedInUser.organization_id;
63
63
 
64
- for (const item of items) {
64
+ const errors: any[] = [];
65
+
66
+ for (let i = 0; i < items.length; i++) {
67
+ const item = items[i];
65
68
  const name = item.name?.trim();
66
69
  const code = item.code?.trim();
67
70
 
68
- if (!name) {
69
- throw new BadRequestException('Name is required and cannot be empty');
70
- }
71
-
72
- let existingItem;
71
+ try {
72
+ if (!name) {
73
+ throw new BadRequestException(`name is missing at index ${i}`);
74
+ }
73
75
 
74
- // Priority: lookup by code + name
75
- if (code) {
76
- existingItem = await this.listItemsRepo.findOneByCondition({
77
- code,
76
+ if (code) {
77
+ // Check if code exists
78
+ const existingItem = await this.listItemsRepo.findOneByCondition({
79
+ code,
80
+ listtype: listType,
81
+ organization_id: orgId,
82
+ });
83
+
84
+ if (existingItem) {
85
+ // It's an update — check if name is changing to a used name
86
+ if (existingItem.name.toLowerCase() !== name.toLowerCase()) {
87
+ const nameExists = await this.listItemsRepo.findOneByCondition({
88
+ name,
89
+ listtype: listType,
90
+ organization_id: orgId,
91
+ });
92
+
93
+ if (nameExists && nameExists.id !== existingItem.id) {
94
+ throw new BadRequestException(
95
+ `${name} already used by another item`,
96
+ );
97
+ }
98
+ }
99
+
100
+ // Update the existing item
101
+ await this.updateEntity(
102
+ {
103
+ ...existingItem,
104
+ ...item,
105
+ name,
106
+ listtype: listType,
107
+ },
108
+ loggedInUser,
109
+ );
110
+
111
+ continue;
112
+ }
113
+ }
114
+
115
+ // Creation path
116
+ const nameExists = await this.listItemsRepo.findOneByCondition({
78
117
  name,
79
118
  listtype: listType,
80
119
  organization_id: orgId,
81
120
  });
82
- }
83
121
 
84
- // Fallback: lookup by name only if not found above
85
- if (!existingItem) {
86
- existingItem = await this.listItemsRepo.findOneByCondition({
87
- name,
88
- listtype: listType,
89
- organization_id: orgId,
90
- });
91
- }
122
+ if (nameExists) {
123
+ throw new BadRequestException(
124
+ `list item with name ${name} already exists`,
125
+ );
126
+ }
127
+
128
+ const finalCode = code || ''; // Could generate code here if needed
92
129
 
93
- if (existingItem) {
94
- await this.updateEntity(
95
- {
96
- ...existingItem,
97
- ...item,
98
- listtype: listType,
99
- },
100
- loggedInUser,
101
- );
102
- } else {
103
130
  await this.createEntity(
104
131
  {
105
132
  ...item,
106
- listtype: listType,
107
133
  name,
134
+ code: finalCode,
135
+ listtype: listType,
108
136
  value: '',
109
137
  },
110
138
  loggedInUser,
111
139
  );
140
+ } catch (error) {
141
+ errors.push({
142
+ row: i,
143
+ error: [
144
+ {
145
+ field: 'name',
146
+ message:
147
+ error.message || 'An error occurred while processing the item',
148
+ },
149
+ ],
150
+ });
112
151
  }
113
152
  }
114
153
 
@@ -119,7 +158,9 @@ export class ListMasterItemService {
119
158
  );
120
159
 
121
160
  return {
161
+ success: errors.length === 0,
122
162
  listType,
163
+ errors,
123
164
  items: updatedItems,
124
165
  };
125
166
  }