1
1
package com .objectcomputing .checkins .services .memberprofile ;
2
2
3
+ import com .objectcomputing .checkins .services .permissions .Permission ;
3
4
import com .objectcomputing .checkins .exceptions .AlreadyExistsException ;
4
5
import com .objectcomputing .checkins .exceptions .BadArgException ;
5
6
import com .objectcomputing .checkins .exceptions .NotFoundException ;
23
24
import org .slf4j .LoggerFactory ;
24
25
25
26
import java .util .*;
27
+ import java .time .LocalDate ;
26
28
27
29
import static com .objectcomputing .checkins .util .Util .nullSafeUUIDToString ;
30
+ import static com .objectcomputing .checkins .services .validate .PermissionsValidation .NOT_AUTHORIZED_MSG ;
28
31
29
32
@ Singleton
30
33
@ CacheConfig ("member-cache" )
@@ -110,27 +113,9 @@ public MemberProfile saveProfile(MemberProfile memberProfile) {
110
113
emailAssignment (createdMemberProfile , true ); // PDL
111
114
emailAssignment (createdMemberProfile , false ); // Supervisor
112
115
return createdMemberProfile ;
113
- }
114
-
115
- Optional <MemberProfile > existingProfileOpt = memberProfileRepository .findById (memberProfile .getId ());
116
- MemberProfile updatedMemberProfile = memberProfileRepository .update (memberProfile );
117
- if (existingProfileOpt .isEmpty ()) {
118
- LOG .error ("MemberProfile with id {} not found" , memberProfile .getId ());
119
116
} else {
120
- MemberProfile existingProfile = existingProfileOpt .get ();
121
-
122
- boolean pdlChanged = !Objects .equals (existingProfile .getPdlId (), memberProfile .getPdlId ());
123
- boolean supervisorChanged = !Objects .equals (existingProfile .getSupervisorid (), memberProfile .getSupervisorid ());
124
-
125
- if (pdlChanged ) {
126
- emailAssignment (updatedMemberProfile , true ); // PDL
127
- }
128
- if (supervisorChanged ) {
129
- emailAssignment (updatedMemberProfile , false ); // Supervisor
130
- }
117
+ throw new BadArgException ("New member created with an id" );
131
118
}
132
-
133
- return updatedMemberProfile ;
134
119
}
135
120
136
121
public void emailAssignment (MemberProfile member , boolean isPDL ) {
@@ -165,9 +150,6 @@ public void emailAssignment(MemberProfile member, boolean isPDL) {
165
150
@ Override
166
151
@ CacheInvalidate (cacheNames = {"member-cache" })
167
152
public boolean deleteProfile (@ NotNull UUID id ) {
168
- if (!currentUserServices .isAdmin ()) {
169
- throw new PermissionException ("Requires admin privileges" );
170
- }
171
153
MemberProfile memberProfile = memberProfileRepository .findById (id ).orElse (null );
172
154
Set <Role > userRoles = (memberProfile != null ) ? roleServices .findUserRoles (memberProfile .getId ()) : Collections .emptySet ();
173
155
@@ -180,7 +162,7 @@ public boolean deleteProfile(@NotNull UUID id) {
180
162
} else if (!teamMemberServices .findByFields (null , id , null ).isEmpty ()) {
181
163
throw new BadArgException (String .format ("User %s cannot be deleted since TeamMember record(s) exist" , MemberProfileUtils .getFullName (memberProfile )));
182
164
} else if (!userRoles .isEmpty ()) {
183
- throw new BadArgException (String .format ("User %s cannot be deleted since user has PDL role " , MemberProfileUtils .getFullName (memberProfile )));
165
+ throw new BadArgException (String .format ("User %s cannot be deleted since user has one or more roles " , MemberProfileUtils .getFullName (memberProfile )));
184
166
}
185
167
186
168
// Update PDL ID for all associated members before termination
@@ -237,6 +219,52 @@ public List<MemberProfile> getSubordinatesForId(UUID id) {
237
219
@ Override
238
220
@ CacheInvalidate (cacheNames = {"member-cache" })
239
221
public MemberProfile updateProfile (MemberProfile memberProfile ) {
240
- return memberProfileRepository .update (memberProfile );
222
+ if (memberProfile .getId () == null ) {
223
+ throw new BadArgException ("Null profile id in update" );
224
+ }
225
+
226
+ MemberProfile currentUser = currentUserServices .getCurrentUser ();
227
+ if (!currentUserServices .hasPermission (Permission .CAN_EDIT_ALL_ORGANIZATION_MEMBERS ) &&
228
+ (currentUser == null || !currentUser .getId ().equals (memberProfile .getId ()))) {
229
+ throw new PermissionException (NOT_AUTHORIZED_MSG );
230
+ }
231
+
232
+ MemberProfile emailProfile = memberProfileRepository .findByWorkEmail (memberProfile .getWorkEmail ()).orElse (null );
233
+
234
+ if (emailProfile != null && emailProfile .getId () != null && !Objects .equals (memberProfile .getId (), emailProfile .getId ())) {
235
+ throw new AlreadyExistsException (String .format ("Email %s already exists in database" ,
236
+ memberProfile .getWorkEmail ()));
237
+ }
238
+
239
+ Optional <MemberProfile > existingProfileOpt = memberProfileRepository .findById (memberProfile .getId ());
240
+ MemberProfile updatedMemberProfile = memberProfileRepository .update (memberProfile );
241
+ if (existingProfileOpt .isEmpty ()) {
242
+ LOG .error ("MemberProfile with id {} not found" , memberProfile .getId ());
243
+ } else {
244
+ MemberProfile existingProfile = existingProfileOpt .get ();
245
+
246
+ boolean pdlChanged = !Objects .equals (existingProfile .getPdlId (), memberProfile .getPdlId ());
247
+ boolean supervisorChanged = !Objects .equals (existingProfile .getSupervisorid (), memberProfile .getSupervisorid ());
248
+
249
+ if (pdlChanged ) {
250
+ emailAssignment (updatedMemberProfile , true ); // PDL
251
+ }
252
+ if (supervisorChanged ) {
253
+ emailAssignment (updatedMemberProfile , false ); // Supervisor
254
+ }
255
+ }
256
+
257
+ return updatedMemberProfile ;
258
+ }
259
+
260
+ @ Override
261
+ @ CacheInvalidate (cacheNames = {"member-cache" })
262
+ public void updateLastSeen (UUID id ) {
263
+ Optional <MemberProfile > profile = memberProfileRepository .findById (id );
264
+ if (profile .isPresent ()) {
265
+ MemberProfile memberProfile = profile .get ();
266
+ memberProfile .setLastSeen (LocalDate .now ());
267
+ memberProfileRepository .update (memberProfile );
268
+ }
241
269
}
242
270
}
0 commit comments