|
1 | 1 | package com.objectcomputing.checkins.services.kudos; |
2 | 2 |
|
| 3 | +import com.objectcomputing.checkins.exceptions.PermissionException; |
3 | 4 | import com.objectcomputing.checkins.services.permissions.Permission; |
4 | 5 | import com.objectcomputing.checkins.services.permissions.RequiredPermission; |
5 | 6 | import com.objectcomputing.checkins.configuration.CheckInsConfiguration; |
|
37 | 38 | import java.util.List; |
38 | 39 | import java.util.UUID; |
39 | 40 | import java.util.Set; |
| 41 | +import java.util.HashSet; |
| 42 | +import java.util.stream.Collectors; |
40 | 43 |
|
41 | 44 | import static com.objectcomputing.checkins.services.validate.PermissionsValidation.NOT_AUTHORIZED_MSG; |
42 | 45 |
|
@@ -140,6 +143,78 @@ public Kudos approve(Kudos kudos) { |
140 | 143 | return updated; |
141 | 144 | } |
142 | 145 |
|
| 146 | + @Override |
| 147 | + public Kudos update(KudosUpdateDTO kudos) { |
| 148 | + // Find the corresponding kudos and make sure we have permission. |
| 149 | + final UUID kudosId = kudos.getId(); |
| 150 | + final Kudos existingKudos = |
| 151 | + kudosRepository.findById(kudosId).orElseThrow(() -> |
| 152 | + new BadArgException(KUDOS_DOES_NOT_EXIST_MSG.formatted(kudosId))); |
| 153 | + |
| 154 | + final MemberProfile currentUser = currentUserServices.getCurrentUser(); |
| 155 | + if (!currentUser.getId().equals(existingKudos.getSenderId()) && |
| 156 | + !hasAdministerKudosPermission()) { |
| 157 | + throw new PermissionException(NOT_AUTHORIZED_MSG); |
| 158 | + } |
| 159 | + |
| 160 | + if (kudos.getRecipientMembers() == null || |
| 161 | + kudos.getRecipientMembers().isEmpty()) { |
| 162 | + throw new BadArgException( |
| 163 | + "Kudos must contain at least one recipient"); |
| 164 | + } |
| 165 | + |
| 166 | + // Begin modifying the existing kudos to reflect desired changes. |
| 167 | + final String originalMessage = existingKudos.getMessage(); |
| 168 | + existingKudos.setMessage(kudos.getMessage()); |
| 169 | + |
| 170 | + boolean existingPublic = existingKudos.getPubliclyVisible(); |
| 171 | + boolean proposedPublic = kudos.getPubliclyVisible(); |
| 172 | + if (existingPublic && !proposedPublic) { |
| 173 | + // TODO: Search for and remove the Slack Kudos that the Check-Ins |
| 174 | + // Integration posted. |
| 175 | + existingKudos.setDateApproved(null); |
| 176 | + } else if ((!existingPublic && proposedPublic) || |
| 177 | + (proposedPublic && |
| 178 | + !originalMessage.equals(existingKudos.getMessage()))) { |
| 179 | + // Clear the date approved when going from private to public or |
| 180 | + // if public and the text changed, require approval again. |
| 181 | + existingKudos.setDateApproved(null); |
| 182 | + } |
| 183 | + |
| 184 | + existingKudos.setPubliclyVisible(kudos.getPubliclyVisible()); |
| 185 | + |
| 186 | + List<KudosRecipient> recipients = kudosRecipientRepository |
| 187 | + .findByKudosId(kudosId); |
| 188 | + Set<UUID> proposed = kudos.getRecipientMembers() |
| 189 | + .stream() |
| 190 | + .map(r -> r.getId()) |
| 191 | + .collect(Collectors.toSet()); |
| 192 | + boolean different = (recipients.size() != proposed.size()); |
| 193 | + if (!different) { |
| 194 | + Set<UUID> existing = recipients.stream() |
| 195 | + .map(r -> r.getMemberId()) |
| 196 | + .collect(Collectors.toSet()); |
| 197 | + different = !existing.equals(proposed); |
| 198 | + } |
| 199 | + |
| 200 | + // First, update the Kudos so that we only change recipients if they |
| 201 | + // are different and we were able to update the Kudos. |
| 202 | + final Kudos updated = kudosRepository.update(existingKudos); |
| 203 | + |
| 204 | + // Change recipients, if necessary. |
| 205 | + if (different) { |
| 206 | + updateRecipients(updated, recipients, proposed); |
| 207 | + } |
| 208 | + |
| 209 | + // The kudos has been updated. Send notification to admin, if going |
| 210 | + // from private to public. |
| 211 | + if (!existingPublic && proposedPublic) { |
| 212 | + sendNotification(updated, NotificationType.creation); |
| 213 | + } |
| 214 | + |
| 215 | + return updated; |
| 216 | + } |
| 217 | + |
143 | 218 | @Override |
144 | 219 | public KudosResponseDTO getById(UUID id) { |
145 | 220 |
|
@@ -172,11 +247,16 @@ public KudosResponseDTO getById(UUID id) { |
172 | 247 | } |
173 | 248 |
|
174 | 249 | @Override |
175 | | - @RequiredPermission(Permission.CAN_ADMINISTER_KUDOS) |
176 | 250 | public void delete(UUID id) { |
177 | 251 | Kudos kudos = kudosRepository.findById(id).orElseThrow(() -> |
178 | 252 | new NotFoundException(KUDOS_DOES_NOT_EXIST_MSG.formatted(id))); |
179 | 253 |
|
| 254 | + MemberProfile currentUser = currentUserServices.getCurrentUser(); |
| 255 | + if (!currentUser.getId().equals(kudos.getSenderId()) && |
| 256 | + !hasAdministerKudosPermission()) { |
| 257 | + throw new PermissionException(NOT_AUTHORIZED_MSG); |
| 258 | + } |
| 259 | + |
180 | 260 | // Delete all KudosRecipients associated with this kudos |
181 | 261 | List<KudosRecipient> recipients = kudosRecipientServices.getAllByKudosId(kudos.getId()); |
182 | 262 | kudosRecipientRepository.deleteAll(recipients); |
@@ -248,7 +328,8 @@ private List<KudosResponseDTO> findAllToMember(UUID memberId) { |
248 | 328 | Kudos relatedKudos = kudosRepository.findById(kudosId).orElseThrow(() -> |
249 | 329 | new NotFoundException(KUDOS_DOES_NOT_EXIST_MSG.formatted(kudosId))); |
250 | 330 |
|
251 | | - if (relatedKudos.getDateApproved() != null) { |
| 331 | + if (!relatedKudos.getPubliclyVisible() || |
| 332 | + relatedKudos.getDateApproved() != null) { |
252 | 333 | kudosList.add(constructKudosResponseDTO(relatedKudos)); |
253 | 334 | } |
254 | 335 | }); |
@@ -387,4 +468,26 @@ private void slackApprovedKudos(Kudos kudos) { |
387 | 468 | private boolean hasAdministerKudosPermission() { |
388 | 469 | return currentUserServices.hasPermission(Permission.CAN_ADMINISTER_KUDOS); |
389 | 470 | } |
| 471 | + |
| 472 | + private void updateRecipients(Kudos updated, |
| 473 | + List<KudosRecipient> recipients, |
| 474 | + Set<UUID> proposed) { |
| 475 | + // Add the new recipients. |
| 476 | + Set<UUID> existing = recipients.stream() |
| 477 | + .map(r -> r.getMemberId()) |
| 478 | + .collect(Collectors.toSet()); |
| 479 | + for (UUID id : proposed) { |
| 480 | + if (!existing.contains(id)) { |
| 481 | + kudosRecipientServices.save( |
| 482 | + new KudosRecipient(updated.getId(), id)); |
| 483 | + } |
| 484 | + } |
| 485 | + |
| 486 | + // Remove any that are no longer designated as recipients. |
| 487 | + for (KudosRecipient recipient : recipients) { |
| 488 | + if (!proposed.contains(recipient.getMemberId())) { |
| 489 | + kudosRecipientRepository.delete(recipient); |
| 490 | + } |
| 491 | + } |
| 492 | + } |
390 | 493 | } |
0 commit comments