Skip to content

Commit a6f5ff6

Browse files
committed
Merge branch 'develop' into feature-2868/pull-slack-kudos-in
2 parents 69f0655 + 1e60cbd commit a6f5ff6

File tree

13 files changed

+580
-125
lines changed

13 files changed

+580
-125
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
Kudos savePreapproved(KudosCreateDTO kudos);

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

@@ -123,6 +126,78 @@ public Kudos savePreapproved(KudosCreateDTO kudos) {
123126
return kudosRepository.update(savedKudos);
124127
}
125128

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+
126201
@Override
127202
public KudosResponseDTO getById(UUID id) {
128203

@@ -155,11 +230,16 @@ public KudosResponseDTO getById(UUID id) {
155230
}
156231

157232
@Override
158-
@RequiredPermission(Permission.CAN_ADMINISTER_KUDOS)
159233
public void delete(UUID id) {
160234
Kudos kudos = kudosRepository.findById(id).orElseThrow(() ->
161235
new NotFoundException(KUDOS_DOES_NOT_EXIST_MSG.formatted(id)));
162236

237+
MemberProfile currentUser = currentUserServices.getCurrentUser();
238+
if (!currentUser.getId().equals(kudos.getSenderId()) &&
239+
!hasAdministerKudosPermission()) {
240+
throw new PermissionException(NOT_AUTHORIZED_MSG);
241+
}
242+
163243
// Delete all KudosRecipients associated with this kudos
164244
List<KudosRecipient> recipients = kudosRecipientServices.getAllByKudosId(kudos.getId());
165245
kudosRecipientRepository.deleteAll(recipients);
@@ -231,7 +311,8 @@ private List<KudosResponseDTO> findAllToMember(UUID memberId) {
231311
Kudos relatedKudos = kudosRepository.findById(kudosId).orElseThrow(() ->
232312
new NotFoundException(KUDOS_DOES_NOT_EXIST_MSG.formatted(kudosId)));
233313

234-
if (relatedKudos.getDateApproved() != null) {
314+
if (!relatedKudos.getPubliclyVisible() ||
315+
relatedKudos.getDateApproved() != null) {
235316
kudosList.add(constructKudosResponseDTO(relatedKudos));
236317
}
237318
});
@@ -404,4 +485,26 @@ private Kudos saveCommon(KudosCreateDTO kudosDTO, boolean verifyAndNotify) {
404485

405486
return savedKudos;
406487
}
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+
}
407510
}
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)