From b35418e3656612b62873863aa094c55a25898c26 Mon Sep 17 00:00:00 2001 From: Eugen Begiqi Date: Fri, 5 Jul 2024 08:48:27 +0200 Subject: [PATCH] SELC-5169 Aggiunta record PecNotification in persistOnboarding --- .../resources/config/core-config.properties | 5 +- .../mscore/constant/GenericError.java | 3 +- .../mscore/core/OnboardingServiceImpl.java | 50 +++++++++++++++++-- .../core/OnboardingServiceImplTest.java | 36 ++++++++++++- .../web/controller/InstitutionController.java | 1 + 5 files changed, 89 insertions(+), 6 deletions(-) diff --git a/app/src/main/resources/config/core-config.properties b/app/src/main/resources/config/core-config.properties index 8f8e3dd7..9a5ec02e 100644 --- a/app/src/main/resources/config/core-config.properties +++ b/app/src/main/resources/config/core-config.properties @@ -25,4 +25,7 @@ mscore.blob-storage.connection-string-product = ${BLOB_STORAGE_PRODUCT_CONNECTIO #SES mscore.aws-ses-secret-id=${AWS_SES_ACCESS_KEY_ID:secret-id-example} mscore.aws-ses-secret-key=${AWS_SES_SECRET_ACCESS_KEY:secret-key-example} -mscore.aws-ses-region=${AWS_SES_REGION:eu-south-1} \ No newline at end of file +mscore.aws-ses-region=${AWS_SES_REGION:eu-south-1} + +mscore.sending-frequency-pec-notification=${SENDING_FREQUENCY_PEC_NOTIFICATION:30} +mscore.epoch-date-pec-notification=${EPOCH_DATE_PEC_NOTIFICATION:2024-01-01} \ No newline at end of file diff --git a/connector-api/src/main/java/it/pagopa/selfcare/mscore/constant/GenericError.java b/connector-api/src/main/java/it/pagopa/selfcare/mscore/constant/GenericError.java index e72036ce..d155c840 100644 --- a/connector-api/src/main/java/it/pagopa/selfcare/mscore/constant/GenericError.java +++ b/connector-api/src/main/java/it/pagopa/selfcare/mscore/constant/GenericError.java @@ -63,7 +63,8 @@ public enum GenericError { UPDATE_USER_INSTITUTION_ERROR("0000", "Error while updating InstitutionUser for id %s"), GENERIC_ERROR("0000", "Generic Error"), DELETE_ONBOARDED_OPERATION_ERROR("0000", "Error while deleting Onboarded Institution"), - DELETE_NOTIFICATION_OPERATION_ERROR ("0000", "PecNotificationEntity was not deleted because either it does not exist or there are multiple records"); + DELETE_NOTIFICATION_OPERATION_ERROR ("0000", "PecNotificationEntity was not deleted because either it does not exist or there are multiple records"), + INVALID_INSERT_PEC_NOTIFICATION_ERROR ("0001", "PecNotificationEntity was not inserted because either it exist or the data are invalid"); private final String code; private final String detail; diff --git a/core/src/main/java/it/pagopa/selfcare/mscore/core/OnboardingServiceImpl.java b/core/src/main/java/it/pagopa/selfcare/mscore/core/OnboardingServiceImpl.java index 5d6f7424..d22af95a 100644 --- a/core/src/main/java/it/pagopa/selfcare/mscore/core/OnboardingServiceImpl.java +++ b/core/src/main/java/it/pagopa/selfcare/mscore/core/OnboardingServiceImpl.java @@ -3,6 +3,7 @@ import it.pagopa.selfcare.mscore.api.InstitutionConnector; import it.pagopa.selfcare.mscore.api.PecNotificationConnector; import it.pagopa.selfcare.mscore.constant.CustomError; +import it.pagopa.selfcare.mscore.constant.GenericError; import it.pagopa.selfcare.mscore.constant.RelationshipState; import it.pagopa.selfcare.mscore.core.util.UtilEnumList; import it.pagopa.selfcare.mscore.exception.InvalidRequestException; @@ -10,15 +11,21 @@ import it.pagopa.selfcare.mscore.model.institution.Institution; import it.pagopa.selfcare.mscore.model.institution.Onboarding; import it.pagopa.selfcare.mscore.model.onboarding.VerifyOnboardingFilters; +import it.pagopa.selfcare.mscore.model.pecnotification.PecNotification; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; +import java.time.LocalDate; +import java.time.Month; import java.time.OffsetDateTime; +import java.time.temporal.ChronoUnit; import java.util.List; import java.util.Objects; import java.util.Optional; +import java.util.UUID; import static it.pagopa.selfcare.mscore.constant.GenericError.*; @@ -30,15 +37,23 @@ public class OnboardingServiceImpl implements OnboardingService { private final InstitutionConnector institutionConnector; private final PecNotificationConnector pecNotificationConnector; + private Integer sendingFrequencyPecNotification; + private String epochDatePecNotification; + + private LocalDate currentDate = LocalDate.now(); public OnboardingServiceImpl(OnboardingDao onboardingDao, InstitutionService institutionService, InstitutionConnector institutionConnector, - PecNotificationConnector pecNotificationConnector) { + PecNotificationConnector pecNotificationConnector, + @Value("${mscore.sending-frequency-pec-notification}") Integer sendingFrequencyPecNotification, + @Value("${mscore.epoch-date-pec-notification}") String epochDatePecNotification) { this.onboardingDao = onboardingDao; this.institutionService = institutionService; this.institutionConnector = institutionConnector; this.pecNotificationConnector = pecNotificationConnector; + this.sendingFrequencyPecNotification = sendingFrequencyPecNotification; + this.epochDatePecNotification = epochDatePecNotification; } @Override @@ -71,6 +86,35 @@ public void verifyOnboardingInfoByFilters(VerifyOnboardingFilters filters) { public Institution persistOnboarding(String institutionId, String productId, Onboarding onboarding, StringBuilder httpStatus) { + Institution institution = this.persistAndReturnInstitution(institutionId, productId, onboarding, httpStatus); + this.insertPecNotification(institutionId, productId); + + return institution; + } + + public void insertPecNotification(String institutionId, String productId) { + + PecNotification pecNotification = new PecNotification(); + pecNotification.setId(UUID.randomUUID().toString()); + pecNotification.setCreatedAt(OffsetDateTime.now()); + pecNotification.setProductId(productId); + pecNotification.setInstitutionId(institutionId); + pecNotification.setModuleDayOfTheEpoch(calculateModuleDayOfTheEpoch()); + + if (!pecNotificationConnector.insertPecNotification(pecNotification)){ + throw new InvalidRequestException(GenericError.INVALID_INSERT_PEC_NOTIFICATION_ERROR.getMessage(), INVALID_INSERT_PEC_NOTIFICATION_ERROR.getCode()); + } + + } + + public int calculateModuleDayOfTheEpoch() { + LocalDate epochStart = LocalDate.parse(this.epochDatePecNotification); + long daysDiff = ChronoUnit.DAYS.between(epochStart, this.currentDate); + int moduleDayOfTheEpoch = (int) (daysDiff % this.sendingFrequencyPecNotification); + return moduleDayOfTheEpoch; + } + + private Institution persistAndReturnInstitution(String institutionId, String productId, Onboarding onboarding, StringBuilder httpStatus) { log.trace("persistForUpdate start"); log.debug("persistForUpdate institutionId = {}, productId = {}", institutionId, productId); onboarding.setStatus(RelationshipState.ACTIVE); @@ -86,9 +130,9 @@ public Institution persistOnboarding(String institutionId, String if (Optional.ofNullable(institution.getOnboarding()).flatMap(onboardings -> onboardings.stream() .filter(item -> item.getProductId().equals(productId) && UtilEnumList.VALID_RELATIONSHIP_STATES.contains(item.getStatus())) .findAny()).isPresent()) { - + httpStatus.append(HttpStatus.OK.value()); - return institution; + return institution; } try { diff --git a/core/src/test/java/it/pagopa/selfcare/mscore/core/OnboardingServiceImplTest.java b/core/src/test/java/it/pagopa/selfcare/mscore/core/OnboardingServiceImplTest.java index a9d3ab49..34806f42 100644 --- a/core/src/test/java/it/pagopa/selfcare/mscore/core/OnboardingServiceImplTest.java +++ b/core/src/test/java/it/pagopa/selfcare/mscore/core/OnboardingServiceImplTest.java @@ -13,6 +13,7 @@ import it.pagopa.selfcare.mscore.model.institution.Institution; import it.pagopa.selfcare.mscore.model.institution.Onboarding; import it.pagopa.selfcare.mscore.model.onboarding.*; +import it.pagopa.selfcare.mscore.model.pecnotification.PecNotification; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -20,11 +21,16 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.http.HttpStatus; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.util.ReflectionTestUtils; import java.time.LocalDate; +import java.time.temporal.ChronoUnit; import java.util.List; import java.util.UUID; @@ -140,7 +146,13 @@ void persistOnboarding_whenUserExistsOnRegistry() { Institution institution = new Institution(); institution.setId("institutionId"); institution.setOnboarding(List.of(onboarding, dummyOnboarding())); + when(institutionConnector.findById(institution.getId())).thenReturn(institution); + when(pecNotificationConnector.insertPecNotification(any(PecNotification.class))) + .thenReturn(true); + + ReflectionTestUtils.setField(onboardingServiceImpl, "sendingFrequencyPecNotification", 30); + ReflectionTestUtils.setField(onboardingServiceImpl, "epochDatePecNotification", "2024-01-01"); String institutionId = institution.getId(); @@ -227,7 +239,12 @@ void persistOnboarding_whenUserNotExistsOnRegistry() { when(institutionConnector.findById(institution.getId())).thenReturn(institution); when(institutionConnector.findAndUpdate(any(), any(), any(), any())).thenReturn(institution); - + when(pecNotificationConnector.insertPecNotification(any(PecNotification.class))) + .thenReturn(true); + + ReflectionTestUtils.setField(onboardingServiceImpl, "sendingFrequencyPecNotification", 30); + ReflectionTestUtils.setField(onboardingServiceImpl, "epochDatePecNotification", "2024-01-01"); + StringBuilder statusCode = new StringBuilder(); onboardingServiceImpl.persistOnboarding(institution.getId(), productId, onboardingToPersist, statusCode); @@ -291,5 +308,22 @@ void deleteOnboardedInstitution_deletePecNotificationFails() { verify(institutionConnector, times(1)).findAndRemoveOnboarding(institutionId, onboarding); verify(pecNotificationConnector, times(1)).findAndDeletePecNotification(institutionId, productId); } + + @Test + public void testCalculateModuleDayOfTheEpoch() { + LocalDate mockCurrentDate = LocalDate.of(2024, 2, 1); // 31 days after epoch + + ReflectionTestUtils.setField(onboardingServiceImpl, "sendingFrequencyPecNotification", 30); + ReflectionTestUtils.setField(onboardingServiceImpl, "epochDatePecNotification", "2024-01-01"); + ReflectionTestUtils.setField(onboardingServiceImpl, "currentDate", mockCurrentDate); + + int result = onboardingServiceImpl.calculateModuleDayOfTheEpoch(); + + LocalDate epochStart = LocalDate.parse("2024-01-01"); + long daysDiff = ChronoUnit.DAYS.between(epochStart, mockCurrentDate); + int expected = (int) (daysDiff % 30); + + assertEquals(expected, result); + } } diff --git a/web/src/main/java/it/pagopa/selfcare/mscore/web/controller/InstitutionController.java b/web/src/main/java/it/pagopa/selfcare/mscore/web/controller/InstitutionController.java index 49c66418..2f9a4195 100644 --- a/web/src/main/java/it/pagopa/selfcare/mscore/web/controller/InstitutionController.java +++ b/web/src/main/java/it/pagopa/selfcare/mscore/web/controller/InstitutionController.java @@ -357,6 +357,7 @@ public ResponseEntity updateInstitution(@ApiParam("${swagge @PostMapping(value = "/{id}/onboarding", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity onboardingInstitution(@RequestBody @Valid InstitutionOnboardingRequest request, @PathVariable("id") String id) { + CustomExceptionMessage.setCustomMessage(GenericError.ONBOARDING_OPERATION_ERROR); StringBuilder httpStatus = new StringBuilder();