rez_core 2.1.56 → 2.1.57

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 (26) hide show
  1. package/dist/module/listmaster/controller/list-master.controller.d.ts +6 -2
  2. package/dist/module/listmaster/controller/list-master.controller.js +13 -9
  3. package/dist/module/listmaster/controller/list-master.controller.js.map +1 -1
  4. package/dist/module/listmaster/repository/list-master-items.repository.d.ts +2 -2
  5. package/dist/module/listmaster/repository/list-master-items.repository.js +7 -4
  6. package/dist/module/listmaster/repository/list-master-items.repository.js.map +1 -1
  7. package/dist/module/listmaster/repository/list-master.repository.d.ts +2 -2
  8. package/dist/module/listmaster/repository/list-master.repository.js +5 -4
  9. package/dist/module/listmaster/repository/list-master.repository.js.map +1 -1
  10. package/dist/module/listmaster/service/list-master-item.service.d.ts +1 -1
  11. package/dist/module/listmaster/service/list-master-item.service.js +39 -11
  12. package/dist/module/listmaster/service/list-master-item.service.js.map +1 -1
  13. package/dist/module/listmaster/service/list-master.service.d.ts +1 -1
  14. package/dist/module/listmaster/service/list-master.service.js +9 -6
  15. package/dist/module/listmaster/service/list-master.service.js.map +1 -1
  16. package/dist/tsconfig.build.tsbuildinfo +1 -1
  17. package/dist/utils/service/codeGenerator.service.d.ts +3 -0
  18. package/dist/utils/service/codeGenerator.service.js +19 -0
  19. package/dist/utils/service/codeGenerator.service.js.map +1 -0
  20. package/package.json +1 -1
  21. package/src/module/listmaster/controller/list-master.controller.ts +12 -3
  22. package/src/module/listmaster/repository/list-master-items.repository.ts +13 -4
  23. package/src/module/listmaster/repository/list-master.repository.ts +6 -4
  24. package/src/module/listmaster/service/list-master-item.service.ts +58 -17
  25. package/src/module/listmaster/service/list-master.service.ts +12 -11
  26. package/src/utils/service/codeGenerator.service.ts +22 -0
