24
24
25
25
import java .util .List ;
26
26
import java .util .Set ;
27
+ import java .util .stream .Collectors ;
27
28
28
29
/**
29
30
* Service implementation for updating users.
@@ -56,7 +57,7 @@ public void update(final String id,
56
57
final AysUser user = userReadPort .findById (id )
57
58
.orElseThrow (() -> new AysUserNotExistByIdException (id ));
58
59
59
- String institutionId = identity .getInstitutionId ();
60
+ final String institutionId = identity .getInstitutionId ();
60
61
if (!institutionId .equals (user .getInstitution ().getId ())) {
61
62
throw new AysUserNotExistByIdException (id );
62
63
}
@@ -70,23 +71,33 @@ public void update(final String id,
70
71
.lineNumber (updateRequest .getPhoneNumber ().getLineNumber ())
71
72
.build ();
72
73
73
- if (!user .getPhoneNumber ().equals (phoneNumber )) {
74
+ final boolean isPhoneNumberChanged = !user .getPhoneNumber ().equals (phoneNumber );
75
+ if (isPhoneNumberChanged ) {
74
76
this .validatePhoneNumber (user , phoneNumber );
75
- user .setPhoneNumber (phoneNumber );
76
77
}
77
78
78
- if (!user .getEmailAddress ().equals (updateRequest .getEmailAddress ())) {
79
+ final boolean isEmailChanged = !user .getEmailAddress ().equals (updateRequest .getEmailAddress ());
80
+ if (isEmailChanged ) {
79
81
this .validateEmailAddress (user , updateRequest .getEmailAddress ());
80
- user .setEmailAddress (updateRequest .getEmailAddress ());
81
82
}
82
83
83
- this .validateRolesAndSet (user , updateRequest .getRoleIds (), institutionId );
84
-
85
- user .setFirstName (updateRequest .getFirstName ());
86
- user .setLastName (updateRequest .getLastName ());
87
- user .setCity (updateRequest .getCity ());
88
- user .setUpdatedUser (identity .getUserId ());
84
+ final Set <String > existingRoleIds = user .getRoles ().stream ()
85
+ .map (AysRole ::getId )
86
+ .collect (Collectors .toSet ());
87
+ final boolean isRoleChanged = !existingRoleIds .equals (updateRequest .getRoleIds ());
88
+ if (isRoleChanged ) {
89
+ this .validateRoles (updateRequest .getRoleIds (), institutionId );
90
+ }
89
91
92
+ user .update (
93
+ updateRequest .getEmailAddress (),
94
+ updateRequest .getFirstName (),
95
+ updateRequest .getLastName (),
96
+ phoneNumber ,
97
+ updateRequest .getCity (),
98
+ updateRequest .getRoleIds (),
99
+ identity .getUserId ()
100
+ );
90
101
userSavePort .save (user );
91
102
}
92
103
@@ -212,36 +223,33 @@ private void validateEmailAddress(AysUser user, String emailAddress) {
212
223
213
224
214
225
/**
215
- * Checks the existence of roles by their IDs and returns the corresponding role entities.
226
+ * Validates if all specified roles exist and are active within the given institution.
227
+ * <p>
228
+ * This method checks if each role ID corresponds to an active role in the specified institution.
229
+ * If any role is not found or not active, an exception is thrown with the list of invalid role IDs.
230
+ * </p>
216
231
*
217
- * @param user The user being updated.
218
- * @param roleIds The set of role IDs to be checked and retrieved.
219
- * @throws AysRolesNotExistException if any of the provided role IDs do not exist.
232
+ * @param roleIds the set of role IDs to validate
233
+ * @param institutionId the ID of the institution where the roles should exist
234
+ * @throws AysRolesNotExistException if any of the specified roles do not exist or are not valid for the institution
220
235
*/
221
- private void validateRolesAndSet (final AysUser user , final Set <String > roleIds , final String institutionId ) {
222
-
223
- boolean isRoleNotChanged = user .getRoles ().stream ()
224
- .allMatch (role -> roleIds .contains (role .getId ()));
225
- if (isRoleNotChanged ) {
226
- return ;
227
- }
236
+ private void validateRoles (final Set <String > roleIds , final String institutionId ) {
228
237
229
238
final List <AysRole > roles = roleReadPort .findAllByIds (roleIds ).stream ()
230
239
.filter (AysRole ::isActive )
231
240
.filter (role -> institutionId .equals (role .getInstitution ().getId ()))
232
241
.toList ();
233
242
234
- if (roles .size () == roleIds .size ()) {
235
- user .setRoles (roles );
236
- return ;
237
- }
243
+ if (roles .size () != roleIds .size ()) {
238
244
239
- final List <String > notExistsRoleIds = roleIds .stream ()
240
- .filter (roleId -> roles .stream ()
241
- .noneMatch (roleEntity -> roleEntity .getId ().equals (roleId )))
242
- .toList ();
245
+ final List <String > notExistsRoleIds = roleIds .stream ()
246
+ .filter (roleId -> roles .stream ()
247
+ .noneMatch (roleEntity -> roleEntity .getId ().equals (roleId )))
248
+ .toList ();
249
+
250
+ throw new AysRolesNotExistException (notExistsRoleIds );
251
+ }
243
252
244
- throw new AysRolesNotExistException (notExistsRoleIds );
245
253
}
246
254
247
255
}
0 commit comments