66import com .objectcomputing .checkins .configuration .CheckInsConfiguration ;
77import com .objectcomputing .checkins .notifications .email .EmailSender ;
88import com .objectcomputing .checkins .notifications .email .MailJetFactory ;
9- import com .objectcomputing .checkins .notifications .social_media .SlackPoster ;
9+ import com .objectcomputing .checkins .notifications .social_media .SlackSender ;
10+ import com .objectcomputing .checkins .services .slack .SlackReader ;
11+ import com .objectcomputing .checkins .services .slack .kudos .BotSentKudosLocator ;
1012import com .objectcomputing .checkins .exceptions .BadArgException ;
1113import com .objectcomputing .checkins .exceptions .NotFoundException ;
1214import com .objectcomputing .checkins .exceptions .PermissionException ;
2325import com .objectcomputing .checkins .services .team .Team ;
2426import com .objectcomputing .checkins .services .team .TeamRepository ;
2527import com .objectcomputing .checkins .util .Util ;
28+ import com .objectcomputing .checkins .configuration .CheckInsConfiguration ;
29+
2630import io .micronaut .core .annotation .Nullable ;
2731import io .micronaut .transaction .annotation .Transactional ;
28- import io .micronaut .http .HttpResponse ;
29- import io .micronaut .http .HttpStatus ;
3032
3133import jakarta .inject .Named ;
3234import jakarta .inject .Singleton ;
35+ import jakarta .inject .Inject ;
36+
3337import org .slf4j .Logger ;
3438import org .slf4j .LoggerFactory ;
3539
@@ -58,13 +62,17 @@ class KudosServicesImpl implements KudosServices {
5862 private final CheckInsConfiguration checkInsConfiguration ;
5963 private final RoleServices roleServices ;
6064 private final MemberProfileServices memberProfileServices ;
61- private final SlackPoster slackPoster ;
65+ private final SlackSender slackSender ;
6266 private final KudosConverter converter ;
67+ private final BotSentKudosLocator botSentKudosLocator ;
6368
6469 private enum NotificationType {
6570 creation , approval
6671 }
6772
73+ @ Inject
74+ private CheckInsConfiguration configuration ;
75+
6876 KudosServicesImpl (KudosRepository kudosRepository ,
6977 KudosRecipientServices kudosRecipientServices ,
7078 KudosRecipientRepository kudosRecipientRepository ,
@@ -75,8 +83,9 @@ private enum NotificationType {
7583 MemberProfileServices memberProfileServices ,
7684 @ Named (MailJetFactory .HTML_FORMAT ) EmailSender emailSender ,
7785 CheckInsConfiguration checkInsConfiguration ,
78- SlackPoster slackPoster ,
79- KudosConverter converter
86+ SlackSender slackSender ,
87+ KudosConverter converter ,
88+ BotSentKudosLocator botSentKudosLocator
8089 ) {
8190 this .kudosRepository = kudosRepository ;
8291 this .kudosRecipientServices = kudosRecipientServices ;
@@ -88,8 +97,9 @@ private enum NotificationType {
8897 this .currentUserServices = currentUserServices ;
8998 this .emailSender = emailSender ;
9099 this .checkInsConfiguration = checkInsConfiguration ;
91- this .slackPoster = slackPoster ;
100+ this .slackSender = slackSender ;
92101 this .converter = converter ;
102+ this .botSentKudosLocator = botSentKudosLocator ;
93103 }
94104
95105 @ Override
@@ -152,9 +162,9 @@ public Kudos update(KudosUpdateDTO kudos) {
152162
153163 boolean existingPublic = existingKudos .getPubliclyVisible ();
154164 boolean proposedPublic = kudos .getPubliclyVisible ();
165+ boolean removePublicSlack = false ;
155166 if (existingPublic && !proposedPublic ) {
156- // TODO: Search for and remove the Slack Kudos that the Check-Ins
157- // Integration posted.
167+ removePublicSlack = true ;
158168 existingKudos .setDateApproved (null );
159169 } else if ((!existingPublic && proposedPublic ) ||
160170 (proposedPublic &&
@@ -195,6 +205,12 @@ public Kudos update(KudosUpdateDTO kudos) {
195205 sendNotification (updated , NotificationType .creation );
196206 }
197207
208+ if (removePublicSlack ) {
209+ // Search for and remove the Slack Kudos that the Check-Ins
210+ // Integration posted.
211+ removeSlackMessage (existingKudos );
212+ }
213+
198214 return updated ;
199215 }
200216
@@ -245,6 +261,12 @@ public void delete(UUID id) {
245261 kudosRecipientRepository .deleteAll (recipients );
246262
247263 kudosRepository .deleteById (id );
264+
265+ if (kudos .getPubliclyVisible ()) {
266+ // Search for and remove the Slack Kudos that the Check-Ins
267+ // Integration posted.
268+ removeSlackMessage (kudos );
269+ }
248270 }
249271
250272 @ Override
@@ -441,11 +463,9 @@ private void sendNotification(Kudos kudos, NotificationType notificationType) {
441463 }
442464
443465 private void slackApprovedKudos (Kudos kudos ) {
444- HttpResponse httpResponse =
445- slackPoster .post (converter .toSlackBlock (kudos ));
446- if (httpResponse .status () != HttpStatus .OK ) {
447- LOG .error ("Unable to POST to Slack: " + httpResponse .reason ());
448- }
466+ slackSender .send (configuration .getApplication ()
467+ .getSlack ().getKudosChannel (),
468+ converter .toSlackBlock (kudos ));
449469 }
450470
451471 private boolean hasAdministerKudosPermission () {
@@ -507,4 +527,13 @@ private void updateRecipients(Kudos updated,
507527 }
508528 }
509529 }
530+
531+ private void removeSlackMessage (Kudos kudos ) {
532+ String ts = botSentKudosLocator .find (kudos );
533+ if (ts != null ) {
534+ slackSender .delete (configuration .getApplication ()
535+ .getSlack ().getKudosChannel (),
536+ ts );
537+ }
538+ }
510539}
0 commit comments