Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
SELC-5169 Aggiunta record PecNotification in persistOnboarding
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenbegiqi committed Jul 5, 2024
1 parent e616f43 commit b35418e
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 6 deletions.
5 changes: 4 additions & 1 deletion app/src/main/resources/config/core-config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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}
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}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,29 @@
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;
import it.pagopa.selfcare.mscore.exception.ResourceNotFoundException;
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.*;

Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,24 @@
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;
import org.mockito.*;
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;

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ public ResponseEntity<InstitutionResponse> updateInstitution(@ApiParam("${swagge
@PostMapping(value = "/{id}/onboarding", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<InstitutionResponse> onboardingInstitution(@RequestBody @Valid InstitutionOnboardingRequest request,
@PathVariable("id") String id) {

CustomExceptionMessage.setCustomMessage(GenericError.ONBOARDING_OPERATION_ERROR);

StringBuilder httpStatus = new StringBuilder();
Expand Down

0 comments on commit b35418e

Please sign in to comment.