From ba512f42b304427174e2f623aef2c0a61848132c Mon Sep 17 00:00:00 2001 From: flaminiaScarciofolo Date: Wed, 27 Nov 2024 17:44:23 +0100 Subject: [PATCH 1/4] [SELC-6087] Updated api updateUserRegistryAndSendNotification to allow updating the phone number in addition to the email. --- apps/user-ms/src/main/docs/openapi.json | 11 +- apps/user-ms/src/main/docs/openapi.yaml | 12 +- .../user/constant/CollectionUtil.java | 2 + .../user/controller/UserController.java | 2 +- .../response/CertifiableFieldResponse.java | 3 +- .../selfcare/user/mapper/UserMapper.java | 159 ++++++-- .../user/model/UpdateUserRequest.java | 6 +- .../user/service/UserRegistryServiceImpl.java | 75 +++- .../pagopa/selfcare/user/util/UserUtils.java | 8 +- .../src/main/openapi/user_registry.json | 167 ++++++--- .../user/controller/UserControllerTest.java | 20 +- .../selfcare/user/mapper/UserMapperTest.java | 350 +++++++++++++++++- .../UserNotificationServiceImplTest.java | 11 +- .../user/service/UserRegistryServiceTest.java | 32 +- .../user/service/UserServiceTest.java | 24 +- .../selfcare/user/util/UserUtilTest.java | 15 +- 16 files changed, 712 insertions(+), 185 deletions(-) diff --git a/apps/user-ms/src/main/docs/openapi.json b/apps/user-ms/src/main/docs/openapi.json index a2addf06..a64081a8 100644 --- a/apps/user-ms/src/main/docs/openapi.json +++ b/apps/user-ms/src/main/docs/openapi.json @@ -1387,14 +1387,10 @@ "type" : "string" }, "certified" : { - "$ref" : "#/components/schemas/CertificationEnum" + "type" : "string" } } }, - "CertificationEnum" : { - "enum" : [ "NONE", "SPID" ], - "type" : "string" - }, "CreateUserDto" : { "required" : [ "institutionId", "user", "product" ], "type" : "object", @@ -1620,7 +1616,6 @@ } }, "UpdateUserRequest" : { - "required" : [ "email" ], "type" : "object", "properties" : { "name" : { @@ -1631,6 +1626,10 @@ }, "email" : { "type" : "string" + }, + "mobilePhone" : { + "pattern" : "^\\+?[0-9]{9,15}$", + "type" : "string" } } }, diff --git a/apps/user-ms/src/main/docs/openapi.yaml b/apps/user-ms/src/main/docs/openapi.yaml index 3c3a0a0c..a173286a 100644 --- a/apps/user-ms/src/main/docs/openapi.yaml +++ b/apps/user-ms/src/main/docs/openapi.yaml @@ -993,12 +993,7 @@ components: value: type: string certified: - $ref: "#/components/schemas/CertificationEnum" - CertificationEnum: - enum: - - NONE - - SPID - type: string + type: string CreateUserDto: required: - institutionId @@ -1182,8 +1177,6 @@ components: institutionRootName: type: string UpdateUserRequest: - required: - - email type: object properties: name: @@ -1192,6 +1185,9 @@ components: type: string email: type: string + mobilePhone: + pattern: "^\\+?[0-9]{9,15}$" + type: string User: required: - fiscalCode diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/constant/CollectionUtil.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/constant/CollectionUtil.java index 1736cc54..a51093ef 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/constant/CollectionUtil.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/constant/CollectionUtil.java @@ -12,4 +12,6 @@ public class CollectionUtil { public static final String CURRENT_ANY = ".$[]."; public static final String MAIL_ID_PREFIX = "ID_MAIL#"; + + public static final String CONTACTS_ID_PREFIX = "ID_CONTACTS#"; } \ No newline at end of file diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/UserController.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/UserController.java index 2f97c2da..35a467d1 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/UserController.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/UserController.java @@ -262,7 +262,7 @@ public Multi retrievePaginatedAndFilteredUser(@QueryPar @Produces(MediaType.APPLICATION_JSON) public Uni updateUserRegistryAndSendNotification(@PathParam(value = "id") String userId, @QueryParam(value = "institutionId") String institutionId, - UpdateUserRequest updateUserRequest) { + @Valid UpdateUserRequest updateUserRequest) { return userRegistryService.updateUserRegistry(updateUserRequest, userId, institutionId) .map(ignore -> Response .status(HttpStatus.SC_NO_CONTENT) diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/response/CertifiableFieldResponse.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/response/CertifiableFieldResponse.java index 569188f1..a9952b39 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/response/CertifiableFieldResponse.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/response/CertifiableFieldResponse.java @@ -3,12 +3,11 @@ import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; -import org.openapi.quarkus.user_registry_json.model.CertifiableFieldResourceOfstring; @Data @NoArgsConstructor @AllArgsConstructor public class CertifiableFieldResponse { private T value; - private CertifiableFieldResourceOfstring.CertificationEnum certified; + private String certified; } diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/mapper/UserMapper.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/mapper/UserMapper.java index 789ea1ed..4cbe4f25 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/mapper/UserMapper.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/mapper/UserMapper.java @@ -14,12 +14,15 @@ import org.mapstruct.Mapping; import org.mapstruct.Named; import org.mapstruct.factory.Mappers; -import org.openapi.quarkus.user_registry_json.model.CertifiableFieldResourceOfstring; +import org.openapi.quarkus.user_registry_json.model.BirthDateCertifiableSchema; +import org.openapi.quarkus.user_registry_json.model.EmailCertifiableSchema; +import org.openapi.quarkus.user_registry_json.model.FamilyNameCertifiableSchema; +import org.openapi.quarkus.user_registry_json.model.MobilePhoneCertifiableSchema; import org.openapi.quarkus.user_registry_json.model.MutableUserFieldsDto; +import org.openapi.quarkus.user_registry_json.model.NameCertifiableSchema; +import org.openapi.quarkus.user_registry_json.model.SaveUserDto; import org.openapi.quarkus.user_registry_json.model.UserResource; import org.openapi.quarkus.user_registry_json.model.WorkContactResource; -import org.openapi.quarkus.user_registry_json.model.SaveUserDto; -import org.openapi.quarkus.user_registry_json.model.CertifiableFieldResourceOfLocalDate; import java.time.LocalDate; import java.util.*; @@ -31,15 +34,15 @@ public interface UserMapper { UserNotificationResponse toUserNotification(UserNotificationToSend user); @Mapping(source = "userResource.fiscalCode", target = "taxCode") - @Mapping(source = "userResource.familyName", target = "surname", qualifiedByName = "fromCertifiableString") - @Mapping(source = "userResource.name", target = "name", qualifiedByName = "fromCertifiableString") + @Mapping(source = "userResource.familyName", target = "surname", qualifiedByName = "fromSurnameCertifiableString") + @Mapping(source = "userResource.name", target = "name", qualifiedByName = "fromNameCertifiableString") @Mapping(target = "email", expression = "java(retrieveMailFromWorkContacts(userResource.getWorkContacts(), userMailUuid))") @Mapping(target = "workContacts", expression = "java(toWorkContacts(userResource.getWorkContacts()))") UserResponse toUserResponse(UserResource userResource, String userMailUuid); @Mapping(target = "email", expression = "java(retrieveCertifiedMailFromWorkContacts(userResource, userMailUuid))") - @Mapping(source = "userResource.familyName", target = "familyName", qualifiedByName = "toCertifiableFieldResponse") - @Mapping(source = "userResource.name", target = "name", qualifiedByName = "toCertifiableFieldResponse") + @Mapping(source = "userResource.familyName", target = "familyName", qualifiedByName = "toFamilyNameCertifiableFieldResponse") + @Mapping(source = "userResource.name", target = "name", qualifiedByName = "toNameCertifiableFieldResponse") @Mapping(target = "workContacts", expression = "java(toWorkContactResponse(userResource.getWorkContacts()))") UserDetailResponse toUserDetailResponse(UserResource userResource, String userMailUuid); @@ -55,9 +58,14 @@ default Map toWorkContacts(Map work } - @Named("fromCertifiableString") - default String fromCertifiableString(CertifiableFieldResourceOfstring certifiableFieldResourceOfstring) { - return Optional.ofNullable(certifiableFieldResourceOfstring).map(CertifiableFieldResourceOfstring::getValue).orElse(null); + @Named("fromSurnameCertifiableString") + default String fromCertifiableString(org.openapi.quarkus.user_registry_json.model.FamilyNameCertifiableSchema certifiableFieldResourceOfstring) { + return Optional.ofNullable(certifiableFieldResourceOfstring).map(org.openapi.quarkus.user_registry_json.model.FamilyNameCertifiableSchema::getValue).orElse(null); + } + + @Named("fromNameCertifiableString") + default String fromCertifiableString(NameCertifiableSchema certifiableFieldResourceOfstring) { + return Optional.ofNullable(certifiableFieldResourceOfstring).map(NameCertifiableSchema::getValue).orElse(null); } @Named("toWorkContactResponse") @@ -66,7 +74,7 @@ default Map toWorkContactResponse(Map { WorkContactResponse workContact = new WorkContactResponse(); - workContact.setEmail(toCertifiableFieldResponse(value.getEmail())); + workContact.setEmail(toEmailCertifiableFieldResponse(value.getEmail())); resourceMap.put(key, workContact); }); } @@ -87,71 +95,142 @@ default String retrieveMailFromWorkContacts(Map map @Named("retrieveCertifiedMailFromWorkContacts") default CertifiableFieldResponse retrieveCertifiedMailFromWorkContacts(UserResource userResource, String userMailUuid){ if(userResource.getWorkContacts()!=null && !userResource.getWorkContacts().isEmpty() && userResource.getWorkContacts().containsKey(userMailUuid)){ - return new CertifiableFieldResponse<>(userResource.getWorkContacts().get(userMailUuid).getEmail().getValue(), userResource.getWorkContacts().get(userMailUuid).getEmail().getCertification()); + return new CertifiableFieldResponse<>(userResource.getWorkContacts().get(userMailUuid).getEmail().getValue(), + Optional.ofNullable(userResource.getWorkContacts().get(userMailUuid).getEmail().getCertification()) + .map(EmailCertifiableSchema.CertificationEnum::value).orElse(null)); } return null; } - @Named("toCertifiableFieldResponse") - default CertifiableFieldResponse toCertifiableFieldResponse(CertifiableFieldResourceOfstring resource){ - return Optional.ofNullable(resource).map(r -> new CertifiableFieldResponse<>(r.getValue(), r.getCertification())).orElse(null); + @Named("toNameCertifiableFieldResponse") + default CertifiableFieldResponse toNameCertifiableFieldResponse(NameCertifiableSchema resource){ + return Optional.ofNullable(resource).map(r -> new CertifiableFieldResponse<>(r.getValue(), + Optional.ofNullable(r.getCertification()).map(NameCertifiableSchema.CertificationEnum::value).orElse(null))) + .orElse(null); + } + + @Named("toFamilyNameCertifiableFieldResponse") + default CertifiableFieldResponse toFamilyNameCertifiableFieldResponse(FamilyNameCertifiableSchema resource){ + return Optional.ofNullable(resource).map(r -> new CertifiableFieldResponse<>(r.getValue(), + Optional.ofNullable(r.getCertification()).map(FamilyNameCertifiableSchema.CertificationEnum::value).orElse(null))) + .orElse(null); + } + + @Named("toEmailCertifiableFieldResponse") + default CertifiableFieldResponse toEmailCertifiableFieldResponse(EmailCertifiableSchema resource){ + return Optional.ofNullable(resource).map(r -> new CertifiableFieldResponse<>(r.getValue(), + Optional.ofNullable(r.getCertification()).map(EmailCertifiableSchema.CertificationEnum::value).orElse(null))) + .orElse(null); } MutableUserFieldsDto toMutableUserFieldsDto(UserResource userResource); - @Mapping(target = "familyName", expression = "java(toCertifiableStringNotEquals(userResource.getFamilyName(), updateUserRequest.getFamilyName()))") - @Mapping(target = "name", expression = "java(toCertifiableStringNotEquals(userResource.getName(), updateUserRequest.getName()))") - @Mapping(target = "workContacts", expression = "java(toWorkContact(updateUserRequest.getEmail(), idMail))") + @Mapping(target = "familyName", expression = "java(toFamilyNameCertifiableStringNotEquals(userResource.getFamilyName(), updateUserRequest.getFamilyName()))") + @Mapping(target = "name", expression = "java(toNameCertifiableStringNotEquals(userResource.getName(), updateUserRequest.getName()))") + @Mapping(target = "workContacts", expression = "java(toWorkContact(updateUserRequest.getEmail(), updateUserRequest.getMobilePhone(), idContact))") @Mapping(target = "email", ignore = true) @Mapping(target = "birthDate", ignore = true) - MutableUserFieldsDto toMutableUserFieldsDto(UpdateUserRequest updateUserRequest, UserResource userResource, String idMail); + MutableUserFieldsDto toMutableUserFieldsDto(UpdateUserRequest updateUserRequest, UserResource userResource, String idContact); @Mapping(source = "user.birthDate", target = "birthDate", qualifiedByName = "toCertifiableLocalDate") - @Mapping(source = "user.familyName", target = "familyName", qualifiedByName = "toCertifiableString") - @Mapping(source = "user.name", target = "name", qualifiedByName = "toCertifiableString") + @Mapping(source = "user.familyName", target = "familyName", qualifiedByName = "toFamilyNameCertifiableString") + @Mapping(source = "user.name", target = "name", qualifiedByName = "toNameCertifiableString") @Mapping(source = "user.fiscalCode", target = "fiscalCode") @Mapping(source = "workContactResource", target = "workContacts") SaveUserDto toSaveUserDto(CreateUserDto.User user, Map workContactResource); + @Named("toFamilyNameCertifiableStringNotEquals") + default FamilyNameCertifiableSchema toFamilyNameCertifiableStringNotEquals(FamilyNameCertifiableSchema certifiableString, String value) { + if(StringUtils.isBlank(value) || + Objects.nonNull(certifiableString) && FamilyNameCertifiableSchema.CertificationEnum.SPID.equals(certifiableString.getCertification())){ + return null; + } + + if(Objects.isNull(certifiableString) || !value.equals(certifiableString.getValue())){ + var certifiableFieldResourceOfstring = new FamilyNameCertifiableSchema(); + certifiableFieldResourceOfstring.setValue(value); + certifiableFieldResourceOfstring.setCertification(FamilyNameCertifiableSchema.CertificationEnum.NONE); + return certifiableFieldResourceOfstring; + } + return null; + } + + @Named("toNameCertifiableStringNotEquals") + default NameCertifiableSchema toNameCertifiableStringNotEquals(NameCertifiableSchema certifiableString, String value) { + if(StringUtils.isBlank(value) || + Objects.nonNull(certifiableString) && NameCertifiableSchema.CertificationEnum.SPID.equals(certifiableString.getCertification())){ + return null; + } + + if(Objects.isNull(certifiableString) || !value.equals(certifiableString.getValue())){ + var certifiableFieldResourceOfstring = new NameCertifiableSchema(); + certifiableFieldResourceOfstring.setValue(value); + certifiableFieldResourceOfstring.setCertification(NameCertifiableSchema.CertificationEnum.NONE); + return certifiableFieldResourceOfstring; + } + return null; + } @Named("toWorkContact") - default Map toWorkContact(String email, String idMail){ - if (StringUtils.isNotBlank(idMail) && StringUtils.isNotBlank(email)){ + default Map toWorkContact(String email, String phoneNumber, String idContact){ + if (StringUtils.isNotBlank(idContact)){ WorkContactResource workContactResource = new WorkContactResource(); - workContactResource.setEmail(toCertString(email)); - return Map.of(idMail, workContactResource); + if(StringUtils.isNotBlank(email)){ + workContactResource.setEmail(toMailCertString(email)); + } + if(StringUtils.isNotBlank(phoneNumber)){ + workContactResource.setMobilePhone(toPhoneCertString(phoneNumber)); + } + return Map.of(idContact, workContactResource); } return null; } + @Named("toCertifiableLocalDate") - default CertifiableFieldResourceOfLocalDate toLocalTime(String time) { + default BirthDateCertifiableSchema toLocalTime(String time) { if(Objects.isNull(time)) return null; - var certifiableFieldResourceOfLocalDate = new CertifiableFieldResourceOfLocalDate(); + var certifiableFieldResourceOfLocalDate = new BirthDateCertifiableSchema(); certifiableFieldResourceOfLocalDate.setValue(LocalDate.parse(time)); - certifiableFieldResourceOfLocalDate.setCertification(CertifiableFieldResourceOfLocalDate.CertificationEnum.NONE); + certifiableFieldResourceOfLocalDate.setCertification(BirthDateCertifiableSchema.CertificationEnum.NONE); return certifiableFieldResourceOfLocalDate; } - @Named("toCertifiableString") - default CertifiableFieldResourceOfstring toCertString(String value) { + + default EmailCertifiableSchema toMailCertString(String value) { if (StringUtils.isNotBlank(value)){ - var certifiableFieldResourceOfstring = new CertifiableFieldResourceOfstring(); + var certifiableFieldResourceOfstring = new EmailCertifiableSchema(); certifiableFieldResourceOfstring.setValue(value); - certifiableFieldResourceOfstring.setCertification(CertifiableFieldResourceOfstring.CertificationEnum.NONE); + certifiableFieldResourceOfstring.setCertification(EmailCertifiableSchema.CertificationEnum.NONE); return certifiableFieldResourceOfstring; } return null; } - @Named("toCertifiableStringNotEquals") - default CertifiableFieldResourceOfstring toCertifiableStringNotEquals(CertifiableFieldResourceOfstring certifiableString, String value) { - if(StringUtils.isBlank(value) || - Objects.nonNull(certifiableString) && CertifiableFieldResourceOfstring.CertificationEnum.SPID.equals(certifiableString.getCertification())){ - return null; + default MobilePhoneCertifiableSchema toPhoneCertString(String value){ + if (StringUtils.isNotBlank(value)){ + var certifiableFieldResourceOfstring = new MobilePhoneCertifiableSchema(); + certifiableFieldResourceOfstring.setValue(value); + certifiableFieldResourceOfstring.setCertification(MobilePhoneCertifiableSchema.CertificationEnum.NONE); + return certifiableFieldResourceOfstring; } + return null; + } - if(Objects.isNull(certifiableString) || !value.equals(certifiableString.getValue())){ - var certifiableFieldResourceOfstring = new CertifiableFieldResourceOfstring(); + @Named("toNameCertifiableString") + default NameCertifiableSchema toNameCertString(String value) { + if (StringUtils.isNotBlank(value)){ + var certifiableFieldResourceOfstring = new NameCertifiableSchema(); + certifiableFieldResourceOfstring.setValue(value); + certifiableFieldResourceOfstring.setCertification(NameCertifiableSchema.CertificationEnum.NONE); + return certifiableFieldResourceOfstring; + } + return null; + } + + @Named("toFamilyNameCertifiableString") + default FamilyNameCertifiableSchema toFamilyNameCertString(String value) { + if (StringUtils.isNotBlank(value)){ + var certifiableFieldResourceOfstring = new FamilyNameCertifiableSchema(); certifiableFieldResourceOfstring.setValue(value); - certifiableFieldResourceOfstring.setCertification(CertifiableFieldResourceOfstring.CertificationEnum.NONE); + certifiableFieldResourceOfstring.setCertification(FamilyNameCertifiableSchema.CertificationEnum.NONE); return certifiableFieldResourceOfstring; } return null; diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/model/UpdateUserRequest.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/model/UpdateUserRequest.java index d89f4df9..3b167150 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/model/UpdateUserRequest.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/model/UpdateUserRequest.java @@ -1,12 +1,14 @@ package it.pagopa.selfcare.user.model; -import jakarta.validation.constraints.NotNull; import lombok.Data; +import javax.validation.constraints.Pattern; + @Data public class UpdateUserRequest { private String name; private String familyName; - @NotNull private String email; + @Pattern(regexp = "^\\+?[0-9]{9,15}$", message = "Il numero di telefono non è valido") + private String mobilePhone; } diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserRegistryServiceImpl.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserRegistryServiceImpl.java index 4145e70b..52a3d371 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserRegistryServiceImpl.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserRegistryServiceImpl.java @@ -16,14 +16,21 @@ import org.eclipse.microprofile.rest.client.inject.RestClient; import org.gradle.internal.impldep.org.apache.commons.lang.StringUtils; import org.openapi.quarkus.user_registry_json.api.UserApi; -import org.openapi.quarkus.user_registry_json.model.*; +import org.openapi.quarkus.user_registry_json.model.EmailCertifiableSchema; +import org.openapi.quarkus.user_registry_json.model.MobilePhoneCertifiableSchema; +import org.openapi.quarkus.user_registry_json.model.MutableUserFieldsDto; +import org.openapi.quarkus.user_registry_json.model.SaveUserDto; +import org.openapi.quarkus.user_registry_json.model.UserId; +import org.openapi.quarkus.user_registry_json.model.UserResource; +import org.openapi.quarkus.user_registry_json.model.UserSearchDto; +import org.openapi.quarkus.user_registry_json.model.WorkContactResource; import java.time.Duration; import java.time.OffsetDateTime; import java.util.*; import java.util.concurrent.TimeoutException; -import static it.pagopa.selfcare.user.constant.CollectionUtil.MAIL_ID_PREFIX; +import static it.pagopa.selfcare.user.constant.CollectionUtil.CONTACTS_ID_PREFIX; @ApplicationScoped @@ -53,7 +60,7 @@ public class UserRegistryServiceImpl implements UserRegistryService { public Uni findByIdUsingGET(String fl, String id) { return userRegistryApi.findByIdUsingGET(fl, id) .onFailure(this::checkIfIsRetryableException) - .retry().withBackOff(Duration.ofSeconds(retryMinBackOff), Duration.ofSeconds(retryMaxBackOff)).atMost(maxRetry); + .retry().withBackOff(Duration.ofSeconds(retryMinBackOff), Duration.ofSeconds(retryMaxBackOff)).atMost(maxRetry); } @@ -89,7 +96,7 @@ public Uni> updateUserRegistry(UpdateUserRequest updateUse log.trace("sendUpdateUserNotification start"); log.debug("sendUpdateUserNotification userId = {}, institutionId = {}", userId, institutionId); - if(StringUtils.isBlank(updateUserRequest.getEmail())) + if (StringUtils.isBlank(updateUserRequest.getEmail())) throw new IllegalArgumentException("email updateUserRequest must not be null!"); UserInstitutionFilter userInstitutionFilter = UserInstitutionFilter.builder() @@ -103,13 +110,14 @@ public Uni> updateUserRegistry(UpdateUserRequest updateUse .onItem().ifNotNull().invoke(() -> log.debug("UserInstitution founded for userId: {} and institutionId: {}", userId, institutionId))) .asTuple() .onItem().transformToMulti(tuple -> findMailUuidAndUpdateUserRegistry(tuple.getItem1(), updateUserRequest) - .onItem().transformToMulti(uuidMail -> updateUserInstitution(tuple.getItem2(), uuidMail))) + .onItem().transformToMulti(idContacts -> updateUserInstitution(tuple.getItem2(), idContacts))) .collect().asList() .onItem().invoke(items -> log.trace("update {} users on userRegistry", items.size())); } private Multi updateUserInstitution(List userInstitutions, String mailUuid) { return Multi.createFrom().iterable(userInstitutions.stream() + .filter(userInstitution -> Objects.isNull(userInstitution.getUserMailUuid()) || !userInstitution.getUserMailUuid().equals(mailUuid)) .peek(userInstitution -> { userInstitution.setUserMailUuid(mailUuid); userInstitution.setUserMailUpdatedAt(OffsetDateTime.now()); @@ -121,19 +129,52 @@ private Multi updateUserInstitution(List userI } private Uni findMailUuidAndUpdateUserRegistry(UserResource userResource, UpdateUserRequest userDto) { - Optional mailAlreadyPresent = Optional.empty(); - String idMail = MAIL_ID_PREFIX + UUID.randomUUID(); - - if(Objects.nonNull(userResource.getWorkContacts())) { - mailAlreadyPresent = userResource.getWorkContacts().entrySet().stream() - .filter(entry -> entry.getValue() != null && entry.getValue().getEmail() != null && StringUtils.isNotBlank(entry.getValue().getEmail().getValue()) - && entry.getValue().getEmail().getValue().equalsIgnoreCase(userDto.getEmail()) && entry.getKey().startsWith(MAIL_ID_PREFIX)) - .findFirst() - .map(Map.Entry::getKey); - } + String idContacts = CONTACTS_ID_PREFIX + UUID.randomUUID(); + + String emailToCompare = userDto.getEmail(); + String mobilePhoneToCompare = userDto.getMobilePhone(); + + String existedUserMailUuid = Optional.ofNullable(userResource.getWorkContacts()) + .flatMap(stringWorkContactResourceMap -> stringWorkContactResourceMap.entrySet().stream() + .filter(stringWorkContactResourceEntry -> existsWorkContactResourceForPhoneAndMail(stringWorkContactResourceEntry, emailToCompare, mobilePhoneToCompare)) + .findFirst() + .map(Map.Entry::getKey)) + .orElse(null); return updateUsingPATCH(userResource.getId().toString(), - userMapper.toMutableUserFieldsDto(userDto, userResource, mailAlreadyPresent.isPresent() ? null : idMail)) - .replaceWith(mailAlreadyPresent.orElse(idMail)); + userMapper.toMutableUserFieldsDto(userDto, userResource, idContacts)) + .replaceWith(StringUtils.isBlank(existedUserMailUuid) ? idContacts : existedUserMailUuid); + } + + private static boolean existsWorkContactResourceForPhoneAndMail(Map.Entry stringWorkContactResourceEntry, String emailToCompare, String mobilePhoneToCompare) { + + WorkContactResource workContact = stringWorkContactResourceEntry.getValue(); + if (Objects.nonNull(workContact)) { + boolean isEqualsEmail = StringUtils.isBlank(emailToCompare) ? checkIfWorkContactMailIsNull(workContact.getEmail()) : checkIfWorkContactEmailIsEquals(workContact, emailToCompare); + boolean isEqualsPhone = StringUtils.isBlank(mobilePhoneToCompare) ? checkIfWorkContactPhoneIsNull(workContact.getMobilePhone()) : checkIfWorkContactMobilePhoneIsEquals(workContact, mobilePhoneToCompare); + return isEqualsEmail && isEqualsPhone; + } + return false; + } + + private static boolean checkIfWorkContactMobilePhoneIsEquals(WorkContactResource workContact, String mobilePhoneToCompare) { + return Objects.nonNull(workContact.getMobilePhone()) + && StringUtils.isNotBlank(workContact.getMobilePhone().getValue()) + && workContact.getMobilePhone().getValue().equalsIgnoreCase(mobilePhoneToCompare); + } + + private static boolean checkIfWorkContactPhoneIsNull(MobilePhoneCertifiableSchema mobilePhone) { + return Objects.isNull(mobilePhone) || StringUtils.isBlank(mobilePhone.getValue()); + } + + private static boolean checkIfWorkContactEmailIsEquals(WorkContactResource workContact, String emailToCompare) { + return Objects.nonNull(workContact.getEmail()) + && StringUtils.isNotBlank(workContact.getEmail().getValue()) + && workContact.getEmail().getValue().equalsIgnoreCase(emailToCompare); + + } + + private static boolean checkIfWorkContactMailIsNull(EmailCertifiableSchema email) { + return Objects.isNull(email) || StringUtils.isBlank(email.getValue()); } } diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/util/UserUtils.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/util/UserUtils.java index bbda300c..d1569b18 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/util/UserUtils.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/util/UserUtils.java @@ -22,7 +22,7 @@ import org.apache.http.HttpStatus; import org.gradle.internal.impldep.org.apache.commons.lang.StringUtils; import org.jboss.resteasy.reactive.ClientWebApplicationException; -import org.openapi.quarkus.user_registry_json.model.CertifiableFieldResourceOfstring; +import org.openapi.quarkus.user_registry_json.model.EmailCertifiableSchema; import org.openapi.quarkus.user_registry_json.model.UserResource; import org.openapi.quarkus.user_registry_json.model.WorkContactResource; @@ -129,8 +129,8 @@ public UserInfo filterInstitutionRoles(UserInfo userInfo, String[] states, Strin public static WorkContactResource buildWorkContact(String mail) { return WorkContactResource.builder() - .email(new CertifiableFieldResourceOfstring( - CertifiableFieldResourceOfstring.CertificationEnum.NONE, + .email(new EmailCertifiableSchema( + EmailCertifiableSchema.CertificationEnum.NONE, mail) ).build(); } @@ -158,7 +158,7 @@ public static Optional getMailByMailUuid(Map("email", CertifiableFieldResourceOfstring.CertificationEnum.NONE)); - userDetailResponse.setName(new CertifiableFieldResponse<>("name", CertifiableFieldResourceOfstring.CertificationEnum.NONE)); - userDetailResponse.setFamilyName(new CertifiableFieldResponse<>("familyName", CertifiableFieldResourceOfstring.CertificationEnum.NONE)); + userDetailResponse.setEmail(new CertifiableFieldResponse<>("email", "NONE")); + userDetailResponse.setName(new CertifiableFieldResponse<>("name", "NONE")); + userDetailResponse.setFamilyName(new CertifiableFieldResponse<>("familyName", "NONE")); userDetailResponse.setFiscalCode("fiscalCode"); - userDetailResponse.setWorkContacts(Map.of("userMailUuid", new WorkContactResponse(new CertifiableFieldResponse("email", CertifiableFieldResourceOfstring.CertificationEnum.NONE)))); + userDetailResponse.setWorkContacts(Map.of("userMailUuid", new WorkContactResponse(new CertifiableFieldResponse("email", "NONE")))); } /** diff --git a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/mapper/UserMapperTest.java b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/mapper/UserMapperTest.java index e490bf49..f7a79f11 100644 --- a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/mapper/UserMapperTest.java +++ b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/mapper/UserMapperTest.java @@ -1,14 +1,24 @@ package it.pagopa.selfcare.user.mapper; import io.quarkus.test.junit.QuarkusTest; +import it.pagopa.selfcare.onboarding.common.PartyRole; import it.pagopa.selfcare.user.controller.response.CertifiableFieldResponse; import it.pagopa.selfcare.user.controller.response.WorkContactResponse; +import it.pagopa.selfcare.user.model.OnboardedProduct; +import it.pagopa.selfcare.user.model.constants.OnboardedProductState; import org.junit.jupiter.api.Test; -import org.openapi.quarkus.user_registry_json.model.CertifiableFieldResourceOfstring; +import org.openapi.quarkus.user_registry_json.model.BirthDateCertifiableSchema; +import org.openapi.quarkus.user_registry_json.model.EmailCertifiableSchema; +import org.openapi.quarkus.user_registry_json.model.FamilyNameCertifiableSchema; +import org.openapi.quarkus.user_registry_json.model.MobilePhoneCertifiableSchema; +import org.openapi.quarkus.user_registry_json.model.NameCertifiableSchema; import org.openapi.quarkus.user_registry_json.model.UserResource; import org.openapi.quarkus.user_registry_json.model.WorkContactResource; +import java.time.LocalDate; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import static org.junit.jupiter.api.Assertions.*; @@ -21,14 +31,14 @@ public class UserMapperTest { @Test void retrieveCertifiedEmailFromWorkContacts(){ final UserResource userResource = new UserResource(); - final CertifiableFieldResourceOfstring email = new CertifiableFieldResourceOfstring(CertifiableFieldResourceOfstring.CertificationEnum.NONE, "email"); - WorkContactResource workContactResource = new WorkContactResource(email); + final EmailCertifiableSchema email = new EmailCertifiableSchema(EmailCertifiableSchema.CertificationEnum.NONE, "email"); + WorkContactResource workContactResource = new WorkContactResource(email, null, null); Map workContactResourceMap = Map.of("email", workContactResource); userResource.setWorkContacts(workContactResourceMap); CertifiableFieldResponse certifiedMail = userMapper.retrieveCertifiedMailFromWorkContacts(userResource, "email"); - assertEquals(CertifiableFieldResourceOfstring.CertificationEnum.NONE,certifiedMail.getCertified()); + assertEquals(EmailCertifiableSchema.CertificationEnum.NONE.value(),certifiedMail.getCertified()); assertEquals("email", certifiedMail.getValue()); } @@ -44,8 +54,8 @@ void retrieveCertifiedEmailFromWorkContacts_nullWorkContacts(){ @Test void retrieveCertifiedEmailFromWorkContacts_notPresent(){ final UserResource userResource = new UserResource(); - final CertifiableFieldResourceOfstring email = new CertifiableFieldResourceOfstring(CertifiableFieldResourceOfstring.CertificationEnum.NONE, "email"); - WorkContactResource workContactResource = new WorkContactResource(email); + final EmailCertifiableSchema email = new EmailCertifiableSchema(EmailCertifiableSchema.CertificationEnum.NONE, "email"); + WorkContactResource workContactResource = new WorkContactResource(email, null, null); Map workContactResourceMap = Map.of("email", workContactResource); userResource.setWorkContacts(workContactResourceMap); @@ -57,8 +67,8 @@ void retrieveCertifiedEmailFromWorkContacts_notPresent(){ @Test void retrieveMailFromWorkContacts(){ - final CertifiableFieldResourceOfstring email = new CertifiableFieldResourceOfstring(CertifiableFieldResourceOfstring.CertificationEnum.NONE, "email"); - WorkContactResource workContactResource = new WorkContactResource(email); + final EmailCertifiableSchema email = new EmailCertifiableSchema(EmailCertifiableSchema.CertificationEnum.NONE, "email"); + WorkContactResource workContactResource = new WorkContactResource(email, null, null); Map workContactResourceMap = Map.of("email", workContactResource); String mailFromWorkContact = userMapper.retrieveMailFromWorkContacts(workContactResourceMap, "email"); @@ -86,8 +96,8 @@ void retrieveMailFromWorkContacts_nullMap(){ @Test void retrieveMailFromWorkContacts_notPresent(){ - final CertifiableFieldResourceOfstring email = new CertifiableFieldResourceOfstring(CertifiableFieldResourceOfstring.CertificationEnum.NONE, "email"); - WorkContactResource workContactResource = new WorkContactResource(email); + final EmailCertifiableSchema email = new EmailCertifiableSchema(EmailCertifiableSchema.CertificationEnum.NONE, "email"); + WorkContactResource workContactResource = new WorkContactResource(email, null, null); Map workContactResourceMap = Map.of("email", workContactResource); String mailFromWorkContact = userMapper.retrieveMailFromWorkContacts(workContactResourceMap, "notPresent"); @@ -99,7 +109,7 @@ void retrieveMailFromWorkContacts_notPresent(){ void toWorkContactResponseMap(){ final Map workContactResourceMap = new HashMap<>(); final WorkContactResource workContactResource = new WorkContactResource(); - final CertifiableFieldResourceOfstring email = new CertifiableFieldResourceOfstring(CertifiableFieldResourceOfstring.CertificationEnum.NONE, "email"); + final EmailCertifiableSchema email = new EmailCertifiableSchema(EmailCertifiableSchema.CertificationEnum.NONE, "email"); workContactResource.setEmail(email); workContactResourceMap.put("email", workContactResource); Map responseMap = userMapper.toWorkContactResponse(workContactResourceMap); @@ -121,4 +131,322 @@ void toWorkContactResponseMap_empty(){ assertTrue(responseMap.isEmpty()); } + + @Test + void toWorkContactsValidWorkContactResourceMap() { + Map workContactResourceMap = new HashMap<>(); + EmailCertifiableSchema email = new EmailCertifiableSchema(EmailCertifiableSchema.CertificationEnum.NONE, "email@example.com"); + WorkContactResource workContactResource = new WorkContactResource(email, null, null); + workContactResourceMap.put("contact1", workContactResource); + + Map result = userMapper.toWorkContacts(workContactResourceMap); + + assertEquals(1, result.size()); + assertEquals("email@example.com", result.get("contact1")); + } + + @Test + void toWorkContactswithNullWorkContactResourceMap() { + Map result = userMapper.toWorkContacts(null); + assertTrue(result.isEmpty()); + } + + @Test + void fromSurnameCertifiableString() { + FamilyNameCertifiableSchema certifiableField = new FamilyNameCertifiableSchema(); + certifiableField.setValue("Doe"); + String result = userMapper.fromCertifiableString(certifiableField); + assertEquals("Doe", result); + } + + @Test + void fromSurnameCertifiableStringWithNullCertifiableField() { + String result = userMapper.fromCertifiableString((FamilyNameCertifiableSchema) null); + assertNull(result); + } + + @Test + void fromSurnameCertifiableStringWithNullCertifiableFieldValue() { + org.openapi.quarkus.user_registry_json.model.FamilyNameCertifiableSchema certifiableField = new FamilyNameCertifiableSchema(); + certifiableField.setValue(null); + String result = userMapper.fromCertifiableString(certifiableField); + + assertNull(result); + } + + @Test + void fromNameCertifiableString() { + NameCertifiableSchema certifiableField = new NameCertifiableSchema(); + certifiableField.setValue("John"); + String result = userMapper.fromCertifiableString(certifiableField); + assertEquals("John", result); + } + + @Test + void fromNameCertifiableStringWithNullCertifiableField() { + String result = userMapper.fromCertifiableString((NameCertifiableSchema) null); + assertNull(result); + } + + @Test + void fromNameCertifiableStringWithNullCertifiableFieldValue() { + NameCertifiableSchema certifiableField = new NameCertifiableSchema(); + certifiableField.setValue(null); + String result = userMapper.fromCertifiableString(certifiableField); + assertNull(result); + } + + @Test + void toNameCertifiableFieldResponse() { + NameCertifiableSchema resource = new NameCertifiableSchema(); + resource.setValue("John"); + CertifiableFieldResponse result = userMapper.toNameCertifiableFieldResponse(resource); + + assertNotNull(result); + assertEquals("John", result.getValue()); + } + + @Test + void toNameCertifiableFieldResponseWithNullResource() { + CertifiableFieldResponse result = userMapper.toNameCertifiableFieldResponse(null); + assertNull(result); + } + + @Test + void toNameCertifiableFieldResponseWithNullValue() { + NameCertifiableSchema resource = new NameCertifiableSchema(); + resource.setValue(null); + CertifiableFieldResponse result = userMapper.toNameCertifiableFieldResponse(resource); + + assertNotNull(result); + assertNull(result.getValue()); + } + + @Test + void toFamilyNameCertifiableFieldResponse() { + FamilyNameCertifiableSchema resource = new FamilyNameCertifiableSchema(); + resource.setValue("Doe"); + + CertifiableFieldResponse result = userMapper.toFamilyNameCertifiableFieldResponse(resource); + + assertNotNull(result); + assertEquals("Doe", result.getValue()); + } + + @Test + void toFamilyNameCertifiableFieldResponseWithNullResource() { + CertifiableFieldResponse result = userMapper.toFamilyNameCertifiableFieldResponse(null); + + assertNull(result); + } + + @Test + void toFamilyNameCertifiableFieldResponseWithNullValue() { + FamilyNameCertifiableSchema resource = new FamilyNameCertifiableSchema(); + resource.setValue(null); + CertifiableFieldResponse result = userMapper.toFamilyNameCertifiableFieldResponse(resource); + + assertNotNull(result); + assertNull(result.getValue()); + } + + @Test + void toFamilyNameCertifiableStringNotEqualsWithSameValue() { + FamilyNameCertifiableSchema certifiableString = new FamilyNameCertifiableSchema(); + certifiableString.setValue("Doe"); + + FamilyNameCertifiableSchema result = userMapper.toFamilyNameCertifiableStringNotEquals(certifiableString, "Doe"); + + assertNull(result); + } + + @Test + void toFamilyNameCertifiableStringNotEqualsWithDifferentValue() { + FamilyNameCertifiableSchema certifiableString = new FamilyNameCertifiableSchema(); + certifiableString.setValue("Smith"); + + FamilyNameCertifiableSchema result = userMapper.toFamilyNameCertifiableStringNotEquals(certifiableString, "Doe"); + + assertNotNull(result); + assertEquals("Doe", result.getValue()); + assertEquals(FamilyNameCertifiableSchema.CertificationEnum.NONE, result.getCertification()); + } + + @Test + void toNameCertifiableStringNotEqualsWithBlankValue() { + NameCertifiableSchema certifiableString = new NameCertifiableSchema(); + certifiableString.setValue("John"); + + NameCertifiableSchema result = userMapper.toNameCertifiableStringNotEquals(certifiableString, " "); + + assertNull(result); + } + + @Test + void toNameCertifiableStringNotEqualsWithSameValue() { + NameCertifiableSchema certifiableString = new NameCertifiableSchema(); + certifiableString.setValue("John"); + + NameCertifiableSchema result = userMapper.toNameCertifiableStringNotEquals(certifiableString, "John"); + + assertNull(result); + } + + @Test + void toNameCertifiableStringNotEqualsWithDifferentValue() { + NameCertifiableSchema certifiableString = new NameCertifiableSchema(); + certifiableString.setValue("Smith"); + + NameCertifiableSchema result = userMapper.toNameCertifiableStringNotEquals(certifiableString, "John"); + + assertNotNull(result); + assertEquals("John", result.getValue()); + assertEquals(NameCertifiableSchema.CertificationEnum.NONE, result.getCertification()); + } + + @Test + void toWorkContact() { + Map result = userMapper.toWorkContact("email@example.com", "1234567890", "contact1"); + + assertNotNull(result); + assertEquals(1, result.size()); + assertEquals("email@example.com", result.get("contact1").getEmail().getValue()); + assertEquals("1234567890", result.get("contact1").getMobilePhone().getValue()); + } + + @Test + void toWorkContactWithBlankIdContact() { + Map result = userMapper.toWorkContact("email@example.com", "1234567890", " "); + assertNull(result); + } + + @Test + void toWorkContactWithNullEmailAndPhoneNumber() { + Map result = userMapper.toWorkContact(null, null, "contact1"); + + assertNotNull(result); + assertEquals(1, result.size()); + assertNull(result.get("contact1").getEmail()); + assertNull(result.get("contact1").getMobilePhone()); + } + + @Test + void toCertifiableLocalDate() { + BirthDateCertifiableSchema result = userMapper.toLocalTime("2000-01-01"); + + assertNotNull(result); + assertEquals(LocalDate.of(2000, 1, 1), result.getValue()); + assertEquals(BirthDateCertifiableSchema.CertificationEnum.NONE, result.getCertification()); + } + + @Test + void toCertifiableLocalDateWithNullTime() { + BirthDateCertifiableSchema result = userMapper.toLocalTime(null); + assertNull(result); + } + + @Test + void toMailCertString() { + EmailCertifiableSchema result = userMapper.toMailCertString("email@example.com"); + + assertNotNull(result); + assertEquals("email@example.com", result.getValue()); + assertEquals(EmailCertifiableSchema.CertificationEnum.NONE, result.getCertification()); + } + + @Test + void toMailCertStringWithBlankValue() { + EmailCertifiableSchema result = userMapper.toMailCertString(" "); + + assertNull(result); + } + + @Test + void toPhoneCertString() { + MobilePhoneCertifiableSchema result = userMapper.toPhoneCertString("1234567890"); + + assertNotNull(result); + assertEquals("1234567890", result.getValue()); + assertEquals(MobilePhoneCertifiableSchema.CertificationEnum.NONE, result.getCertification()); + } + + @Test + void toPhoneCertStringWithBlankValue() { + MobilePhoneCertifiableSchema result = userMapper.toPhoneCertString(" "); + assertNull(result); + } + + @Test + void toNameCertString() { + NameCertifiableSchema result = userMapper.toNameCertString("John"); + + assertNotNull(result); + assertEquals("John", result.getValue()); + assertEquals(NameCertifiableSchema.CertificationEnum.NONE, result.getCertification()); + } + + @Test + void toNameCertStringWithBlankValue() { + NameCertifiableSchema result = userMapper.toNameCertString(" "); + + assertNull(result); + } + + @Test + void toFamilyNameCertString() { + FamilyNameCertifiableSchema result = userMapper.toFamilyNameCertString("Doe"); + + assertNotNull(result); + assertEquals("Doe", result.getValue()); + assertEquals(FamilyNameCertifiableSchema.CertificationEnum.NONE, result.getCertification()); + } + + @Test + void toFamilyNameCertStringWithBlankValue() { + FamilyNameCertifiableSchema result = userMapper.toFamilyNameCertString(" "); + assertNull(result); + } + + @Test + void getMaxStatusWithEmptyList() { + List onboardedProductList = new ArrayList<>(); + + String result = userMapper.getMaxStatus(onboardedProductList); + + assertNull(result); + } + + @Test + void getMaxStatusWithNonEmptyList() { + OnboardedProduct product1 = new OnboardedProduct(); + product1.setStatus(OnboardedProductState.ACTIVE); + OnboardedProduct product2 = new OnboardedProduct(); + product2.setStatus(OnboardedProductState.SUSPENDED); + List onboardedProductList = List.of(product1, product2); + + String result = userMapper.getMaxStatus(onboardedProductList); + + assertEquals(OnboardedProductState.ACTIVE.name(), result); + } + + @Test + void getMaxRoleWithEmptyList() { + List onboardedProductList = new ArrayList<>(); + String result = userMapper.getMaxRole(onboardedProductList); + + assertNull(result); + } + + @Test + void getMaxRoleWithNonEmptyList() { + OnboardedProduct product1 = new OnboardedProduct(); + product1.setRole(PartyRole.MANAGER); + OnboardedProduct product2 = new OnboardedProduct(); + product2.setRole(PartyRole.DELEGATE); + List onboardedProductList = List.of(product1, product2); + + String result = userMapper.getMaxRole(onboardedProductList); + + assertEquals(PartyRole.MANAGER.name(), result); + } } diff --git a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserNotificationServiceImplTest.java b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserNotificationServiceImplTest.java index 3ecfe9a4..02b6564f 100644 --- a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserNotificationServiceImplTest.java +++ b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserNotificationServiceImplTest.java @@ -24,7 +24,8 @@ import org.eclipse.microprofile.rest.client.inject.RestClient; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; -import org.openapi.quarkus.user_registry_json.model.CertifiableFieldResourceOfstring; +import org.openapi.quarkus.user_registry_json.model.EmailCertifiableSchema; +import org.openapi.quarkus.user_registry_json.model.NameCertifiableSchema; import org.openapi.quarkus.user_registry_json.model.UserResource; import org.openapi.quarkus.user_registry_json.model.WorkContactResource; @@ -68,12 +69,14 @@ Configuration freemarkerConfig() { static { userResource = new UserResource(); userResource.setId(UUID.randomUUID()); - CertifiableFieldResourceOfstring certifiedName = new CertifiableFieldResourceOfstring(); + NameCertifiableSchema certifiedName = new NameCertifiableSchema(); + org.openapi.quarkus.user_registry_json.model.FamilyNameCertifiableSchema certifiedFamilyName = new org.openapi.quarkus.user_registry_json.model.FamilyNameCertifiableSchema(); certifiedName.setValue("name"); + certifiedFamilyName.setValue("familyName"); userResource.setName(certifiedName); - userResource.setFamilyName(certifiedName); + userResource.setFamilyName(certifiedFamilyName); userResource.setFiscalCode("taxCode"); - CertifiableFieldResourceOfstring certifiedEmail = new CertifiableFieldResourceOfstring(); + EmailCertifiableSchema certifiedEmail = new EmailCertifiableSchema(); certifiedEmail.setValue("test@test.it"); WorkContactResource workContactResource = new WorkContactResource(); workContactResource.setEmail(certifiedEmail); diff --git a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserRegistryServiceTest.java b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserRegistryServiceTest.java index 03af02b8..2454796b 100644 --- a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserRegistryServiceTest.java +++ b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserRegistryServiceTest.java @@ -9,7 +9,9 @@ import io.smallrye.mutiny.helpers.test.UniAssertSubscriber; import io.smallrye.reactive.messaging.memory.InMemoryConnector; import it.pagopa.selfcare.user.entity.UserInstitution; -import it.pagopa.selfcare.user.model.*; +import it.pagopa.selfcare.user.model.OnboardedProduct; +import it.pagopa.selfcare.user.model.UpdateUserRequest; +import it.pagopa.selfcare.user.model.UserNotificationToSend; import jakarta.inject.Inject; import jakarta.ws.rs.core.Response; import org.bson.types.ObjectId; @@ -17,7 +19,10 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; +import org.openapi.quarkus.user_registry_json.model.EmailCertifiableSchema; +import org.openapi.quarkus.user_registry_json.model.FamilyNameCertifiableSchema; import org.openapi.quarkus.user_registry_json.model.MutableUserFieldsDto; +import org.openapi.quarkus.user_registry_json.model.NameCertifiableSchema; import org.openapi.quarkus.user_registry_json.model.SaveUserDto; import org.openapi.quarkus.user_registry_json.model.UserId; import org.openapi.quarkus.user_registry_json.model.UserResource; @@ -28,8 +33,6 @@ import java.util.UUID; import static io.smallrye.common.constraint.Assert.assertNotNull; -import static it.pagopa.selfcare.user.model.constants.OnboardedProductState.ACTIVE; -import static it.pagopa.selfcare.user.model.constants.OnboardedProductState.DELETED; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; @@ -59,15 +62,20 @@ public class UserRegistryServiceTest { static { userResource = new org.openapi.quarkus.user_registry_json.model.UserResource(); userResource.setId(UUID.randomUUID()); - org.openapi.quarkus.user_registry_json.model.CertifiableFieldResourceOfstring certifiedName = new org.openapi.quarkus.user_registry_json.model.CertifiableFieldResourceOfstring(); + org.openapi.quarkus.user_registry_json.model.NameCertifiableSchema certifiedName = new NameCertifiableSchema(); certifiedName.setValue("name"); userResource.setName(certifiedName); - userResource.setFamilyName(certifiedName); + FamilyNameCertifiableSchema certifiedFamilyName = new FamilyNameCertifiableSchema(); + certifiedFamilyName.setValue("familyName"); + userResource.setFamilyName(certifiedFamilyName); userResource.setFiscalCode("taxCode"); - org.openapi.quarkus.user_registry_json.model.CertifiableFieldResourceOfstring certifiedEmail = new org.openapi.quarkus.user_registry_json.model.CertifiableFieldResourceOfstring(); + EmailCertifiableSchema certifiedEmail = new EmailCertifiableSchema(); certifiedEmail.setValue(userMailDefault); + org.openapi.quarkus.user_registry_json.model.MobilePhoneCertifiableSchema certifiedMobilePhone = new org.openapi.quarkus.user_registry_json.model.MobilePhoneCertifiableSchema(); + certifiedMobilePhone.setValue("12345678912"); org.openapi.quarkus.user_registry_json.model.WorkContactResource workContactResource = new org.openapi.quarkus.user_registry_json.model.WorkContactResource(); workContactResource.setEmail(certifiedEmail); + workContactResource.setMobilePhone(certifiedMobilePhone); userResource.setEmail(certifiedEmail); userResource.setWorkContacts(Map.of(userMailUuidDefault, workContactResource)); @@ -137,6 +145,7 @@ void updateUserRegistryAndSendNotificationToQueue_whenUserRegistryNothingToUpdat UpdateUserRequest updateUserRequest = new UpdateUserRequest(); updateUserRequest.setName(userResource.getName().getValue()); updateUserRequest.setEmail(userMailDefault); + updateUserRequest.setMobilePhone(userResource.getWorkContacts().get(userMailUuidDefault).getMobilePhone().getValue()); when(userInstitutionService.findAllWithFilter(anyMap())).thenReturn(Multi.createFrom().item(userInstitution)); when(userInstitutionService.persistOrUpdate(any(UserInstitution.class))).thenReturn(Uni.createFrom().item(userInstitution)); @@ -146,16 +155,6 @@ void updateUserRegistryAndSendNotificationToQueue_whenUserRegistryNothingToUpdat UniAssertSubscriber> subscriber = userRegistryService.updateUserRegistry(updateUserRequest, userId, institutionId) .subscribe().withSubscriber(UniAssertSubscriber.create()); subscriber.assertCompleted(); - - ArgumentCaptor userFieldsDtoArgumentCaptor = ArgumentCaptor.forClass(MutableUserFieldsDto.class); - verify(userRegistryApi, times(1)) - .updateUsingPATCH(eq(userId), userFieldsDtoArgumentCaptor.capture()); - assertNull(userFieldsDtoArgumentCaptor.getValue().getName()); - - ArgumentCaptor userInstitutionArgumentCaptor = ArgumentCaptor.forClass(UserInstitution.class); - verify(userInstitutionService, times(1)) - .persistOrUpdate(userInstitutionArgumentCaptor.capture()); - assertEquals(userMailUuidDefault, userInstitutionArgumentCaptor.getValue().getUserMailUuid()); } @Test @@ -218,6 +217,7 @@ void testSendUpdateUserNotificationToQueue2() { UpdateUserRequest updateUserRequest = new UpdateUserRequest(); updateUserRequest.setEmail("test@test.it"); + updateUserRequest.setMobilePhone("12345678912"); when(userInstitutionService.persistOrUpdate(any(UserInstitution.class))).thenReturn(Uni.createFrom().item(userInstitution)); when(userInstitutionService.findAllWithFilter(anyMap())).thenReturn(Multi.createFrom().item(userInstitution)); when(userRegistryApi.updateUsingPATCH(eq("userId"), any(MutableUserFieldsDto.class))).thenReturn(Uni.createFrom().item(Response.accepted().build())); diff --git a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserServiceTest.java b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserServiceTest.java index 8199cb74..8376c0ef 100644 --- a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserServiceTest.java +++ b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserServiceTest.java @@ -48,8 +48,10 @@ import org.mockito.ArgumentCaptor; import org.mockito.Mockito; import org.mockito.Spy; -import org.openapi.quarkus.user_registry_json.model.CertifiableFieldResourceOfLocalDate; -import org.openapi.quarkus.user_registry_json.model.CertifiableFieldResourceOfstring; +import org.openapi.quarkus.user_registry_json.model.BirthDateCertifiableSchema; +import org.openapi.quarkus.user_registry_json.model.EmailCertifiableSchema; +import org.openapi.quarkus.user_registry_json.model.FamilyNameCertifiableSchema; +import org.openapi.quarkus.user_registry_json.model.NameCertifiableSchema; import org.openapi.quarkus.user_registry_json.model.UserResource; import org.openapi.quarkus.user_registry_json.model.UserSearchDto; import org.openapi.quarkus.user_registry_json.model.WorkContactResource; @@ -108,12 +110,14 @@ class UserServiceTest { static { userResource = new UserResource(); userResource.setId(userId); - CertifiableFieldResourceOfstring certifiedName = new CertifiableFieldResourceOfstring(); + NameCertifiableSchema certifiedName = new NameCertifiableSchema(); + FamilyNameCertifiableSchema certifiedSurname = new FamilyNameCertifiableSchema(); + certifiedSurname.setValue("surname"); certifiedName.setValue("name"); userResource.setName(certifiedName); - userResource.setFamilyName(certifiedName); + userResource.setFamilyName(certifiedSurname); userResource.setFiscalCode("taxCode"); - CertifiableFieldResourceOfstring certifiedEmail = new CertifiableFieldResourceOfstring(); + org.openapi.quarkus.user_registry_json.model.EmailCertifiableSchema certifiedEmail = new EmailCertifiableSchema(); certifiedEmail.setValue("test@test.it"); WorkContactResource workContactResource = new WorkContactResource(); workContactResource.setEmail(certifiedEmail); @@ -309,10 +313,10 @@ UserResource dummyUserResource() { UserResource userResource = new UserResource(); userResource.setId(UUID.randomUUID()); userResource.setFiscalCode("test"); - userResource.setBirthDate(CertifiableFieldResourceOfLocalDate.builder().value(LocalDate.now()).build()); - userResource.setEmail(CertifiableFieldResourceOfstring.builder().value("test@test.com").build()); - userResource.setName(CertifiableFieldResourceOfstring.builder().value("testName").build()); - userResource.setFamilyName(CertifiableFieldResourceOfstring.builder().value("testFamilyName").build()); + userResource.setBirthDate(BirthDateCertifiableSchema.builder().value(LocalDate.now()).build()); + userResource.setEmail(EmailCertifiableSchema.builder().value("test@test.com").build()); + userResource.setName(NameCertifiableSchema.builder().value("testName").build()); + userResource.setFamilyName(FamilyNameCertifiableSchema.builder().value("testFamilyName").build()); return userResource; } @@ -325,7 +329,7 @@ void testRetrievePerson() { UserResource userResource = dummyUserResource(); WorkContactResource workContactResource = new WorkContactResource(); - workContactResource.setEmail(CertifiableFieldResourceOfstring.builder().value("userMail").build()); + workContactResource.setEmail(EmailCertifiableSchema.builder().value("userMail").build()); userResource.setWorkContacts(Map.of(userMailUuId, workContactResource)); when(userInstitutionService.retrieveFirstFilteredUserInstitution(any())).thenReturn(Uni.createFrom().item(createUserInstitution())); diff --git a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/util/UserUtilTest.java b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/util/UserUtilTest.java index 2701311e..b7fe063a 100644 --- a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/util/UserUtilTest.java +++ b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/util/UserUtilTest.java @@ -22,7 +22,8 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.mockito.Mockito; -import org.openapi.quarkus.user_registry_json.model.CertifiableFieldResourceOfstring; +import org.openapi.quarkus.user_registry_json.model.EmailCertifiableSchema; +import org.openapi.quarkus.user_registry_json.model.NameCertifiableSchema; import org.openapi.quarkus.user_registry_json.model.UserResource; import org.openapi.quarkus.user_registry_json.model.WorkContactResource; @@ -162,7 +163,7 @@ void testBuildWorkContact() { assertNotNull(workContact); assertEquals(mail, workContact.getEmail().getValue()); - assertEquals(CertifiableFieldResourceOfstring.CertificationEnum.NONE, workContact.getEmail().getCertification()); + assertEquals(EmailCertifiableSchema.CertificationEnum.NONE, workContact.getEmail().getCertification()); } @Test @@ -213,11 +214,11 @@ void testGetMailUuidFromMail() { String email = "test@example.com"; Map workContacts = new HashMap<>(); WorkContactResource workContact1 = new WorkContactResource(); - workContact1.setEmail(new CertifiableFieldResourceOfstring()); + workContact1.setEmail(new EmailCertifiableSchema()); workContact1.getEmail().setValue(email); workContacts.put(MAIL_ID_PREFIX + "mail1", workContact1); WorkContactResource workContact2 = new WorkContactResource(); - workContact2.setEmail(new CertifiableFieldResourceOfstring()); + workContact2.setEmail(new EmailCertifiableSchema()); workContact2.getEmail().setValue("another@example.com"); workContacts.put("mail2", workContact2); @@ -232,12 +233,12 @@ void testGetMailUuidFromMail() { private static UserResource getUserResource(UUID uuid) { Map map = new HashMap<>(); WorkContactResource workContactResource = new WorkContactResource(); - workContactResource.setEmail(CertifiableFieldResourceOfstring.builder().value("test@test.it").build()); + workContactResource.setEmail(EmailCertifiableSchema.builder().value("test@test.it").build()); map.put("MAIL_ID#123", workContactResource); return UserResource.builder() .id(uuid) - .name(CertifiableFieldResourceOfstring.builder().value("name").build()) - .familyName(CertifiableFieldResourceOfstring.builder().value("familyName").build()) + .name(NameCertifiableSchema.builder().value("name").build()) + .familyName(org.openapi.quarkus.user_registry_json.model.FamilyNameCertifiableSchema.builder().value("familyName").build()) .workContacts(map) .build(); } From 4fe637b28527769d37d46cc331903a7413a97bc6 Mon Sep 17 00:00:00 2001 From: flaminiaScarciofolo Date: Thu, 28 Nov 2024 11:44:13 +0100 Subject: [PATCH 2/4] [SELC-6087] Restore CertificationEnum in UserDataResponse, refactor method to verify email and mobilePhone --- apps/user-ms/src/main/docs/openapi.json | 6 +++- apps/user-ms/src/main/docs/openapi.yaml | 7 ++++- .../user/constant/CertificationEnum.java | 6 ++++ .../response/CertifiableFieldResponse.java | 3 +- .../selfcare/user/mapper/UserMapper.java | 19 +++++++++--- .../user/service/UserRegistryServiceImpl.java | 30 +++++++++---------- .../user/controller/UserControllerTest.java | 9 +++--- .../selfcare/user/mapper/UserMapperTest.java | 3 +- 8 files changed, 56 insertions(+), 27 deletions(-) create mode 100644 apps/user-ms/src/main/java/it/pagopa/selfcare/user/constant/CertificationEnum.java diff --git a/apps/user-ms/src/main/docs/openapi.json b/apps/user-ms/src/main/docs/openapi.json index a64081a8..b9a4306b 100644 --- a/apps/user-ms/src/main/docs/openapi.json +++ b/apps/user-ms/src/main/docs/openapi.json @@ -1387,10 +1387,14 @@ "type" : "string" }, "certified" : { - "type" : "string" + "$ref" : "#/components/schemas/CertificationEnum" } } }, + "CertificationEnum" : { + "enum" : [ "NONE", "SPID" ], + "type" : "string" + }, "CreateUserDto" : { "required" : [ "institutionId", "user", "product" ], "type" : "object", diff --git a/apps/user-ms/src/main/docs/openapi.yaml b/apps/user-ms/src/main/docs/openapi.yaml index a173286a..3745414c 100644 --- a/apps/user-ms/src/main/docs/openapi.yaml +++ b/apps/user-ms/src/main/docs/openapi.yaml @@ -993,7 +993,12 @@ components: value: type: string certified: - type: string + $ref: "#/components/schemas/CertificationEnum" + CertificationEnum: + enum: + - NONE + - SPID + type: string CreateUserDto: required: - institutionId diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/constant/CertificationEnum.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/constant/CertificationEnum.java new file mode 100644 index 00000000..21005531 --- /dev/null +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/constant/CertificationEnum.java @@ -0,0 +1,6 @@ +package it.pagopa.selfcare.user.constant; + +public enum CertificationEnum { + NONE, + SPID +} diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/response/CertifiableFieldResponse.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/response/CertifiableFieldResponse.java index a9952b39..e8e7f248 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/response/CertifiableFieldResponse.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/response/CertifiableFieldResponse.java @@ -1,5 +1,6 @@ package it.pagopa.selfcare.user.controller.response; +import it.pagopa.selfcare.user.constant.CertificationEnum; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @@ -9,5 +10,5 @@ @AllArgsConstructor public class CertifiableFieldResponse { private T value; - private String certified; + private CertificationEnum certified; } diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/mapper/UserMapper.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/mapper/UserMapper.java index 4cbe4f25..3e2d7288 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/mapper/UserMapper.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/mapper/UserMapper.java @@ -1,10 +1,12 @@ package it.pagopa.selfcare.user.mapper; import it.pagopa.selfcare.onboarding.common.PartyRole; +import it.pagopa.selfcare.user.constant.CertificationEnum; import it.pagopa.selfcare.user.controller.request.CreateUserDto; import it.pagopa.selfcare.user.controller.response.*; import it.pagopa.selfcare.user.entity.UserInfo; import it.pagopa.selfcare.user.entity.UserInstitution; +import it.pagopa.selfcare.user.exception.InvalidRequestException; import it.pagopa.selfcare.user.model.OnboardedProduct; import it.pagopa.selfcare.user.model.UpdateUserRequest; import it.pagopa.selfcare.user.model.UserNotificationToSend; @@ -97,7 +99,7 @@ default CertifiableFieldResponse retrieveCertifiedMailFromWorkContacts(U if(userResource.getWorkContacts()!=null && !userResource.getWorkContacts().isEmpty() && userResource.getWorkContacts().containsKey(userMailUuid)){ return new CertifiableFieldResponse<>(userResource.getWorkContacts().get(userMailUuid).getEmail().getValue(), Optional.ofNullable(userResource.getWorkContacts().get(userMailUuid).getEmail().getCertification()) - .map(EmailCertifiableSchema.CertificationEnum::value).orElse(null)); + .map(certificationEnum -> mapToCertificationEnum(certificationEnum.value())).orElse(null)); } return null; } @@ -105,23 +107,32 @@ default CertifiableFieldResponse retrieveCertifiedMailFromWorkContacts(U @Named("toNameCertifiableFieldResponse") default CertifiableFieldResponse toNameCertifiableFieldResponse(NameCertifiableSchema resource){ return Optional.ofNullable(resource).map(r -> new CertifiableFieldResponse<>(r.getValue(), - Optional.ofNullable(r.getCertification()).map(NameCertifiableSchema.CertificationEnum::value).orElse(null))) + Optional.ofNullable(r.getCertification()).map(certificationEnum -> mapToCertificationEnum(certificationEnum.value())).orElse(null))) .orElse(null); } @Named("toFamilyNameCertifiableFieldResponse") default CertifiableFieldResponse toFamilyNameCertifiableFieldResponse(FamilyNameCertifiableSchema resource){ return Optional.ofNullable(resource).map(r -> new CertifiableFieldResponse<>(r.getValue(), - Optional.ofNullable(r.getCertification()).map(FamilyNameCertifiableSchema.CertificationEnum::value).orElse(null))) + Optional.ofNullable(r.getCertification()).map(certificationEnum -> mapToCertificationEnum(certificationEnum.value())).orElse(null))) .orElse(null); } @Named("toEmailCertifiableFieldResponse") default CertifiableFieldResponse toEmailCertifiableFieldResponse(EmailCertifiableSchema resource){ return Optional.ofNullable(resource).map(r -> new CertifiableFieldResponse<>(r.getValue(), - Optional.ofNullable(r.getCertification()).map(EmailCertifiableSchema.CertificationEnum::value).orElse(null))) + Optional.ofNullable(r.getCertification()).map(certificationEnum -> mapToCertificationEnum(certificationEnum.value())).orElse(null))) .orElse(null); } + + default CertificationEnum mapToCertificationEnum(String certificationEnum){ + return switch (certificationEnum) { + case "SPID" -> CertificationEnum.SPID; + case "NONE" -> CertificationEnum.NONE; + default -> throw new InvalidRequestException("Invalid certificationEnum"); + }; + } + MutableUserFieldsDto toMutableUserFieldsDto(UserResource userResource); @Mapping(target = "familyName", expression = "java(toFamilyNameCertifiableStringNotEquals(userResource.getFamilyName(), updateUserRequest.getFamilyName()))") diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserRegistryServiceImpl.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserRegistryServiceImpl.java index 52a3d371..ad141d0c 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserRegistryServiceImpl.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserRegistryServiceImpl.java @@ -149,12 +149,21 @@ private Uni findMailUuidAndUpdateUserRegistry(UserResource userResource, private static boolean existsWorkContactResourceForPhoneAndMail(Map.Entry stringWorkContactResourceEntry, String emailToCompare, String mobilePhoneToCompare) { WorkContactResource workContact = stringWorkContactResourceEntry.getValue(); - if (Objects.nonNull(workContact)) { - boolean isEqualsEmail = StringUtils.isBlank(emailToCompare) ? checkIfWorkContactMailIsNull(workContact.getEmail()) : checkIfWorkContactEmailIsEquals(workContact, emailToCompare); - boolean isEqualsPhone = StringUtils.isBlank(mobilePhoneToCompare) ? checkIfWorkContactPhoneIsNull(workContact.getMobilePhone()) : checkIfWorkContactMobilePhoneIsEquals(workContact, mobilePhoneToCompare); - return isEqualsEmail && isEqualsPhone; - } - return false; + return Optional.ofNullable(workContact) + .map(workContactResource -> isEqualsEmail(workContactResource, emailToCompare) && isEqualsPhone(workContactResource, mobilePhoneToCompare)) + .orElse(false); + } + + private static boolean isEqualsPhone(WorkContactResource workContact, String mobilePhoneToCompare) { + return Optional.ofNullable(mobilePhoneToCompare) + .map(phone -> checkIfWorkContactMobilePhoneIsEquals(workContact, phone)) + .orElse(Objects.isNull(workContact.getMobilePhone()) || StringUtils.isBlank(workContact.getMobilePhone().getValue())); + } + + private static Boolean isEqualsEmail(WorkContactResource workContact, String emailToCompare) { + return Optional.ofNullable(emailToCompare) + .map(email -> checkIfWorkContactEmailIsEquals(workContact, email)) + .orElse(Objects.isNull(workContact.getEmail()) || StringUtils.isBlank(workContact.getEmail().getValue())); } private static boolean checkIfWorkContactMobilePhoneIsEquals(WorkContactResource workContact, String mobilePhoneToCompare) { @@ -163,18 +172,9 @@ private static boolean checkIfWorkContactMobilePhoneIsEquals(WorkContactResource && workContact.getMobilePhone().getValue().equalsIgnoreCase(mobilePhoneToCompare); } - private static boolean checkIfWorkContactPhoneIsNull(MobilePhoneCertifiableSchema mobilePhone) { - return Objects.isNull(mobilePhone) || StringUtils.isBlank(mobilePhone.getValue()); - } - private static boolean checkIfWorkContactEmailIsEquals(WorkContactResource workContact, String emailToCompare) { return Objects.nonNull(workContact.getEmail()) && StringUtils.isNotBlank(workContact.getEmail().getValue()) && workContact.getEmail().getValue().equalsIgnoreCase(emailToCompare); - - } - - private static boolean checkIfWorkContactMailIsNull(EmailCertifiableSchema email) { - return Objects.isNull(email) || StringUtils.isBlank(email.getValue()); } } diff --git a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/controller/UserControllerTest.java b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/controller/UserControllerTest.java index 6ad83cdb..8a588fbe 100644 --- a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/controller/UserControllerTest.java +++ b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/controller/UserControllerTest.java @@ -8,6 +8,7 @@ import io.smallrye.mutiny.Multi; import io.smallrye.mutiny.Uni; import it.pagopa.selfcare.onboarding.common.PartyRole; +import it.pagopa.selfcare.user.constant.CertificationEnum; import it.pagopa.selfcare.user.controller.request.AddUserRoleDto; import it.pagopa.selfcare.user.controller.request.CreateUserDto; import it.pagopa.selfcare.user.controller.response.*; @@ -62,11 +63,11 @@ class UserControllerTest { userDetailResponse = new UserDetailResponse(); userDetailResponse.setId(UUID.randomUUID().toString()); - userDetailResponse.setEmail(new CertifiableFieldResponse<>("email", "NONE")); - userDetailResponse.setName(new CertifiableFieldResponse<>("name", "NONE")); - userDetailResponse.setFamilyName(new CertifiableFieldResponse<>("familyName", "NONE")); + userDetailResponse.setEmail(new CertifiableFieldResponse<>("email", CertificationEnum.SPID)); + userDetailResponse.setName(new CertifiableFieldResponse<>("name", CertificationEnum.SPID)); + userDetailResponse.setFamilyName(new CertifiableFieldResponse<>("familyName", CertificationEnum.SPID)); userDetailResponse.setFiscalCode("fiscalCode"); - userDetailResponse.setWorkContacts(Map.of("userMailUuid", new WorkContactResponse(new CertifiableFieldResponse("email", "NONE")))); + userDetailResponse.setWorkContacts(Map.of("userMailUuid", new WorkContactResponse(new CertifiableFieldResponse("email", CertificationEnum.SPID)))); } /** diff --git a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/mapper/UserMapperTest.java b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/mapper/UserMapperTest.java index f7a79f11..fb4f296b 100644 --- a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/mapper/UserMapperTest.java +++ b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/mapper/UserMapperTest.java @@ -2,6 +2,7 @@ import io.quarkus.test.junit.QuarkusTest; import it.pagopa.selfcare.onboarding.common.PartyRole; +import it.pagopa.selfcare.user.constant.CertificationEnum; import it.pagopa.selfcare.user.controller.response.CertifiableFieldResponse; import it.pagopa.selfcare.user.controller.response.WorkContactResponse; import it.pagopa.selfcare.user.model.OnboardedProduct; @@ -38,7 +39,7 @@ void retrieveCertifiedEmailFromWorkContacts(){ CertifiableFieldResponse certifiedMail = userMapper.retrieveCertifiedMailFromWorkContacts(userResource, "email"); - assertEquals(EmailCertifiableSchema.CertificationEnum.NONE.value(),certifiedMail.getCertified()); + assertEquals(CertificationEnum.NONE,certifiedMail.getCertified()); assertEquals("email", certifiedMail.getValue()); } From 02e128df62157047d42b61c5b1a3563595f2f071 Mon Sep 17 00:00:00 2001 From: flaminiaScarciofolo Date: Thu, 28 Nov 2024 14:13:50 +0100 Subject: [PATCH 3/4] [SELC-6087] merge main in feature branch --- .../selfcare/user/mapper/UserMapper.java | 40 +++++++++++-------- .../user/controller/UserControllerTest.java | 1 - .../selfcare/user/mapper/UserMapperTest.java | 21 +++++----- 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/mapper/UserMapper.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/mapper/UserMapper.java index 3e2d7288..558ba03f 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/mapper/UserMapper.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/mapper/UserMapper.java @@ -43,6 +43,7 @@ public interface UserMapper { UserResponse toUserResponse(UserResource userResource, String userMailUuid); @Mapping(target = "email", expression = "java(retrieveCertifiedMailFromWorkContacts(userResource, userMailUuid))") + @Mapping(target = "mobilePhone", expression = "java(retrieveCertifiedMobilePhoneFromWorkContacts(userResource, userMailUuid))") @Mapping(source = "userResource.familyName", target = "familyName", qualifiedByName = "toFamilyNameCertifiableFieldResponse") @Mapping(source = "userResource.name", target = "name", qualifiedByName = "toNameCertifiableFieldResponse") @Mapping(target = "workContacts", expression = "java(toWorkContactResponse(userResource.getWorkContacts()))") @@ -95,19 +96,19 @@ default String retrieveMailFromWorkContacts(Map map @Named("retrieveCertifiedMailFromWorkContacts") - default CertifiableFieldResponse retrieveCertifiedMailFromWorkContacts(UserResource userResource, String userMailUuid){ - if(userResource.getWorkContacts()!=null && !userResource.getWorkContacts().isEmpty() && userResource.getWorkContacts().containsKey(userMailUuid)){ - return new CertifiableFieldResponse<>(userResource.getWorkContacts().get(userMailUuid).getEmail().getValue(), - Optional.ofNullable(userResource.getWorkContacts().get(userMailUuid).getEmail().getCertification()) - .map(certificationEnum -> mapToCertificationEnum(certificationEnum.value())).orElse(null)); - } - return null; + default CertifiableFieldResponse retrieveCertifiedMailFromWorkContacts(UserResource userResource, String userMailUuid) { + return Optional.ofNullable(userResource) + .map(UserResource::getWorkContacts) + .map(workContacts -> workContacts.get(userMailUuid)).flatMap(resource -> Optional.ofNullable(resource.getEmail()) + .map(email -> new CertifiableFieldResponse<>(email.getValue(), mapToCertificationEnum(email.getCertification().value())))) + .orElse(null); } - @Named("toNameCertifiableFieldResponse") - default CertifiableFieldResponse toNameCertifiableFieldResponse(NameCertifiableSchema resource){ - return Optional.ofNullable(resource).map(r -> new CertifiableFieldResponse<>(r.getValue(), - Optional.ofNullable(r.getCertification()).map(certificationEnum -> mapToCertificationEnum(certificationEnum.value())).orElse(null))) + @Named("retrieveCertifiedMobilePhoneFromWorkContacts") + default CertifiableFieldResponse retrieveCertifiedMobilePhoneFromWorkContacts(UserResource userResource, String userMailUuid) { + return Optional.ofNullable(userResource.getWorkContacts()) + .map(workContacts -> workContacts.get(userMailUuid)).flatMap(resource -> Optional.ofNullable(resource.getMobilePhone()) + .map(mobilePhone -> new CertifiableFieldResponse<>(mobilePhone.getValue(), mapToCertificationEnum(mobilePhone.getCertification().value())))) .orElse(null); } @@ -118,6 +119,13 @@ default CertifiableFieldResponse toFamilyNameCertifiableFieldResponse(Fa .orElse(null); } + @Named("toNameCertifiableFieldResponse") + default CertifiableFieldResponse toNameCertifiableFieldResponse(NameCertifiableSchema resource) { + return Optional.ofNullable(resource).map(r -> new CertifiableFieldResponse<>(r.getValue(), + Optional.ofNullable(r.getCertification()).map(certificationEnum -> mapToCertificationEnum(certificationEnum.value())).orElse(null))) + .orElse(null); + } + @Named("toEmailCertifiableFieldResponse") default CertifiableFieldResponse toEmailCertifiableFieldResponse(EmailCertifiableSchema resource){ return Optional.ofNullable(resource).map(r -> new CertifiableFieldResponse<>(r.getValue(), @@ -135,16 +143,16 @@ default CertificationEnum mapToCertificationEnum(String certificationEnum){ MutableUserFieldsDto toMutableUserFieldsDto(UserResource userResource); - @Mapping(target = "familyName", expression = "java(toFamilyNameCertifiableStringNotEquals(userResource.getFamilyName(), updateUserRequest.getFamilyName()))") - @Mapping(target = "name", expression = "java(toNameCertifiableStringNotEquals(userResource.getName(), updateUserRequest.getName()))") - @Mapping(target = "workContacts", expression = "java(toWorkContact(updateUserRequest.getEmail(), updateUserRequest.getMobilePhone(), idContact))") + @Mapping(target = "familyName", expression = "java(toFamilyNameCertifiableStringNotEquals(userResource.getFamilyName(), updateUserRequest.getFamilyName()))") + @Mapping(target = "name", expression = "java(toNameCertifiableStringNotEquals(userResource.getName(), updateUserRequest.getName()))") + @Mapping(target = "workContacts", expression = "java(toWorkContact(updateUserRequest.getEmail(), updateUserRequest.getMobilePhone(), idContact))") @Mapping(target = "email", ignore = true) @Mapping(target = "birthDate", ignore = true) MutableUserFieldsDto toMutableUserFieldsDto(UpdateUserRequest updateUserRequest, UserResource userResource, String idContact); @Mapping(source = "user.birthDate", target = "birthDate", qualifiedByName = "toCertifiableLocalDate") - @Mapping(source = "user.familyName", target = "familyName", qualifiedByName = "toFamilyNameCertifiableString") - @Mapping(source = "user.name", target = "name", qualifiedByName = "toNameCertifiableString") + @Mapping(source = "user.familyName", target = "familyName", qualifiedByName = "toFamilyNameCertifiableString") + @Mapping(source = "user.name", target = "name", qualifiedByName = "toNameCertifiableString") @Mapping(source = "user.fiscalCode", target = "fiscalCode") @Mapping(source = "workContactResource", target = "workContacts") SaveUserDto toSaveUserDto(CreateUserDto.User user, Map workContactResource); diff --git a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/controller/UserControllerTest.java b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/controller/UserControllerTest.java index 9e5df6a5..0ee52727 100644 --- a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/controller/UserControllerTest.java +++ b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/controller/UserControllerTest.java @@ -28,7 +28,6 @@ import org.apache.http.HttpStatus; import org.junit.jupiter.api.Test; import org.mockito.Mockito; -import org.openapi.quarkus.user_registry_json.model.*; import org.openapi.quarkus.user_registry_json.model.EmailCertifiableSchema; import org.openapi.quarkus.user_registry_json.model.FamilyNameCertifiableSchema; import org.openapi.quarkus.user_registry_json.model.MobilePhoneCertifiableSchema; diff --git a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/mapper/UserMapperTest.java b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/mapper/UserMapperTest.java index 506a0fed..c24e8dac 100644 --- a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/mapper/UserMapperTest.java +++ b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/mapper/UserMapperTest.java @@ -27,15 +27,15 @@ @QuarkusTest public class UserMapperTest { - private UserMapper userMapper = new UserMapperImpl(); + private final UserMapper userMapper = new UserMapperImpl(); @Test void retrieveCertifiedEmailFromWorkContacts(){ final UserResource userResource = new UserResource(); final EmailCertifiableSchema email = new EmailCertifiableSchema(EmailCertifiableSchema.CertificationEnum.NONE, "email"); final MobilePhoneCertifiableSchema phone = new MobilePhoneCertifiableSchema(MobilePhoneCertifiableSchema.CertificationEnum.NONE, "mobilePhone"); - WorkContactResource workContactResource = new WorkContactResource(email, null, null); - Map workContactResourceMap = Map.of("email", workContactResource); + WorkContactResource workContactResource = new WorkContactResource(email, phone, null); + Map workContactResourceMap = Map.of("contactsUuid", workContactResource); userResource.setWorkContacts(workContactResourceMap); CertifiableFieldResponse certifiedMail = userMapper.retrieveCertifiedMailFromWorkContacts(userResource, "contactsUuid"); @@ -47,16 +47,16 @@ void retrieveCertifiedEmailFromWorkContacts(){ @Test void retrieveCertifiedMobilePhoneFromWorkContacts(){ final UserResource userResource = new UserResource(); - final CertifiableFieldResourceOfstring email = new CertifiableFieldResourceOfstring(CertifiableFieldResourceOfstring.CertificationEnum.NONE, "email"); + final EmailCertifiableSchema email = new EmailCertifiableSchema(EmailCertifiableSchema.CertificationEnum.NONE, "email"); String mobilePhone = "mobilePhone"; - final CertifiableFieldResourceOfstring phone = new CertifiableFieldResourceOfstring(CertifiableFieldResourceOfstring.CertificationEnum.NONE, mobilePhone); - WorkContactResource workContactResource = new WorkContactResource(email, phone); + final MobilePhoneCertifiableSchema phone = new MobilePhoneCertifiableSchema(MobilePhoneCertifiableSchema.CertificationEnum.NONE, mobilePhone); + WorkContactResource workContactResource = new WorkContactResource(email, phone, null); Map workContactResourceMap = Map.of("contactsUuid", workContactResource); userResource.setWorkContacts(workContactResourceMap); CertifiableFieldResponse certifiedPhone = userMapper.retrieveCertifiedMobilePhoneFromWorkContacts(userResource, "contactsUuid"); - assertEquals(CertifiableFieldResourceOfstring.CertificationEnum.NONE,certifiedPhone.getCertified()); + assertEquals(CertificationEnum.NONE,certifiedPhone.getCertified()); assertEquals(mobilePhone, certifiedPhone.getValue()); } @@ -75,7 +75,7 @@ void retrieveCertifiedEmailFromWorkContacts_notPresent(){ final EmailCertifiableSchema email = new EmailCertifiableSchema(EmailCertifiableSchema.CertificationEnum.NONE, "email"); final MobilePhoneCertifiableSchema phone = new MobilePhoneCertifiableSchema(MobilePhoneCertifiableSchema.CertificationEnum.NONE, "mobilePhone"); - WorkContactResource workContactResource = new WorkContactResource(email, null, null); + WorkContactResource workContactResource = new WorkContactResource(email, phone, null); Map workContactResourceMap = Map.of("email", workContactResource); userResource.setWorkContacts(workContactResourceMap); @@ -89,7 +89,7 @@ void retrieveCertifiedEmailFromWorkContacts_notPresent(){ void retrieveMailFromWorkContacts(){ final EmailCertifiableSchema email = new EmailCertifiableSchema(EmailCertifiableSchema.CertificationEnum.NONE, "email"); final MobilePhoneCertifiableSchema phone = new MobilePhoneCertifiableSchema(MobilePhoneCertifiableSchema.CertificationEnum.NONE, "mobilePhone"); - WorkContactResource workContactResource = new WorkContactResource(email, null, null); + WorkContactResource workContactResource = new WorkContactResource(email, phone, null); Map workContactResourceMap = Map.of("email", workContactResource); String mailFromWorkContact = userMapper.retrieveMailFromWorkContacts(workContactResourceMap, "email"); @@ -108,9 +108,8 @@ void retrieveMailFromWorkContacts_emptyMap(){ @Test void retrieveMailFromWorkContacts_nullMap(){ - Map workContactResourceMap = null; - String mailFromWorkContact = userMapper.retrieveMailFromWorkContacts(workContactResourceMap, "email"); + String mailFromWorkContact = userMapper.retrieveMailFromWorkContacts(null, "email"); assertNull(mailFromWorkContact); } From 993de8e06d53c5240e9ef6ea7c670a43103a2b83 Mon Sep 17 00:00:00 2001 From: flaminiaScarciofolo Date: Fri, 29 Nov 2024 10:51:33 +0100 Subject: [PATCH 4/4] [SELC-6087] fix findMailUuidAndUpdateUserRegistry method to retrieve workContact id --- apps/user-ms/src/main/docs/openapi.json | 2 +- apps/user-ms/src/main/docs/openapi.yaml | 2 +- .../it/pagopa/selfcare/user/model/UpdateUserRequest.java | 2 +- .../selfcare/user/service/UserRegistryServiceImpl.java | 7 ++++--- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/apps/user-ms/src/main/docs/openapi.json b/apps/user-ms/src/main/docs/openapi.json index ca69cf80..0ffc9925 100644 --- a/apps/user-ms/src/main/docs/openapi.json +++ b/apps/user-ms/src/main/docs/openapi.json @@ -1632,7 +1632,7 @@ "type" : "string" }, "mobilePhone" : { - "pattern" : "^\\+?[0-9]{9,15}$", + "pattern" : "^\\+?[0-9]{7,15}$", "type" : "string" } } diff --git a/apps/user-ms/src/main/docs/openapi.yaml b/apps/user-ms/src/main/docs/openapi.yaml index 822e1c46..3394b481 100644 --- a/apps/user-ms/src/main/docs/openapi.yaml +++ b/apps/user-ms/src/main/docs/openapi.yaml @@ -1191,7 +1191,7 @@ components: email: type: string mobilePhone: - pattern: "^\\+?[0-9]{9,15}$" + pattern: "^\\+?[0-9]{7,15}$" type: string User: required: diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/model/UpdateUserRequest.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/model/UpdateUserRequest.java index 3b167150..a9381996 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/model/UpdateUserRequest.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/model/UpdateUserRequest.java @@ -9,6 +9,6 @@ public class UpdateUserRequest { private String name; private String familyName; private String email; - @Pattern(regexp = "^\\+?[0-9]{9,15}$", message = "Il numero di telefono non è valido") + @Pattern(regexp = "^\\+?[0-9]{7,15}$", message = "Il numero di telefono non è valido") private String mobilePhone; } diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserRegistryServiceImpl.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserRegistryServiceImpl.java index ad141d0c..13485f30 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserRegistryServiceImpl.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserRegistryServiceImpl.java @@ -139,11 +139,12 @@ private Uni findMailUuidAndUpdateUserRegistry(UserResource userResource, .filter(stringWorkContactResourceEntry -> existsWorkContactResourceForPhoneAndMail(stringWorkContactResourceEntry, emailToCompare, mobilePhoneToCompare)) .findFirst() .map(Map.Entry::getKey)) - .orElse(null); + .orElse(idContacts); + return updateUsingPATCH(userResource.getId().toString(), - userMapper.toMutableUserFieldsDto(userDto, userResource, idContacts)) - .replaceWith(StringUtils.isBlank(existedUserMailUuid) ? idContacts : existedUserMailUuid); + userMapper.toMutableUserFieldsDto(userDto, userResource, existedUserMailUuid)) + .replaceWith(existedUserMailUuid); } private static boolean existsWorkContactResourceForPhoneAndMail(Map.Entry stringWorkContactResourceEntry, String emailToCompare, String mobilePhoneToCompare) {