Skip to content

Commit 73f3947

Browse files
authored
Merge branch 'develop' into feature-2805/convert-skills-data-structure
2 parents 051b895 + 8aa3f26 commit 73f3947

File tree

10 files changed

+474
-99
lines changed

10 files changed

+474
-99
lines changed

server/src/main/java/com/objectcomputing/checkins/services/kudos/KudosController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public Kudos create(@Body @Valid KudosCreateDTO kudos) {
3939
}
4040

4141
@Put
42+
public Kudos update(@Body @Valid KudosUpdateDTO kudos) {
43+
return kudosServices.update(kudos);
44+
}
45+
46+
@Put("/approve")
4247
public Kudos approve(@Body @Valid Kudos kudos) {
4348
return kudosServices.approve(kudos);
4449
}

server/src/main/java/com/objectcomputing/checkins/services/kudos/KudosServices.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public interface KudosServices {
99

1010
Kudos save(KudosCreateDTO kudos);
1111

12+
Kudos update(KudosUpdateDTO kudos);
13+
1214
Kudos approve(Kudos kudos);
1315

1416
List<KudosResponseDTO> getRecent();

server/src/main/java/com/objectcomputing/checkins/services/kudos/KudosServicesImpl.java

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.objectcomputing.checkins.services.kudos;
22

3+
import com.objectcomputing.checkins.exceptions.PermissionException;
34
import com.objectcomputing.checkins.services.permissions.Permission;
45
import com.objectcomputing.checkins.services.permissions.RequiredPermission;
56
import com.objectcomputing.checkins.configuration.CheckInsConfiguration;
@@ -37,6 +38,8 @@
3738
import java.util.List;
3839
import java.util.UUID;
3940
import java.util.Set;
41+
import java.util.HashSet;
42+
import java.util.stream.Collectors;
4043

4144
import static com.objectcomputing.checkins.services.validate.PermissionsValidation.NOT_AUTHORIZED_MSG;
4245

@@ -140,6 +143,78 @@ public Kudos approve(Kudos kudos) {
140143
return updated;
141144
}
142145

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+
143218
@Override
144219
public KudosResponseDTO getById(UUID id) {
145220

@@ -172,11 +247,16 @@ public KudosResponseDTO getById(UUID id) {
172247
}
173248

174249
@Override
175-
@RequiredPermission(Permission.CAN_ADMINISTER_KUDOS)
176250
public void delete(UUID id) {
177251
Kudos kudos = kudosRepository.findById(id).orElseThrow(() ->
178252
new NotFoundException(KUDOS_DOES_NOT_EXIST_MSG.formatted(id)));
179253

254+
MemberProfile currentUser = currentUserServices.getCurrentUser();
255+
if (!currentUser.getId().equals(kudos.getSenderId()) &&
256+
!hasAdministerKudosPermission()) {
257+
throw new PermissionException(NOT_AUTHORIZED_MSG);
258+
}
259+
180260
// Delete all KudosRecipients associated with this kudos
181261
List<KudosRecipient> recipients = kudosRecipientServices.getAllByKudosId(kudos.getId());
182262
kudosRecipientRepository.deleteAll(recipients);
@@ -248,7 +328,8 @@ private List<KudosResponseDTO> findAllToMember(UUID memberId) {
248328
Kudos relatedKudos = kudosRepository.findById(kudosId).orElseThrow(() ->
249329
new NotFoundException(KUDOS_DOES_NOT_EXIST_MSG.formatted(kudosId)));
250330

251-
if (relatedKudos.getDateApproved() != null) {
331+
if (!relatedKudos.getPubliclyVisible() ||
332+
relatedKudos.getDateApproved() != null) {
252333
kudosList.add(constructKudosResponseDTO(relatedKudos));
253334
}
254335
});
@@ -387,4 +468,26 @@ private void slackApprovedKudos(Kudos kudos) {
387468
private boolean hasAdministerKudosPermission() {
388469
return currentUserServices.hasPermission(Permission.CAN_ADMINISTER_KUDOS);
389470
}
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+
}
390493
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.objectcomputing.checkins.services.kudos;
2+
3+
import com.objectcomputing.checkins.services.memberprofile.MemberProfile;
4+
import io.micronaut.core.annotation.Introspected;
5+
import io.micronaut.core.annotation.Nullable;
6+
import jakarta.validation.constraints.NotBlank;
7+
import jakarta.validation.constraints.NotNull;
8+
import lombok.AllArgsConstructor;
9+
import lombok.Getter;
10+
import lombok.Setter;
11+
12+
import java.util.List;
13+
import java.util.UUID;
14+
15+
@Getter
16+
@Setter
17+
@AllArgsConstructor
18+
@Introspected
19+
public class KudosUpdateDTO {
20+
@NotNull
21+
private UUID id;
22+
23+
@NotBlank
24+
private String message;
25+
26+
@Nullable
27+
private Boolean publiclyVisible;
28+
29+
@NotNull
30+
private List<MemberProfile> recipientMembers;
31+
}

0 commit comments

Comments
 (0)