diff --git a/kudos-services/src/main/java/io/meeds/kudos/service/KudosService.java b/kudos-services/src/main/java/io/meeds/kudos/service/KudosService.java index fe7379b27..27b3a9438 100644 --- a/kudos-services/src/main/java/io/meeds/kudos/service/KudosService.java +++ b/kudos-services/src/main/java/io/meeds/kudos/service/KudosService.java @@ -207,17 +207,25 @@ public Kudos createKudos(Kudos kudos, String currentUser) throws IllegalAccessEx throw new IllegalAccessException("User having username'" + currentUser + "' is not authorized to send more kudos"); } - if (kudos.getSenderIdentityId() == null) { - kudos.setSenderIdentityId(senderIdentity.getId()); - } + kudos.setSenderId(senderIdentity.getRemoteId()); + kudos.setSenderIdentityId(senderIdentity.getId()); Object receiverObject = checkStatusAndGetReceiver(kudos.getReceiverType(), kudos.getReceiverId()); if (kudos.getReceiverIdentityId() == null) { - if (receiverObject instanceof Identity identity) { + if (receiverObject instanceof Identity identity && identity.isUser()) { + kudos.setReceiverId(identity.getRemoteId()); + kudos.setReceiverType(USER_ACCOUNT_TYPE); kudos.setReceiverIdentityId(identity.getId()); + } else if (receiverObject instanceof Identity identity && identity.isSpace()) { + Space space = getSpace(identity.getRemoteId()); + kudos.setReceiverIdentityId(space.getId()); + kudos.setReceiverId(space.getPrettyName()); + kudos.setReceiverType(SPACE_ACCOUNT_TYPE); } else if (receiverObject instanceof Space space) { if (canSendKudosInSpace(kudos, space, currentUser)) { + kudos.setReceiverId(space.getPrettyName()); kudos.setReceiverIdentityId(space.getId()); + kudos.setReceiverType(SPACE_ACCOUNT_TYPE); } else { throw new IllegalAccessException("User cannot redact on space"); } diff --git a/kudos-services/src/test/java/io/meeds/kudos/service/KudosServiceTest.java b/kudos-services/src/test/java/io/meeds/kudos/service/KudosServiceTest.java index 4584205a4..eac243a89 100644 --- a/kudos-services/src/test/java/io/meeds/kudos/service/KudosServiceTest.java +++ b/kudos-services/src/test/java/io/meeds/kudos/service/KudosServiceTest.java @@ -441,6 +441,45 @@ public void testSendKudosToSpace() { assertEquals(kudosToSendOnActivity.getEntityId(), retrievedKudos.getEntityId()); } + @Test + @SneakyThrows + public void testSendKudosUsingSpaceId() { + String spaceRemoteId = "space4"; + + Identity spaceIdentity = identityManager.getOrCreateSpaceIdentity(spaceRemoteId); + KudosPeriod currentKudosPeriod = kudosService.getCurrentKudosPeriod(); + + Kudos kudosToSend = newKudosDTO(); + kudosToSend.setReceiverType(SpaceIdentityProvider.NAME); + kudosToSend.setReceiverId(spaceService.getSpaceByPrettyName(spaceIdentity.getRemoteId()).getId()); + kudosToSend.setReceiverIdentityId(null); + kudosToSend.setSpacePrettyName(spaceRemoteId); + + restartTransaction(); + + List list = kudosService.getKudosByPeriodAndReceiver(Long.parseLong(spaceIdentity.getId()), + currentKudosPeriod.getStartDateInSeconds(), + currentKudosPeriod.getEndDateInSeconds(), + 10); + assertNotNull(list); + assertEquals(0, list.size()); + + SpaceServiceMock.setRedactor(SENDER_REMOTE_ID); + try { + Kudos kudos = kudosService.createKudos(kudosToSend, SENDER_REMOTE_ID); + assertNotNull(kudos); + } finally { + SpaceServiceMock.setRedactor(null); + } + + list = kudosService.getKudosByPeriodAndReceiver(Long.parseLong(spaceIdentity.getId()), + currentKudosPeriod.getStartDateInSeconds(), + currentKudosPeriod.getEndDateInSeconds(), + 10); + assertNotNull(list); + assertEquals(1, list.size()); + } + @Test public void testGetKudosByPeriodType() { long startTime = getCurrentTimeInSeconds();