rez_core 1.0.80 → 1.0.82

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": "1.0.80",
3
+ "version": "1.0.82",
4
4
  "description": "",
5
5
  "author": "",
6
6
  "private": false,
@@ -56,13 +56,23 @@ export class EntityValidationService {
56
56
  const value = entityData[attr.attribute_key];
57
57
 
58
58
  if (this.hasValidValue(value)) {
59
- const existing = await this.dataSource
59
+ let qb = this.dataSource
60
60
  .createQueryBuilder()
61
- .select("*")
61
+ .select('*')
62
62
  .from(db_table_name, db_table_name)
63
- .where(`${db_table_name}.${attr.attribute_key} = :value`, { value })
64
- .limit(1)
65
- .getRawOne();
63
+ .where(`${db_table_name}.${attr.attribute_key} = :value`, { value });
64
+
65
+ // Add AND condition for organization_id if present
66
+ const orgId = entityData.organization_id;
67
+
68
+ if (orgId !== undefined && orgId !== null) {
69
+ qb = qb.andWhere(`${db_table_name}.organization_id = :organization_id`, {
70
+ organization_id: orgId,
71
+ });
72
+ }
73
+
74
+
75
+ const existing = await qb.limit(1).getRawOne();
66
76
 
67
77
  if (existing) {
68
78
  errors.push({
@@ -76,6 +86,7 @@ export class EntityValidationService {
76
86
  return errors;
77
87
  }
78
88
 
89
+
79
90
  /**
80
91
  * Validates both required and unique fields for a given entity type.
81
92
  */
@@ -17,16 +17,26 @@ export class RoleRepository {
17
17
  }
18
18
 
19
19
 
20
- public async isRoleNameExists(name: string, excludeRoleId?: number): Promise<boolean> {
20
+ public async isRoleNameExists(
21
+ name: string,
22
+ options?: { excludeRoleId?: number; organization_id?: number }
23
+ ): Promise<boolean> {
21
24
  const query = this.roleRepo
22
25
  .createQueryBuilder('role')
23
26
  .where('role.name = :name', { name });
24
-
25
- if (excludeRoleId) {
26
- query.andWhere('role.id != :excludeRoleId', { excludeRoleId });
27
+
28
+ if (options?.excludeRoleId) {
29
+ query.andWhere('role.id != :excludeRoleId', { excludeRoleId: options.excludeRoleId });
27
30
  }
28
-
31
+
32
+ if (options?.organization_id !== undefined) {
33
+ query.andWhere('role.organization_id = :organization_id', {
34
+ organization_id: options.organization_id,
35
+ });
36
+ }
37
+
29
38
  const existingRole = await query.getOne();
30
39
  return !!existingRole;
31
40
  }
41
+
32
42
  }
@@ -9,23 +9,32 @@ export class UserRepository {
9
9
  @InjectRepository(UserData) private userRepository: Repository<UserData>,
10
10
  ) {}
11
11
 
12
- async findByEmailId(email: string): Promise<UserData | null> {
13
- if (email) {
14
- return this.userRepository.findOne({ where: { email_id: email } });
15
- }
16
- return null;
17
- }
18
-
19
- async findById(userId: number): Promise<UserData | null> {
12
+ async findById(userId: number ): Promise<UserData | null> {
20
13
  return this.userRepository.findOne({ where: { id: userId } });
21
14
  }
22
-
23
- async findByMobile(mobile: string): Promise<UserData | null> {
24
- if (mobile) {
25
- return this.userRepository.findOne({ where: { mobile: mobile } });
15
+ async findByEmailId(email: string, organization_id?: number): Promise<UserData | null> {
16
+ if (!email) return null;
17
+
18
+ const where: any = { email_id: email };
19
+ if (organization_id !== undefined) {
20
+ where.organization_id = organization_id;
21
+ }
22
+
23
+ return this.userRepository.findOne({ where });
24
+ }
25
+
26
+ async findByMobile(mobile: string, organization_id?: number): Promise<UserData | null> {
27
+ if (!mobile) return null;
28
+
29
+ const where: any = { mobile: mobile };
30
+ if (organization_id !== undefined) {
31
+ where.organization_id = organization_id;
26
32
  }
27
- return null;
33
+
34
+ return this.userRepository.findOne({ where });
28
35
  }
36
+
37
+
29
38
 
30
39
  async saveUser(user: UserData): Promise<UserData> {
31
40
  return this.userRepository.save(user);
@@ -27,7 +27,7 @@ export class RoleService extends EntityServiceImpl {
27
27
  ): Promise<BaseEntity> {
28
28
  let role = entityData as Role;
29
29
 
30
- if (await this.roleRepository.isRoleNameExists(role.name)) {
30
+ if (await this.roleRepository.isRoleNameExists(role.name,{organization_id:entityData.organization_id})) {
31
31
  throw new BadRequestException('Role name already exists.');
32
32
  }
33
33
 
@@ -98,7 +98,7 @@ export class RoleService extends EntityServiceImpl {
98
98
  }
99
99
 
100
100
  if (
101
- await this.roleRepository.isRoleNameExists(entityData.name, entityData.id)
101
+ await this.roleRepository.isRoleNameExists(entityData.name, {excludeRoleId:entityData.id})
102
102
  ) {
103
103
  throw new BadRequestException('Role name already exists.');
104
104
  }
@@ -39,12 +39,16 @@ export class UserService extends EntityServiceImpl {
39
39
  let userData = entityData as CreateUserDto;
40
40
  let existingUser = await this.userRepository.findByEmailId(
41
41
  userData.email_id,
42
+ userData.organization_id
42
43
  );
43
44
  if (existingUser) {
44
45
  throw new BadRequestException('User already exists');
45
46
  }
46
47
 
47
- existingUser = await this.userRepository.findByMobile(userData.mobile);
48
+ existingUser = await this.userRepository.findByMobile(
49
+ userData.mobile,
50
+ userData.organization_id
51
+ );
48
52
  if (existingUser) {
49
53
  throw new BadRequestException('User already exists');
50
54
  }
@@ -113,37 +117,63 @@ export class UserService extends EntityServiceImpl {
113
117
  entityData: BaseEntity,
114
118
  loggedInUserData: UserData,
115
119
  ): Promise<BaseEntity> {
116
- let userDto = entityData as UpdateUserDto;
117
-
120
+ const userDto = entityData as UpdateUserDto;
121
+
122
+ // Fetch the current user data from DB to get the existing encrypted password
123
+ const existingUser = await this.userRepository.findById(entityData.id);
124
+
125
+ if (!existingUser) {
126
+ throw new BadRequestException('User not found');
127
+ }
128
+
129
+ // If password is being updated, check if it's the same as existing one
118
130
  if (userDto.password) {
119
- const password = EncryptUtilService.encryptGCM(
131
+ const decryptedPassword = EncryptUtilService.decryptGCM(
132
+ existingUser.password,
133
+ this.masterKey,
134
+ this.masterIv,
135
+ );
136
+
137
+ if (decryptedPassword === userDto.password) {
138
+ throw new BadRequestException(
139
+ 'New password cannot be the same as the current password',
140
+ );
141
+ }
142
+
143
+ // Encrypt new password
144
+ const encryptedPassword = EncryptUtilService.encryptGCM(
120
145
  userDto.password,
121
146
  this.masterKey,
122
147
  this.masterIv,
123
148
  );
124
- userDto.password = password;
149
+ userDto.password = encryptedPassword;
125
150
  }
126
-
127
- let userData = plainToInstance(UserData, userDto);
128
- let savedData = await super.updateEntity(userData, loggedInUserData);
151
+
152
+ const userData = plainToInstance(UserData, userDto);
153
+
154
+ const savedData = await super.updateEntity(userData, loggedInUserData);
155
+
156
+ // Update roles if provided
129
157
  if (userDto.roles) {
130
- let roles = userDto.roles;
131
- for (const role in roles) {
132
- let userRoleMapping = new UserRoleMapping(
158
+ await this.userRoleMappingService.deleteByUserId(savedData.id); // prevent duplicates
159
+ for (const role of userDto.roles) {
160
+ const userRoleMapping = new UserRoleMapping(
133
161
  savedData.id,
134
- Number(roles[role]),
162
+ Number(role),
135
163
  );
136
164
  await this.userRoleMappingService.assignUserRole(userRoleMapping);
137
165
  }
138
166
  }
167
+
139
168
  return savedData;
140
169
  }
170
+
141
171
 
142
- async findByEmailId(email_id: string): Promise<UserData | null> {
143
- return await this.userRepository.findByEmailId(email_id);
172
+ async findByEmailId(email_id: string,organization_id?: number): Promise<UserData | null> {
173
+ return await this.userRepository.findByEmailId(email_id,organization_id);
144
174
  }
145
175
 
146
- async findByMobile(mobile: string) {
147
- return await this.userRepository.findByMobile(mobile);
176
+ async findByMobile(mobile: string,organization_id: number): Promise<UserData | null> {
177
+ return await this.userRepository.findByMobile(mobile,organization_id);
148
178
  }
149
179
  }