rez_core 2.1.55 → 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 -1
  2. package/dist/module/listmaster/controller/list-master.controller.js +20 -4
  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 +14 -7
  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 -1
  8. package/dist/module/listmaster/repository/list-master.repository.js +12 -2
  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 -17
  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 -0
  14. package/dist/module/listmaster/service/list-master.service.js +11 -4
  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 +27 -3
  22. package/src/module/listmaster/repository/list-master-items.repository.ts +26 -7
  23. package/src/module/listmaster/repository/list-master.repository.ts +19 -2
  24. package/src/module/listmaster/service/list-master-item.service.ts +55 -24
  25. package/src/module/listmaster/service/list-master.service.ts +15 -10
  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.55",
3
+ "version": "2.1.57",
4
4
  "description": "",
5
5
  "author": "",
6
6
  "private": false,
@@ -53,10 +53,34 @@ export class ListMasterController {
53
53
  @Get('/getListMasterItems/:type')
54
54
  @UseGuards(JwtAuthGuard)
55
55
  @HttpCode(HttpStatus.OK)
56
- async getListMasterItems(@Param('type') type: string) {
57
- console.log('Fetching list master items for type:', type);
58
- return await this.listMasterItemService.getListMasterItemsByType(type);
56
+ async getListMasterItems(
57
+ @Param('type') type: string,
58
+ @Req() req: Request & { user: any },
59
+ @Query('search') search?: string,
60
+ ) {
61
+ const loggedInUser = req.user.userData;
62
+
63
+ return await this.listMasterItemService.getListMasterItemsByType(
64
+ type,
65
+ loggedInUser.organization_id,
66
+ search,
67
+ );
68
+ }
69
+
70
+ @Get('')
71
+ @UseGuards(JwtAuthGuard)
72
+ @HttpCode(HttpStatus.OK)
73
+ async getAllListMasters(
74
+ @Req() req: Request & { user: any },
75
+ @Query('search') search?: string,
76
+ ) {
77
+ const loggedInUser = req.user.userData;
78
+ return await this.service.getAllListMasterByOrganization(
79
+ loggedInUser.organization_id,
80
+ search,
81
+ );
59
82
  }
83
+
60
84
  @Post('/upsertListMasterItem/:type')
61
85
  @UseGuards(JwtAuthGuard)
62
86
  @HttpCode(HttpStatus.OK)
@@ -28,16 +28,35 @@ 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) {
32
- return this.repo.find({
33
- where: { listtype: type },
34
- order: { sortindex: sortBy === 'asc' ? 'ASC' : 'DESC' },
35
- });
31
+ async findAllItemsByListType(
32
+ type: string,
33
+ sortBy: string,
34
+ organizationId: number,
35
+ search?: string,
36
+ ) {
37
+ const qb = this.repo
38
+ .createQueryBuilder('item')
39
+ .where('item.listtype = :type', { type })
40
+ .andWhere('item.organization_id = :organizationId', { organizationId });
41
+
42
+ if (search?.trim()) {
43
+ qb.andWhere(
44
+ '(LOWER(item.name) LIKE :search OR LOWER(item.code) LIKE :search)',
45
+ { search: `%${search.toLowerCase()}%` },
46
+ );
47
+ }
48
+
49
+ qb.orderBy('item.sortindex', sortBy === 'asc' ? 'ASC' : 'DESC');
50
+
51
+ return await qb.getMany();
36
52
  }
37
53
 
38
- async findOneByCodeAndType(code: string, type: string) {
54
+ // more generic find method
55
+ async findOneByCondition(condition: Partial<ListMasterItems>) {
39
56
  return this.repo.findOne({
40
- where: { code, listtype: type },
57
+ where: {
58
+ ...condition,
59
+ },
41
60
  });
42
61
  }
43
62
 
@@ -14,7 +14,24 @@ 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 });
20
+ }
21
+
22
+ async findAllItems(organizationId: number, search?: string) {
23
+ const qb = this.repo
24
+ .createQueryBuilder('item')
25
+ .where('item.source = :source', { source: 'master' })
26
+ .andWhere('item.organization_id = :organizationId', { organizationId });
27
+
28
+ if (search?.trim()) {
29
+ qb.andWhere(
30
+ '(LOWER(item.name) LIKE :search OR LOWER(item.code) LIKE :search)',
31
+ { search: `%${search.toLowerCase()}%` },
32
+ );
33
+ }
34
+
35
+ return await qb.getMany();
19
36
  }