@@ -0,0 +1,3 @@
1
+ export declare class CodeGeneratorService {
2
+ static generateCode(input_string: string): string;
3
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CodeGeneratorService = void 0;
4
+ class CodeGeneratorService {
5
+ static generateCode(input_string) {
6
+ if (!input_string) {
7
+ throw new Error('Input string cannot be empty');
8
+ }
9
+ let code = input_string.trim().toLowerCase();
10
+ code = code.replace(/\s+/g, '_');
11
+ const sanitizedCode = code.replace(/[^a-z0-9_]/g, '');
12
+ if (!sanitizedCode) {
13
+ throw new Error('Invalid input string, cannot generate code');
14
+ }
15
+ return sanitizedCode;
16
+ }
17
+ }
18
+ exports.CodeGeneratorService = CodeGeneratorService;
19
+ //# sourceMappingURL=codeGenerator.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codeGenerator.service.js","sourceRoot":"","sources":["../../../src/utils/service/codeGenerator.service.ts"],"names":[],"mappings":";;;AAAA,MAAa,oBAAoB;IAC/B,MAAM,CAAC,YAAY,CAAC,YAAoB;QACtC,IAAI,CAAC,YAAY,EAAE,CAAC;YAClB,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;QAClD,CAAC;QAGD,IAAI,IAAI,GAAG,YAAY,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;QAG7C,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;QAGjC,MAAM,aAAa,GAAG,IAAI,CAAC,OAAO,CAAC,aAAa,EAAE,EAAE,CAAC,CAAC;QAEtD,IAAI,CAAC,aAAa,EAAE,CAAC;YACnB,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;QAChE,CAAC;QAED,OAAO,aAAa,CAAC;IACvB,CAAC;CACF;AArBD,oDAqBC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rez_core",
3
- "version": "2.1.56",
3
+ "version": "2.1.57",
4
4
  "description": "",
5
5
  "author": "",
6
6
  "private": false,
@@ -55,10 +55,14 @@ export class ListMasterController {
55
55
  @HttpCode(HttpStatus.OK)
56
56
  async getListMasterItems(
57
57
  @Param('type') type: string,
58
+ @Req() req: Request & { user: any },
58
59
  @Query('search') search?: string,
59
60
  ) {
61
+ const loggedInUser = req.user.userData;
62
+
60
63
  return await this.listMasterItemService.getListMasterItemsByType(
61
64
  type,
65
+ loggedInUser.organization_id,
62
66
  search,
63
67
  );
64
68
  }
@@ -66,10 +70,15 @@ export class ListMasterController {
66
70
  @Get('')
67
71
  @UseGuards(JwtAuthGuard)
68
72
  @HttpCode(HttpStatus.OK)
69
- async getAllListMasterItems(
73
+ async getAllListMasters(
74
+ @Req() req: Request & { user: any },
70
75
  @Query('search') search?: string,
71
- ): Promise<any[]> {
72
- return await this.service.getAllListMasterItems(search);
76
+ ) {
77
+ const loggedInUser = req.user.userData;
78
+ return await this.service.getAllListMasterByOrganization(
79
+ loggedInUser.organization_id,
80
+ search,
81
+ );
73
82
  }
74
83
 
75
84
  @Post('/upsertListMasterItem/:type')
@@ -28,10 +28,16 @@ export class ListMasterItemsRepository {
28
28
  return items.map((i) => ({ label: i.name, value: i.value }));
29
29
  }
30
30
 
31
- async findAllItemsByListType(type: string, sortBy: string, search?: string) {
31
+ async findAllItemsByListType(
32
+ type: string,
33
+ sortBy: string,
34
+ organizationId: number,
35
+ search?: string,
36
+ ) {
32
37
  const qb = this.repo
33
38
  .createQueryBuilder('item')
34
- .where('item.listtype = :type', { type });
39
+ .where('item.listtype = :type', { type })
40
+ .andWhere('item.organization_id = :organizationId', { organizationId });
35
41
 
36
42
  if (search?.trim()) {
37
43
  qb.andWhere(
@@ -45,9 +51,12 @@ export class ListMasterItemsRepository {
45
51
  return await qb.getMany();
46
52
  }
47
53
 
48
- async findOneByCodeAndType(code: string, type: string) {
54
+ // more generic find method
55
+ async findOneByCondition(condition: Partial<ListMasterItems>) {
49
56
  return this.repo.findOne({
50
- where: { code, listtype: type },
57
+ where: {
58
+ ...condition,
59
+ },
51
60
  });
52
61
  }
53
62
 
@@ -14,14 +14,16 @@ export class ListMasterRepository {
14
14
  return this.repo.findOne({ where: { type } });
15
15
  }
16
16
 
17
- findByName(name: string) {
18
- return this.repo.findOne({ where: { name } });
17
+ // much better generic find method
18
+ async findOneByCondition(condition: Partial<ListMasterData>) {
19
+ return await this.repo.findOne({ where: condition });
19
20
  }
20
21
 
21
- async findAllItems(search?: string) {
22
+ async findAllItems(organizationId: number, search?: string) {
22
23
  const qb = this.repo
23
24
  .createQueryBuilder('item')
24
- .where('item.source = :source', { source: 'master' });
25
+ .where('item.source = :source', { source: 'master' })
26
+ .andWhere('item.organization_id = :organizationId', { organizationId });
25
27
 
26
28
  if (search?.trim()) {
27
29
  qb.andWhere(
@@ -7,6 +7,7 @@ import {
7
7
  import { ListMasterItemsRepository } from '../repository/list-master-items.repository';
8
8
  import { EntityServiceImpl } from 'src/module/meta/service/entity-service-impl.service';
9
9
  import { UserData } from 'src/module/user/entity/user.entity';
10
+ import { CodeGeneratorService } from 'src/utils/service/codeGenerator.service';
10
11
 
11
12
  @Injectable()
12
13
  export class ListMasterItemService {
@@ -17,12 +18,35 @@ export class ListMasterItemService {
17
18
  private readonly entityServiceImpl: EntityServiceImpl,
18
19
  ) {}
19
20
 
20
- async getListMasterItemsByType(listType: string, search?: string) {
21
- return this.listItemsRepo.findAllItemsByListType(listType, 'asc', search);
21
+ async getListMasterItemsByType(
22
+ listType: string,
23
+ organization_id: number,
24
+ search?: string,
25
+ ) {
26
+ // this method retrieves all list master items for a specific type and organization
27
+ return this.listItemsRepo.findAllItemsByListType(
28
+ listType,
29
+ 'asc',
30
+ organization_id,
31
+ search,
32
+ );
22
33
  }
23
34
 
24
35
  async createEntity(entityData: any, loggedInUser: UserData): Promise<any> {
25
- return await this.entityServiceImpl.createEntity(entityData, loggedInUser);
36
+ try {
37
+ const generatedCode = CodeGeneratorService.generateCode(entityData.name);
38
+
39
+ entityData.code = generatedCode;
40
+ entityData.organization_id = loggedInUser.organization_id;
41
+ entityData.value = generatedCode;
42
+
43
+ return await this.entityServiceImpl.createEntity(
44
+ entityData,
45
+ loggedInUser,
46
+ );
47
+ } catch (error) {
48
+ throw error;
49
+ }
26
50
  }
27
51
 
28
52
  async updateEntity(entityData: any, loggedInUser: UserData): Promise<any> {
@@ -34,15 +58,33 @@ export class ListMasterItemService {
34
58
  items: any[],
35
59
  loggedInUser,
36
60
  ): Promise<any> {
61
+ const orgId = loggedInUser.organization_id;
62
+
37
63
  for (const item of items) {
64
+ const name = item.name?.trim();
65
+ const code = item.code?.trim();
66
+
67
+ if (!name) continue; // Skip invalid item
68
+
38
69
  let existingItem;
39
70
 
40
- // If code is provided, use it for lookup
41
- if (item.code) {
42
- existingItem = await this.listItemsRepo.findOneByCodeAndType(
43
- item.code,
44
- listType,
45
- );
71
+ // Priority: lookup by code + name
72
+ if (code) {
73
+ existingItem = await this.listItemsRepo.findOneByCondition({
74
+ code,
75
+ name,
76
+ listtype: listType,
77
+ organization_id: orgId,
78
+ });
79
+ }
80
+
81
+ // Fallback: lookup by name only if not found above
82
+ if (!existingItem) {
83
+ existingItem = await this.listItemsRepo.findOneByCondition({
84
+ name,
85
+ listtype: listType,
86
+ organization_id: orgId,
87
+ });
46
88
  }
47
89
 
48
90
  if (existingItem) {
@@ -55,26 +97,22 @@ export class ListMasterItemService {
55
97
  loggedInUser,
56
98
  );
57
99
  } else {
58
- const createdItem = await this.createEntity(
100
+ await this.createEntity(
59
101
  {
60
102
  ...item,
61
103
  listtype: listType,
104
+ name,
62
105
  value: '',
63
106
  },
64
107
  loggedInUser,
65
108
  );
66
-
67
- // now update the value of the item to its code
68
-
69
- await this.listItemsRepo.update(createdItem.id, {
70
- value: createdItem.code,
71
- });
72
109
  }
73
110
  }
74
111
 
75
112
  const updatedItems = await this.listItemsRepo.findAllItemsByListType(
76
113
  listType,
77
114
  'asc',
115
+ orgId,
78
116
  );
79
117
 
80
118
  return {
@@ -84,7 +122,10 @@ export class ListMasterItemService {
84
122
  }
85
123
 
86
124
  async deleteListMasterItem(listType: string, code: string): Promise<string> {
87
- const item = await this.listItemsRepo.findOneByCodeAndType(code, listType);
125
+ const item = await this.listItemsRepo.findOneByCondition({
126
+ code,
127
+ listtype: listType,
128
+ });
88
129
  if (!item) {
89
130
  throw new NotFoundException(
90
131
  `Item with name ${code} not found in type ${listType}`,
@@ -241,13 +241,16 @@ export class ListMasterService {
241
241
  entityData.is_factory = entityData.is_factory || 0;
242
242
  entityData.source = entityData.source || 'master';
243
243
  entityData.status = entityData.status || 'ACTIVE';
244
- entityData.type = entityData.type || 'LIST_MASTER';
244
+ entityData.type = entityData.code;
245
245
  entityData.sort_by = entityData.sort_by || 'asc';
246
+ entityData.organization_id = loggedInUser.organization_id;
246
247
 
247
248
  // check if a list master with the same type already exists
248
- const existingListMaster = await this.listMasterRepo.findByName(
249
- entityData.name,
250
- );
249
+ const existingListMaster = await this.listMasterRepo.findOneByCondition({
250
+ name: entityData.name,
251
+ code: entityData.code,
252
+ organization_id: loggedInUser.organization_id,
253
+ });
251
254
 
252
255
  if (existingListMaster) {
253
256
  throw new BadRequestException(
@@ -264,11 +267,6 @@ export class ListMasterService {
264
267
  throw new BadRequestException('Failed to create entity');
265
268
  }
266
269
 
267
- createdListMaster.type = createdListMaster.code;
268
-
269
- // update the type of the created list master to code
270
- await this.entityServiceImpl.updateEntity(createdListMaster, loggedInUser);
271
-
272
270
  return createdListMaster;
273
271
  }
274
272
 
@@ -280,7 +278,10 @@ export class ListMasterService {
280
278
  return await this.entityServiceImpl.getEntityData(entity_type, id);
281
279
  }
282
280
 
283
- async getAllListMasterItems(search?: string): Promise<any[]> {
284
- return await this.listMasterRepo.findAllItems(search);
281
+ async getAllListMasterByOrganization(
282
+ organizationId: number,
283
+ search?: string,
284
+ ): Promise<any[]> {
285
+ return await this.listMasterRepo.findAllItems(organizationId, search);
285
286
  }
286
287
  }
@@ -0,0 +1,22 @@
1
+ export class CodeGeneratorService {
2
+ static generateCode(input_string: string) {
3
+ if (!input_string) {
4
+ throw new Error('Input string cannot be empty');
5
+ }
6
+
7
+ // Step 1: Trim, to lowercase
8
+ let code = input_string.trim().toLowerCase();
9
+
10
+ // Step 2: Replace all whitespace with underscores
11
+ code = code.replace(/\s+/g, '_');
12
+
13
+ // Step 3: Remove all non-alphanumeric and non-underscore characters
14
+ const sanitizedCode = code.replace(/[^a-z0-9_]/g, '');
15
+
16
+ if (!sanitizedCode) {
17
+ throw new Error('Invalid input string, cannot generate code');
18
+ }
19
+
20
+ return sanitizedCode;
21
+ }
22
+ }