From ff3894f49247b3f1a90fd3865a18ef4becde4b50 Mon Sep 17 00:00:00 2001 From: flaminiaScarciofolo Date: Wed, 30 Oct 2024 17:09:04 +0100 Subject: [PATCH] [SELC-5887] fix user data for fd events --- .../user/event/mapper/NotificationMapper.java | 15 +++++++++++---- .../event/mapper/NotificationMapperTest.java | 17 +++++++++++++++++ .../selfcare/user/model/UserToNotify.java | 5 +++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/apps/user-cdc/src/main/java/it/pagopa/selfcare/user/event/mapper/NotificationMapper.java b/apps/user-cdc/src/main/java/it/pagopa/selfcare/user/event/mapper/NotificationMapper.java index 5d3366fb..88632682 100644 --- a/apps/user-cdc/src/main/java/it/pagopa/selfcare/user/event/mapper/NotificationMapper.java +++ b/apps/user-cdc/src/main/java/it/pagopa/selfcare/user/event/mapper/NotificationMapper.java @@ -1,5 +1,6 @@ package it.pagopa.selfcare.user.event.mapper; +import com.microsoft.applicationinsights.web.dependencies.apachecommons.lang3.StringUtils; import it.pagopa.selfcare.user.UserUtils; import it.pagopa.selfcare.user.event.entity.UserInstitution; import it.pagopa.selfcare.user.model.*; @@ -10,9 +11,7 @@ import org.openapi.quarkus.user_registry_json.model.UserResource; import javax.swing.text.html.Option; -import java.util.Objects; -import java.util.Optional; -import java.util.UUID; +import java.util.*; @Mapper(componentModel = "cdi", imports = UUID.class) public interface NotificationMapper { @@ -36,10 +35,18 @@ default String toUniqueIdNotification(UserInstitution userInstitution, Onboarded @Mapping(target = "product", source = "product.productId") @Mapping(target = "createdAt", source = "product.createdAt") @Mapping(target = "updatedAt", expression = "java((null == product.getUpdatedAt()) ? product.getCreatedAt() : product.getUpdatedAt())") - @Mapping(target = "user", expression = "java(mapUser(userResource, userInstitutionChanged.getUserMailUuid(), product))") + @Mapping(target = "user", expression = "java(mapUserForFD(userResource, product))") @Mapping(target = "type", source = "type") FdUserNotificationToSend toFdUserNotificationToSend(UserInstitution userInstitutionChanged, OnboardedProduct product, UserResource userResource, NotificationUserType type); + @Named("mapUserForFD") + default UserToNotify mapUserForFD(UserResource userResource,OnboardedProduct onboardedProduct) { + UserToNotify userToNotify = new UserToNotify(); + userToNotify.setUserId(Optional.ofNullable(userResource.getId()).map(UUID::toString).orElse(null)); + userToNotify.setRoles(StringUtils.isNotBlank(onboardedProduct.getProductRole()) ? List.of(onboardedProduct.getProductRole()) : Collections.emptyList()); + userToNotify.setRole(Optional.ofNullable(onboardedProduct.getRole()).map(Enum::name).orElse(null)); + return userToNotify; + } @Named("mapUser") default UserToNotify mapUser(UserResource userResource, String userMailUuid, OnboardedProduct onboardedProduct) { diff --git a/apps/user-cdc/src/test/java/it/pagopa/selfcare/user/event/mapper/NotificationMapperTest.java b/apps/user-cdc/src/test/java/it/pagopa/selfcare/user/event/mapper/NotificationMapperTest.java index fc99117e..f6568b88 100644 --- a/apps/user-cdc/src/test/java/it/pagopa/selfcare/user/event/mapper/NotificationMapperTest.java +++ b/apps/user-cdc/src/test/java/it/pagopa/selfcare/user/event/mapper/NotificationMapperTest.java @@ -10,6 +10,8 @@ import org.openapi.quarkus.user_registry_json.model.WorkContactResource; import static org.junit.jupiter.api.Assertions.*; + +import java.util.List; import java.util.Map; import java.util.UUID; @@ -38,6 +40,21 @@ void mapUser_withValidData_shouldMapFieldsCorrectly() { assertEquals(OnboardedProductState.ACTIVE, result.getRelationshipStatus()); } + @Test + void mapUserForFdTest() { + UserResource userResource = new UserResource(); + userResource.setId(UUID.randomUUID()); + OnboardedProduct onboardedProduct = new OnboardedProduct(); + onboardedProduct.setProductRole("Admin"); + onboardedProduct.setRole(PartyRole.MANAGER); + + UserToNotify result = new NotificationMapperImpl().mapUserForFD(userResource, onboardedProduct); + + assertEquals(userResource.getId().toString(), result.getUserId()); + assertEquals(List.of("Admin"), result.getRoles()); + assertEquals("MANAGER", result.getRole()); + } + @Test void mapUser_withNullFields_shouldHandleNullValues() { UserResource userResource = new UserResource(); diff --git a/libs/user-sdk-event/src/main/java/it/pagopa/selfcare/user/model/UserToNotify.java b/libs/user-sdk-event/src/main/java/it/pagopa/selfcare/user/model/UserToNotify.java index 8030707f..6510cf6d 100644 --- a/libs/user-sdk-event/src/main/java/it/pagopa/selfcare/user/model/UserToNotify.java +++ b/libs/user-sdk-event/src/main/java/it/pagopa/selfcare/user/model/UserToNotify.java @@ -1,14 +1,18 @@ package it.pagopa.selfcare.user.model; +import com.fasterxml.jackson.annotation.JsonInclude; import it.pagopa.selfcare.user.model.constants.OnboardedProductState; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.eclipse.microprofile.openapi.annotations.media.Schema; +import java.util.List; + @Data @AllArgsConstructor @NoArgsConstructor +@JsonInclude(JsonInclude.Include.NON_NULL) public class UserToNotify { private String userId; @@ -18,6 +22,7 @@ public class UserToNotify { @Schema(description = "Available values: MANAGER, DELEGATE, SUB_DELEGATE, OPERATOR, ADMIN_EA") private String role; private String productRole; + private List roles; private OnboardedProductState relationshipStatus; }