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

Commit

Permalink
[SELC-5169] feat : Refactored and added environment variables to disa…
Browse files Browse the repository at this point in the history
…ble PecNotification (#528)
  • Loading branch information
eugenbegiqi authored Jul 11, 2024
1 parent 1bf381e commit 6a70ed1
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 20 deletions.
3 changes: 2 additions & 1 deletion app/src/main/resources/config/core-config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ mscore.aws-ses-secret-key=${AWS_SES_SECRET_ACCESS_KEY:secret-key-example}
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}
mscore.epoch-date-pec-notification=${EPOCH_DATE_PEC_NOTIFICATION:2024-01-01}
mscore.pec-notification.disabled=${PEC_NOTIFICATION_DISABLED:false}
4 changes: 4 additions & 0 deletions connector-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<artifactId>onboarding-sdk-product</artifactId>
<version>0.1.15</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>bson</artifactId>
</dependency>
</dependencies>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.util.List;
import org.bson.types.ObjectId;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class PecNotification {

private Object id;
private ObjectId id;
private String institutionId;
private String productId;
private Integer moduleDayOfTheEpoch;
private String digitalAddress;

private OffsetDateTime createdAt;
private OffsetDateTime updatedAt;
private Instant createdAt;
private Instant updatedAt;


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldNameConstants;
import org.bson.codecs.pojo.annotations.BsonId;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Sharded;

import java.time.Instant;
import java.time.OffsetDateTime;

@Data
Expand All @@ -16,14 +19,14 @@
@FieldNameConstants(asEnum = true)
public class PecNotificationEntity {

@Id
private Object id;
@BsonId
private ObjectId id;
private String institutionId;
private String productId;
private Integer moduleDayOfTheEpoch;
private String digitalAddress;

private OffsetDateTime createdAt;
private OffsetDateTime updatedAt;
private Instant createdAt;
private Instant updatedAt;

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@Mapper(componentModel = "spring", imports = UUID.class)
public interface PecNotificationEntityMapper {

@Mapping(target = "id", defaultExpression = "java(UUID.randomUUID().toString())")
@Mapping(target = "id", defaultExpression = "java(org.bson.types.ObjectId.get())")
PecNotificationEntity convertToPecNotificationEntity(PecNotification institution);

PecNotification convertToPecNotification(PecNotificationEntity entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@
import it.pagopa.selfcare.mscore.model.pecnotification.PecNotification;
import lombok.extern.slf4j.Slf4j;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDate;
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 @@ -34,23 +35,27 @@ public class OnboardingServiceImpl implements OnboardingService {
private final InstitutionService institutionService;
private final InstitutionConnector institutionConnector;
private final PecNotificationConnector pecNotificationConnector;
private Integer sendingFrequencyPecNotification;
private String epochDatePecNotification;
private LocalDate currentDate = LocalDate.now();
private final Integer sendingFrequencyPecNotification;
private final String epochDatePecNotification;
private final LocalDate currentDate = LocalDate.now();

private final Boolean disabledPecNotification;

public OnboardingServiceImpl(OnboardingDao onboardingDao,
InstitutionService institutionService,
InstitutionConnector institutionConnector,
PecNotificationConnector pecNotificationConnector,
@Value("${mscore.sending-frequency-pec-notification}") Integer sendingFrequencyPecNotification,
@Value("${mscore.epoch-date-pec-notification}") String epochDatePecNotification) {
@Value("${mscore.epoch-date-pec-notification}") String epochDatePecNotification,
@Value("${mscore.pec-notification.disabled}") Boolean disabledPecNotification) {

this.onboardingDao = onboardingDao;
this.institutionService = institutionService;
this.institutionConnector = institutionConnector;
this.pecNotificationConnector = pecNotificationConnector;
this.sendingFrequencyPecNotification = sendingFrequencyPecNotification;
this.epochDatePecNotification = epochDatePecNotification;
this.disabledPecNotification = disabledPecNotification;
}

@Override
Expand Down Expand Up @@ -82,8 +87,8 @@ public void verifyOnboardingInfoByFilters(VerifyOnboardingFilters filters) {
public void insertPecNotification(String institutionId, String productId, String digitalAddress) {

PecNotification pecNotification = new PecNotification();
pecNotification.setId(UUID.randomUUID().toString());
pecNotification.setCreatedAt(OffsetDateTime.now());
pecNotification.setId(ObjectId.get());
pecNotification.setCreatedAt(Instant.now());
pecNotification.setProductId(productId);
pecNotification.setInstitutionId(institutionId);
pecNotification.setModuleDayOfTheEpoch(calculateModuleDayOfTheEpoch());
Expand All @@ -98,16 +103,19 @@ public void insertPecNotification(String institutionId, String productId, String
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;
return (int) (daysDiff % this.sendingFrequencyPecNotification);
}

@Override
public Institution persistOnboarding(String institutionId, String
productId, Onboarding onboarding, StringBuilder httpStatus) {

Institution institution = persistAndGetInstitution(institutionId, productId, onboarding, httpStatus);
this.insertPecNotification(institutionId, productId, institution.getDigitalAddress());

if (!disabledPecNotification){
this.insertPecNotification(institutionId, productId, institution.getDigitalAddress());
}

return institution;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ void persistOnboarding_whenUserExistsOnRegistry() {

ReflectionTestUtils.setField(onboardingServiceImpl, "sendingFrequencyPecNotification", 30);
ReflectionTestUtils.setField(onboardingServiceImpl, "epochDatePecNotification", "2024-01-01");
ReflectionTestUtils.setField(onboardingServiceImpl, "disabledPecNotification", false);

Onboarding onboarding = dummyOnboarding();
onboarding.setStatus(UtilEnumList.VALID_RELATIONSHIP_STATES.get(0));
Expand All @@ -159,6 +160,36 @@ void persistOnboarding_whenUserExistsOnRegistry() {
assertEquals(HttpStatus.OK.value(), Integer.parseInt(statusCode.toString()));
}

@Test
void persistOnboarding_whenPecNotificationIsDisabled() {

ReflectionTestUtils.setField(onboardingServiceImpl, "sendingFrequencyPecNotification", 30);
ReflectionTestUtils.setField(onboardingServiceImpl, "epochDatePecNotification", "2024-01-01");
ReflectionTestUtils.setField(onboardingServiceImpl, "disabledPecNotification", true);

Onboarding onboarding = dummyOnboarding();
onboarding.setStatus(UtilEnumList.VALID_RELATIONSHIP_STATES.get(0));
Institution institution = new Institution();
institution.setId("institutionId");
institution.setOnboarding(List.of(onboarding, dummyOnboarding()));

when(institutionConnector.findById(institution.getId())).thenReturn(institution);

String institutionId = institution.getId();

String productId = onboarding.getProductId();
Onboarding onb = new Onboarding();

StringBuilder statusCode = new StringBuilder();

onboardingServiceImpl.persistOnboarding(institutionId,
productId, onb, statusCode);

verify(pecNotificationConnector, never()).insertPecNotification(any(PecNotification.class));

assertEquals(HttpStatus.OK.value(), Integer.parseInt(statusCode.toString()));
}



/**
Expand Down Expand Up @@ -201,6 +232,7 @@ void persistOnboarding_whenUserNotExistsOnRegistry() {

ReflectionTestUtils.setField(onboardingServiceImpl, "sendingFrequencyPecNotification", 30);
ReflectionTestUtils.setField(onboardingServiceImpl, "epochDatePecNotification", "2024-01-01");
ReflectionTestUtils.setField(onboardingServiceImpl, "disabledPecNotification", false);

String pricingPlan = "pricingPlan";
String productId = "productId";
Expand Down
4 changes: 4 additions & 0 deletions infra/container_apps/env/dev-pnpg/terraform.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ app_settings = [
{
name = "MAIL_SENDER_ADDRESS"
value = "[email protected]"
},
{
name = "PEC_NOTIFICATION_DISABLED"
value = "true"
}
]

Expand Down
4 changes: 4 additions & 0 deletions infra/container_apps/env/dev/terraform.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ app_settings = [
{
name = "MAIL_SENDER_ADDRESS"
value = "[email protected]"
},
{
name = "PEC_NOTIFICATION_DISABLED"
value = "false"
}
]

Expand Down
4 changes: 4 additions & 0 deletions infra/container_apps/env/prod-pnpg/terraform.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ app_settings = [
{
name = "MAIL_SENDER_ADDRESS"
value = "[email protected]"
},
{
name = "PEC_NOTIFICATION_DISABLED"
value = "true"
}
]

Expand Down
4 changes: 4 additions & 0 deletions infra/container_apps/env/prod/terraform.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ app_settings = [
{
name = "MAIL_SENDER_ADDRESS"
value = "[email protected]"
},
{
name = "PEC_NOTIFICATION_DISABLED"
value = "false"
}
]

Expand Down
4 changes: 4 additions & 0 deletions infra/container_apps/env/uat-pnpg/terraform.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ app_settings = [
{
name = "MAIL_SENDER_ADDRESS"
value = "[email protected]"
},
{
name = "PEC_NOTIFICATION_DISABLED"
value = "true"
}
]

Expand Down
4 changes: 4 additions & 0 deletions infra/container_apps/env/uat/terraform.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ app_settings = [
{
name = "MAIL_SENDER_ADDRESS"
value = "[email protected]"
},
{
name = "PEC_NOTIFICATION_DISABLED"
value = "false"
}
]

Expand Down

0 comments on commit 6a70ed1

Please sign in to comment.