Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into feature/SELC-5264

� Conflicts:
�	infra/functions/onboarding-functions/env/dev/terraform.tfvars
  • Loading branch information
pierpaolo.didato@emeal.nttdata.com authored and pierpaolo.didato@emeal.nttdata.com committed Jul 17, 2024
2 parents 347a3bb + 39f8680 commit f12c399
Show file tree
Hide file tree
Showing 33 changed files with 318 additions and 126 deletions.
17 changes: 11 additions & 6 deletions apps/onboarding-functions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,17 @@
<artifactId>quarkus-jacoco</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>applicationinsights-web</artifactId>
<version>2.6.4</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ public void filter(ClientRequestContext clientRequestContext) {
NotificationConfig.Consumer consumerConfiguration = notificationConfig.consumers().values().stream()
.filter(consumer -> consumer.topic().equals(topic))
.findFirst().orElse(null);
if(Objects.nonNull(consumerConfiguration))
if (Objects.nonNull(consumerConfiguration)) {
clientRequestContext.getHeaders().add("Authorization", getSASToken(resourceUri.toString(), consumerConfiguration.name(), consumerConfiguration.key()));
}
}

private static String getSASToken(String resourceUri, String keyName, String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.eclipse.microprofile.rest.client.annotation.RegisterProvider;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

import java.util.concurrent.CompletionStage;

@RegisterRestClient(configKey = "event-hub")
@ApplicationScoped
@Path("/")
Expand All @@ -16,7 +18,7 @@ public interface EventHubRestClient {

@POST
@Path("{hubName}/messages")
void sendMessage(@PathParam("hubName") String topicName, String notification);
Void sendMessage(@PathParam("hubName") String topicName, String notification);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,4 @@ public PadesSignService padesSignService(@ConfigProperty(name = "onboarding-func
default -> new PadesSignServiceImpl(pkcs7HashSignService());
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,16 @@ public String getParentDescription() {
public void setParentDescription(String parentDescription) {
this.parentDescription = parentDescription;
}

@Override
public String toString() {
return "Institution{" +
"id='" + id + '\'' +
", taxCode='" + taxCode + '\'' +
", description=" + description +
", digitalAddress=" + digitalAddress +
", origin=" + origin +
", originId=" + originId +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ public class NotificationFunctions {
private final NotificationEventResenderService notificationEventResenderService;
private final ObjectMapper objectMapper;


public NotificationFunctions(ObjectMapper objectMapper,
NotificationEventService notificationEventService,
OnboardingService onboardingService, NotificationEventResenderService notificationEventResenderService) {
OnboardingService onboardingService,
NotificationEventResenderService notificationEventResenderService) {
this.objectMapper = objectMapper;
this.notificationEventService = notificationEventService;
this.onboardingService = onboardingService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
public class OnboardingFunctions {

public static final String CREATED_NEW_ONBOARDING_ORCHESTRATION_WITH_INSTANCE_ID_MSG = "Created new Onboarding orchestration with instance ID = ";

private final OnboardingService service;
private final CompletionService completionService;
private final ObjectMapper objectMapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public class CompletionServiceDefault implements CompletionService {
@ConfigProperty(name = "onboarding-functions.persist-users.send-mail")
private boolean hasToSendEmail;

@ConfigProperty(name = "onboarding-function.force-institution-persist")
@ConfigProperty(name = "onboarding-functions.force-institution-persist")
private boolean forceInstitutionCreation;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.microsoft.applicationinsights.TelemetryClient;
import com.microsoft.applicationinsights.TelemetryConfiguration;
import com.microsoft.azure.functions.ExecutionContext;
import it.pagopa.selfcare.onboarding.client.eventhub.EventHubRestClient;
import it.pagopa.selfcare.onboarding.config.NotificationConfig;
Expand All @@ -20,19 +22,24 @@
import it.pagopa.selfcare.product.service.ProductService;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Context;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.openapi.quarkus.core_json.api.InstitutionApi;
import org.openapi.quarkus.core_json.model.InstitutionResponse;

import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.*;

import static it.pagopa.selfcare.onboarding.utils.Utils.isNotInstitutionOnboarding;

@ApplicationScoped
public class NotificationEventServiceDefault implements NotificationEventService {

public static final String EVENT_ONBOARDING_FN_NAME = "ONBOARDING-FN";
public static final String EVENT_ONBOARDING_INSTTITUTION_FN_FAILURE = "EventsOnboardingInstitution_failures";
public static final String EVENT_ONBOARDING_INSTTITUTION_FN_SUCCESS = "EventsOnboardingInstitution_success";
private static final String OPERATION_NAME = "ONBOARDING-FN";
private final TelemetryClient telemetryClient;
@RestClient
@Inject
EventHubRestClient eventHubRestClient;
Expand All @@ -52,12 +59,17 @@ public NotificationEventServiceDefault(ProductService productService,
NotificationConfig notificationConfig,
NotificationBuilderFactory notificationBuilderFactory,
TokenRepository tokenRepository,
QueueEventExaminer queueEventExaminer) {
QueueEventExaminer queueEventExaminer,
@Context @ConfigProperty(name = "onboarding-functions.appinsights.connection-string") String appInsightsConnectionString) {
this.productService = productService;
this.notificationConfig = notificationConfig;
this.notificationBuilderFactory = notificationBuilderFactory;
this.tokenRepository = tokenRepository;
this.queueEventExaminer = queueEventExaminer;
TelemetryConfiguration telemetryConfiguration = TelemetryConfiguration.createDefault();
telemetryConfiguration.setConnectionString(appInsightsConnectionString);
this.telemetryClient = new TelemetryClient(telemetryConfiguration);
this.telemetryClient.getContext().getOperation().setName(OPERATION_NAME);
mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
}
Expand All @@ -79,9 +91,8 @@ public void send(ExecutionContext context, Onboarding onboarding, QueueEvent que
queueEvent = queueEventExaminer.determineEventType(onboarding);
}

Optional<Token> token = tokenRepository.findByOnboardingId(onboarding.getId());
InstitutionResponse institution = institutionApi.retrieveInstitutionByIdUsingGET(onboarding.getInstitution().getId());

Optional<Token> token = tokenRepository.findByOnboardingId(onboarding.getId());
for (String consumer : product.getConsumers()) {
NotificationConfig.Consumer consumerConfig = notificationConfig.consumers().get(consumer.toLowerCase());
prepareAndSendNotification(context, product, consumerConfig, onboarding, token.orElse(null), institution, queueEvent);
Expand All @@ -105,7 +116,14 @@ private void prepareAndSendNotification(ExecutionContext context, Product produc
private void sendNotification(ExecutionContext context, String topic, NotificationToSend notificationToSend) throws JsonProcessingException {
String message = mapper.writeValueAsString(notificationToSend);
context.getLogger().info(() -> String.format("Sending notification on topic: %s with message: %s", topic, message));
eventHubRestClient.sendMessage(topic, message);

try {
eventHubRestClient.sendMessage(topic, message);
telemetryClient.trackEvent(EVENT_ONBOARDING_FN_NAME, notificationEventMap(notificationToSend), Map.of(EVENT_ONBOARDING_INSTTITUTION_FN_SUCCESS, 1D));
} catch (Exception e) {
telemetryClient.trackEvent(EVENT_ONBOARDING_FN_NAME, notificationEventMap(notificationToSend), Map.of(EVENT_ONBOARDING_INSTTITUTION_FN_FAILURE, 1D));
throw new NotificationException(e.getMessage());
}
}

private void sendTestEnvProductsNotification(ExecutionContext context, Product product, String topic, NotificationToSend notificationToSend) throws JsonProcessingException {
Expand All @@ -119,4 +137,48 @@ private void sendTestEnvProductsNotification(ExecutionContext context, Product p
}
}

public static Map<String, String> notificationEventMap(NotificationToSend notificationToSend) {
Map<String, String> propertiesMap = new HashMap<>();
Optional.ofNullable(notificationToSend.getId()).ifPresent(value -> propertiesMap.put("id", value));
Optional.ofNullable(notificationToSend.getInternalIstitutionID()).ifPresent(value -> propertiesMap.put("internalIstitutionID", value));
Optional.ofNullable(notificationToSend.getInstitutionId()).ifPresent(value -> propertiesMap.put("institutionId", value));
Optional.ofNullable(notificationToSend.getProduct()).ifPresent(value -> propertiesMap.put("product", value));
Optional.ofNullable(notificationToSend.getState()).ifPresent(value -> propertiesMap.put("state", value));
Optional.ofNullable(notificationToSend.getFilePath()).ifPresent(value -> propertiesMap.put("filePath", value));
Optional.ofNullable(notificationToSend.getFileName()).ifPresent(value -> propertiesMap.put("fileName", value));
Optional.ofNullable(notificationToSend.getContentType()).ifPresent(value -> propertiesMap.put("contentType", value));
Optional.ofNullable(notificationToSend.getOnboardingTokenId()).ifPresent(value -> propertiesMap.put("onboardingTokenId", value));
Optional.ofNullable(notificationToSend.getPricingPlan()).ifPresent(value -> propertiesMap.put("pricingPlan", value));

if (Optional.ofNullable(notificationToSend.getInstitution()).isPresent()) {
Optional.ofNullable(notificationToSend.getInstitution().getDescription()).ifPresent(value -> propertiesMap.put("description", value));
Optional.ofNullable(notificationToSend.getInstitution().getInstitutionType()).ifPresent(value -> propertiesMap.put("institutionType", value.name()));
Optional.ofNullable(notificationToSend.getInstitution().getDigitalAddress()).ifPresent(value -> propertiesMap.put("digitalAddress", value));
Optional.ofNullable(notificationToSend.getInstitution().getAddress()).ifPresent(value -> propertiesMap.put("address", value));
Optional.ofNullable(notificationToSend.getInstitution().getTaxCode()).ifPresent(value -> propertiesMap.put("taxCode", value));
Optional.ofNullable(notificationToSend.getInstitution().getOrigin()).ifPresent(value -> propertiesMap.put("origin", value));
Optional.ofNullable(notificationToSend.getInstitution().getOriginId()).ifPresent(value -> propertiesMap.put("originId", value));
Optional.ofNullable(notificationToSend.getInstitution().getIstatCode()).ifPresent(value -> propertiesMap.put("istatCode", value));
Optional.ofNullable(notificationToSend.getInstitution().getCity()).ifPresent(value -> propertiesMap.put("city", value));
Optional.ofNullable(notificationToSend.getInstitution().getCountry()).ifPresent(value -> propertiesMap.put("country", value));
Optional.ofNullable(notificationToSend.getInstitution().getCounty()).ifPresent(value -> propertiesMap.put("county", value));
Optional.ofNullable(notificationToSend.getInstitution().getSubUnitCode()).ifPresent(value -> propertiesMap.put("subUnitCode", value));
Optional.ofNullable(notificationToSend.getInstitution().getCategory()).ifPresent(value -> propertiesMap.put("category", value));
Optional.ofNullable(notificationToSend.getInstitution().getSubUnitType()).ifPresent(value -> propertiesMap.put("subUnitType", value));
if (Optional.ofNullable(notificationToSend.getInstitution().getRootParent()).isPresent()) {
Optional.ofNullable(notificationToSend.getInstitution().getRootParent().getId()).ifPresent(value -> propertiesMap.put("root.parentId", value));
Optional.ofNullable(notificationToSend.getInstitution().getRootParent().getDescription()).ifPresent(value -> propertiesMap.put("root.parentDescription", value));
Optional.ofNullable(notificationToSend.getInstitution().getRootParent().getOriginId()).ifPresent(value -> propertiesMap.put("root.parentOriginId", value));
}
}

if (Optional.ofNullable(notificationToSend.getBilling()).isPresent()) {
Optional.ofNullable(notificationToSend.getBilling().getRecipientCode()).ifPresent(value -> propertiesMap.put("billing.recipientCode", value));
Optional.ofNullable(notificationToSend.getBilling().getTaxCodeInvoicing()).ifPresent(value -> propertiesMap.put("billing.TaxCodeInvoicing", value));
Optional.ofNullable(notificationToSend.getBilling().getVatNumber()).ifPresent(value -> propertiesMap.put("billing.VatNumber", value));
Optional.ofNullable(notificationToSend.getBilling().isPublicService()).ifPresent(value -> propertiesMap.put("billing.isPublicService", value ? "true" : "false"));
}

return propertiesMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ onboarding-functions.purge.all-from = 150
onboarding-functions.purge.all-to = 120
#property to invoke or not the user microservice
onboarding-functions.persist-users.send-mail = ${USER_MS_SEND_MAIL:true}
onboarding-functions.appinsights.connection-string=${APPLICATIONINSIGHTS_CONNECTION_STRING:InstrumentationKey=00000000-0000-0000-0000-000000000000}

#property to force the institution creation when a new onboarding to pnpg is performed only in pnpg environment
onboarding-function.force-institution-persist= ${FORCE_INSTITUTION_PERSIST:false}
onboarding-functions.force-institution-persist= ${FORCE_INSTITUTION_PERSIST:false}


## REST CLIENT #

quarkus.openapi-generator.user_registry_json.auth.api_key.api-key = ${USER_REGISTRY_API_KEY:example-api-key}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class CompletionServiceDefaultTest2 {
public static class ForceCreationProfile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
return Map.of("onboarding-function.force-institution-persist", "true");
return Map.of("onboarding-functions.force-institution-persist", "true");
}
}

Expand Down
Loading

0 comments on commit f12c399

Please sign in to comment.