|
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 |
|
@@ -123,6 +126,78 @@ public Kudos savePreapproved(KudosCreateDTO kudos) { |
123 | 126 | return kudosRepository.update(savedKudos); |
124 | 127 | } |
125 | 128 |
|
| 129 | + @Override |
| 130 | + public Kudos update(KudosUpdateDTO kudos) { |
| 131 | + // Find the corresponding kudos and make sure we have permission. |
| 132 | + final UUID kudosId = kudos.getId(); |
| 133 | + final Kudos existingKudos = |
| 134 | + kudosRepository.findById(kudosId).orElseThrow(() -> |
| 135 | + new BadArgException(KUDOS_DOES_NOT_EXIST_MSG.formatted(kudosId))); |
| 136 | + |
| 137 | + final MemberProfile currentUser = currentUserServices.getCurrentUser(); |
| 138 | + if (!currentUser.getId().equals(existingKudos.getSenderId()) && |
| 139 | + !hasAdministerKudosPermission()) { |
| 140 | + throw new PermissionException(NOT_AUTHORIZED_MSG); |
| 141 | + } |
| 142 | + |
| 143 | + if (kudos.getRecipientMembers() == null || |
| 144 | + kudos.getRecipientMembers().isEmpty()) { |
| 145 | + throw new BadArgException( |
| 146 | + "Kudos must contain at least one recipient"); |
| 147 | + } |
| 148 | + |
| 149 | + // Begin modifying the existing kudos to reflect desired changes. |
| 150 | + final String originalMessage = existingKudos.getMessage(); |
| 151 | + existingKudos.setMessage(kudos.getMessage()); |
| 152 | + |
| 153 | + boolean existingPublic = existingKudos.getPubliclyVisible(); |
| 154 | + boolean proposedPublic = kudos.getPubliclyVisible(); |
| 155 | + if (existingPublic && !proposedPublic) { |
| 156 | + // TODO: Search for and remove the Slack Kudos that the Check-Ins |
| 157 | + // Integration posted. |
| 158 | + existingKudos.setDateApproved(null); |
| 159 | + } else if ((!existingPublic && proposedPublic) || |
| 160 | + (proposedPublic && |
| 161 | + !originalMessage.equals(existingKudos.getMessage()))) { |
| 162 | + // Clear the date approved when going from private to public or |
| 163 | + // if public and the text changed, require approval again. |
| 164 | + existingKudos.setDateApproved(null); |
| 165 | + } |
| 166 | + |
| 167 | + existingKudos.setPubliclyVisible(kudos.getPubliclyVisible()); |
| 168 | + |
| 169 | + List<KudosRecipient> recipients = kudosRecipientRepository |
| 170 | + .findByKudosId(kudosId); |
| 171 | + Set<UUID> proposed = kudos.getRecipientMembers() |
| 172 | + .stream() |
| 173 | + .map(r -> r.getId()) |
| 174 | + .collect(Collectors.toSet()); |
| 175 | + boolean different = (recipients.size() != proposed.size()); |
| 176 | + if (!different) { |
| 177 | + Set<UUID> existing = recipients.stream() |
| 178 | + .map(r -> r.getMemberId()) |
| 179 | + .collect(Collectors.toSet()); |
| 180 | + different = !existing.equals(proposed); |
| 181 | + } |
| 182 | + |
| 183 | + // First, update the Kudos so that we only change recipients if they |
| 184 | + // are different and we were able to update the Kudos. |
| 185 | + final Kudos updated = kudosRepository.update(existingKudos); |
| 186 | + |
| 187 | + // Change recipients, if necessary. |
| 188 | + if (different) { |
| 189 | + updateRecipients(updated, recipients, proposed); |
| 190 | + } |
| 191 | + |
| 192 | + // The kudos has been updated. Send notification to admin, if going |
| 193 | + // from private to public. |
| 194 | + if (!existingPublic && proposedPublic) { |
| 195 | + sendNotification(updated, NotificationType.creation); |
| 196 | + } |
| 197 | + |
| 198 | + return updated; |
| 199 | + } |
| 200 | + |
126 | 201 | @Override |
127 | 202 | public KudosResponseDTO getById(UUID id) { |
128 | 203 |
|
@@ -155,11 +230,16 @@ public KudosResponseDTO getById(UUID id) { |
155 | 230 | } |
156 | 231 |
|
157 | 232 | @Override |
158 | | - @RequiredPermission(Permission.CAN_ADMINISTER_KUDOS) |
159 | 233 | public void delete(UUID id) { |
160 | 234 | Kudos kudos = kudosRepository.findById(id).orElseThrow(() -> |
161 | 235 | new NotFoundException(KUDOS_DOES_NOT_EXIST_MSG.formatted(id))); |
162 | 236 |
|
| 237 | + MemberProfile currentUser = currentUserServices.getCurrentUser(); |
| 238 | + if (!currentUser.getId().equals(kudos.getSenderId()) && |
| 239 | + !hasAdministerKudosPermission()) { |
| 240 | + throw new PermissionException(NOT_AUTHORIZED_MSG); |
| 241 | + } |
| 242 | + |
163 | 243 | // Delete all KudosRecipients associated with this kudos |
164 | 244 | List<KudosRecipient> recipients = kudosRecipientServices.getAllByKudosId(kudos.getId()); |
165 | 245 | kudosRecipientRepository.deleteAll(recipients); |
@@ -231,7 +311,8 @@ private List<KudosResponseDTO> findAllToMember(UUID memberId) { |
231 | 311 | Kudos relatedKudos = kudosRepository.findById(kudosId).orElseThrow(() -> |
232 | 312 | new NotFoundException(KUDOS_DOES_NOT_EXIST_MSG.formatted(kudosId))); |
233 | 313 |
|
234 | | - if (relatedKudos.getDateApproved() != null) { |
| 314 | + if (!relatedKudos.getPubliclyVisible() || |
| 315 | + relatedKudos.getDateApproved() != null) { |
235 | 316 | kudosList.add(constructKudosResponseDTO(relatedKudos)); |
236 | 317 | } |
237 | 318 | }); |
@@ -404,4 +485,26 @@ private Kudos saveCommon(KudosCreateDTO kudosDTO, boolean verifyAndNotify) { |
404 | 485 |
|
405 | 486 | return savedKudos; |
406 | 487 | } |
| 488 | + |
| 489 | + private void updateRecipients(Kudos updated, |
| 490 | + List<KudosRecipient> recipients, |
| 491 | + Set<UUID> proposed) { |
| 492 | + // Add the new recipients. |
| 493 | + Set<UUID> existing = recipients.stream() |
| 494 | + .map(r -> r.getMemberId()) |
| 495 | + .collect(Collectors.toSet()); |
| 496 | + for (UUID id : proposed) { |
| 497 | + if (!existing.contains(id)) { |
| 498 | + kudosRecipientServices.save( |
| 499 | + new KudosRecipient(updated.getId(), id)); |
| 500 | + } |
| 501 | + } |
| 502 | + |
| 503 | + // Remove any that are no longer designated as recipients. |
| 504 | + for (KudosRecipient recipient : recipients) { |
| 505 | + if (!proposed.contains(recipient.getMemberId())) { |
| 506 | + kudosRecipientRepository.delete(recipient); |
| 507 | + } |
| 508 | + } |
| 509 | + } |
407 | 510 | } |
0 commit comments