20
37
  }
@@ -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) {
21
- return await this.listItemsRepo.findAllItemsByListType(listType, 'asc');
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,25 +58,34 @@ export class ListMasterItemService {
34
58
  items: any[],
35
59
  loggedInUser,
36
60
  ): Promise<any> {
37
- console.log('Upserting list master items for type:', listType);
61
+ const orgId = loggedInUser.organization_id;
38
62
 
39
63
  for (const item of items) {
40
- console.log('Processing item:', item);
64
+ const name = item.name?.trim();
65
+ const code = item.code?.trim();
41
66
 
42
- let existingItem;
67
+ if (!name) continue; // Skip invalid item
43
68
 
44
- // If code is provided, use it for lookup
45
- if (item.code) {
46
- console.log('Code provided:', item.code);
47
- existingItem = await this.listItemsRepo.findOneByCodeAndType(
48
- item.code,
49
- listType,
50
- );
69
+ let existingItem;
51
70
 
52
- console.log('Existing item found by code:', existingItem);
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
+ });
53
79
  }
54
80
 
55
- console.log('Existing item found:', existingItem);
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
+ });
88
+ }
56
89
 
57
90
  if (existingItem) {
58
91
  await this.updateEntity(
@@ -64,27 +97,22 @@ export class ListMasterItemService {
64
97
  loggedInUser,
65
98
  );
66
99
  } else {
67
- const createdItem = await this.createEntity(
100
+ await this.createEntity(
68
101
  {
69
102
  ...item,
70
103
  listtype: listType,
104
+ name,
71
105
  value: '',
72
106
  },
73
107
  loggedInUser,
74
108
  );
75
-
76
- // now update the value of the item to its code
77
- console.log('Created item, updating value to code:', createdItem.code);
78
-
79
- await this.listItemsRepo.update(createdItem.id, {
80
- value: createdItem.code,
81
- });
82
109
  }
83
110
  }
84
111
 
85
112
  const updatedItems = await this.listItemsRepo.findAllItemsByListType(
86
113
  listType,
87
114
  'asc',
115
+ orgId,
88
116
  );
89
117
 
90
118
  return {
@@ -94,7 +122,10 @@ export class ListMasterItemService {
94
122
  }
95
123
 
96
124
  async deleteListMasterItem(listType: string, code: string): Promise<string> {
97
- const item = await this.listItemsRepo.findOneByCodeAndType(code, listType);
125
+ const item = await this.listItemsRepo.findOneByCondition({
126
+ code,
127
+ listtype: listType,
128
+ });
98
129
  if (!item) {
99
130
  throw new NotFoundException(
100
131
  `Item with name ${code} not found in type ${listType}`,
@@ -13,7 +13,6 @@ import { HttpService } from '@nestjs/axios';
13
13
  import { EntityManager } from 'typeorm';
14
14
  import { EntityMasterService } from 'src/module/meta/service/entity-master.service';
15
15
  import { UserData } from 'src/module/user/entity/user.entity';
16
- import { log } from 'console';
17
16
  import { EntityServiceImpl } from 'src/module/meta/service/entity-service-impl.service';
18
17
 
19
18
  @Injectable()
@@ -242,12 +241,16 @@ export class ListMasterService {
242
241
  entityData.is_factory = entityData.is_factory || 0;
243
242
  entityData.source = entityData.source || 'master';
244
243
  entityData.status = entityData.status || 'ACTIVE';
245
- entityData.type = entityData.type || 'LIST_MASTER';
244
+ entityData.type = entityData.code;
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
 
@@ -279,4 +277,11 @@ export class ListMasterService {
279
277
  async getEntityData(entity_type: string, id: number): Promise<any> {
280
278
  return await this.entityServiceImpl.getEntityData(entity_type, id);
281
279
  }
280
+
281
+ async getAllListMasterByOrganization(
282
+ organizationId: number,
283
+ search?: string,
284
+ ): Promise<any[]> {
285
+ return await this.listMasterRepo.findAllItems(organizationId, search);
286
+ }
282
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
+ }