Skip to content

Commit

Permalink
Removed telemetry client for sending emails
Browse files Browse the repository at this point in the history
  • Loading branch information
pierpaolo.didato@emeal.nttdata.com authored and pierpaolo.didato@emeal.nttdata.com committed Aug 22, 2024
1 parent ba2e216 commit f3a3c47
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 318 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,31 +189,31 @@ public void saveToken(@DurableActivityTrigger(name = "onboardingString") String
@FunctionName(SEND_MAIL_REGISTRATION_FOR_CONTRACT)
public void sendMailRegistrationForContract(@DurableActivityTrigger(name = "onboardingString") String onboardingWorkflowString, final ExecutionContext context) {
context.getLogger().info(String.format(FORMAT_LOGGER_ONBOARDING_STRING, SEND_MAIL_REGISTRATION_FOR_CONTRACT, onboardingWorkflowString));
service.sendMailRegistrationForContract(context, readOnboardingWorkflowValue(objectMapper, onboardingWorkflowString));
service.sendMailRegistrationForContract(readOnboardingWorkflowValue(objectMapper, onboardingWorkflowString));
}

@FunctionName(SEND_MAIL_REGISTRATION_FOR_CONTRACT_WHEN_APPROVE_ACTIVITY)
public void sendMailRegistrationForContractWhenApprove(@DurableActivityTrigger(name = "onboardingString") String onboardingWorkflowString, final ExecutionContext context) {
context.getLogger().info(String.format(FORMAT_LOGGER_ONBOARDING_STRING, SEND_MAIL_REGISTRATION_FOR_CONTRACT_WHEN_APPROVE_ACTIVITY, onboardingWorkflowString));
service.sendMailRegistrationForContractWhenApprove(context, readOnboardingWorkflowValue(objectMapper, onboardingWorkflowString));
service.sendMailRegistrationForContractWhenApprove(readOnboardingWorkflowValue(objectMapper, onboardingWorkflowString));
}

@FunctionName(SEND_MAIL_REGISTRATION_REQUEST_ACTIVITY)
public void sendMailRegistration(@DurableActivityTrigger(name = "onboardingString") String onboardingString, final ExecutionContext context) {
context.getLogger().info(String.format(FORMAT_LOGGER_ONBOARDING_STRING, SEND_MAIL_REGISTRATION_REQUEST_ACTIVITY, onboardingString));
service.sendMailRegistration(context, readOnboardingValue(objectMapper, onboardingString));
service.sendMailRegistration(readOnboardingValue(objectMapper, onboardingString));
}

@FunctionName(SEND_MAIL_REGISTRATION_APPROVE_ACTIVITY)
public void sendMailRegistrationApprove(@DurableActivityTrigger(name = "onboardingString") String onboardingString, final ExecutionContext context) {
context.getLogger().info(String.format(FORMAT_LOGGER_ONBOARDING_STRING, SEND_MAIL_REGISTRATION_APPROVE_ACTIVITY, onboardingString));
service.sendMailRegistrationApprove(context, readOnboardingValue(objectMapper, onboardingString));
service.sendMailRegistrationApprove(readOnboardingValue(objectMapper, onboardingString));
}

@FunctionName(SEND_MAIL_ONBOARDING_APPROVE_ACTIVITY)
public void sendMailOnboardingApprove(@DurableActivityTrigger(name = "onboardingString") String onboardingString, final ExecutionContext context) {
context.getLogger().info(String.format(FORMAT_LOGGER_ONBOARDING_STRING, SEND_MAIL_ONBOARDING_APPROVE_ACTIVITY, onboardingString));
service.sendMailOnboardingApprove(context, readOnboardingValue(objectMapper, onboardingString));
service.sendMailOnboardingApprove(readOnboardingValue(objectMapper, onboardingString));
}

@FunctionName(CREATE_INSTITUTION_ACTIVITY)
Expand All @@ -222,22 +222,22 @@ public String createInstitutionAndPersistInstitutionId(@DurableActivityTrigger(n
return completionService.createInstitutionAndPersistInstitutionId(readOnboardingValue(objectMapper, onboardingString));
}

@FunctionName(CREATE_ONBOARDING_ACTIVITY)
public void createOnboarding(@DurableActivityTrigger(name = "onboardingString") String onboardingString, final ExecutionContext context) {
context.getLogger().info(String.format(FORMAT_LOGGER_ONBOARDING_STRING, CREATE_ONBOARDING_ACTIVITY, onboardingString));
completionService.persistOnboarding(readOnboardingValue(objectMapper, onboardingString));
}

@FunctionName(STORE_ONBOARDING_ACTIVATEDAT)
public void storeOnboardingActivatedAt(@DurableActivityTrigger(name = "onboardingString") String onboardingString, final ExecutionContext context) {
context.getLogger().info(String.format(FORMAT_LOGGER_ONBOARDING_STRING, STORE_ONBOARDING_ACTIVATEDAT, onboardingString));
completionService.persistActivatedAt(readOnboardingValue(objectMapper, onboardingString));
}

@FunctionName(CREATE_ONBOARDING_ACTIVITY)
public void createOnboarding(@DurableActivityTrigger(name = "onboardingString") String onboardingString, final ExecutionContext context) {
context.getLogger().info(String.format(FORMAT_LOGGER_ONBOARDING_STRING, CREATE_ONBOARDING_ACTIVITY, onboardingString));
completionService.persistOnboarding(readOnboardingValue(objectMapper, onboardingString));
}

@FunctionName(SEND_MAIL_COMPLETION_ACTIVITY)
public void sendMailCompletion(@DurableActivityTrigger(name = "onboardingString") String onboardingWorkflowString, final ExecutionContext context) {
context.getLogger().info(String.format(FORMAT_LOGGER_ONBOARDING_STRING, SEND_MAIL_COMPLETION_ACTIVITY, onboardingWorkflowString));
completionService.sendCompletedEmail(context, readOnboardingWorkflowValue(objectMapper, onboardingWorkflowString));
completionService.sendCompletedEmail(readOnboardingWorkflowValue(objectMapper, onboardingWorkflowString));
}

@FunctionName(SEND_MAIL_REJECTION_ACTIVITY)
Expand All @@ -255,7 +255,7 @@ public void createOnboardedUsers(@DurableActivityTrigger(name = "onboardingStrin
@FunctionName(SEND_MAIL_COMPLETION_AGGREGATE_ACTIVITY)
public void sendMailCompletionAggregate(@DurableActivityTrigger(name = "onboardingString") String onboardingString, final ExecutionContext context) {
context.getLogger().info(String.format(FORMAT_LOGGER_ONBOARDING_STRING, SEND_MAIL_COMPLETION_AGGREGATE_ACTIVITY, onboardingString));
completionService.sendCompletedEmailAggregate(context, readOnboardingValue(objectMapper, onboardingString));
completionService.sendCompletedEmailAggregate(readOnboardingValue(objectMapper, onboardingString));
}

@FunctionName(CREATE_AGGREGATE_ONBOARDING_REQUEST_ACTIVITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public interface CompletionService {

void persistActivatedAt(Onboarding onboarding);

void sendCompletedEmail(ExecutionContext context, OnboardingWorkflow onboardingWorkflow);
void sendCompletedEmail(OnboardingWorkflow onboardingWorkflow);

void sendCompletedEmailAggregate(ExecutionContext context, Onboarding onboarding);
void sendCompletedEmailAggregate(Onboarding onboarding);

void persistUsers(Onboarding onboarding);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package it.pagopa.selfcare.onboarding.service;

import com.microsoft.applicationinsights.TelemetryClient;
import com.microsoft.applicationinsights.TelemetryConfiguration;
import com.microsoft.azure.functions.ExecutionContext;
import it.pagopa.selfcare.onboarding.common.InstitutionPaSubunitType;
import it.pagopa.selfcare.onboarding.common.InstitutionType;
Expand All @@ -21,7 +19,6 @@
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.Response;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.microprofile.config.inject.ConfigProperty;
Expand All @@ -36,14 +33,15 @@

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import static it.pagopa.selfcare.onboarding.common.PartyRole.MANAGER;
import static it.pagopa.selfcare.onboarding.common.WorkflowType.CONFIRMATION_AGGREGATE;
import static it.pagopa.selfcare.onboarding.service.NotificationEventServiceDefault.*;
import static it.pagopa.selfcare.onboarding.service.OnboardingService.USERS_FIELD_LIST;
import static it.pagopa.selfcare.onboarding.utils.Utils.TelemetryConstants.*;
import static jakarta.ws.rs.core.Response.Status.Family.SUCCESSFUL;

@ApplicationScoped
Expand Down Expand Up @@ -79,7 +77,6 @@ public class CompletionServiceDefault implements CompletionService {
private final NotificationService notificationService;
private final ProductService productService;
private final OnboardingMapper onboardingMapper;
private final TelemetryClient telemetryClient;
private final boolean hasToSendEmail;
private final boolean forceInstitutionCreation;

Expand All @@ -92,22 +89,17 @@ public CompletionServiceDefault(ProductService productService,
OnboardingRepository onboardingRepository,
TokenRepository tokenRepository,
@ConfigProperty(name = "onboarding-functions.persist-users.send-mail") boolean hasToSendEmail,
@ConfigProperty(name = "onboarding-functions.force-institution-persist")boolean forceInstitutionCreation,
@Context @ConfigProperty(name = "onboarding-functions.appinsights.connection-string") String appInsightsConnectionString) {
@ConfigProperty(name = "onboarding-functions.force-institution-persist")boolean forceInstitutionCreation) {
this.institutionMapper = institutionMapper;
this.onboardingRepository = onboardingRepository;
this.tokenRepository = tokenRepository;
TelemetryConfiguration telemetryConfiguration = TelemetryConfiguration.createDefault();
telemetryConfiguration.setConnectionString(appInsightsConnectionString);
this.productService = productService;
this.notificationService = notificationService;
this.onboardingMapper = onboardingMapper;
this.userMapper = userMapper;
this.productMapper = productMapper;
this.hasToSendEmail = hasToSendEmail;
this.forceInstitutionCreation = forceInstitutionCreation;
this.telemetryClient = new TelemetryClient(telemetryConfiguration);
this.telemetryClient.getContext().getOperation().setName(OPERATION_NAME);
}

@Override
Expand Down Expand Up @@ -144,20 +136,14 @@ public InstitutionResponse createOrRetrieveInstitution(Onboarding onboarding) {
}

@Override
public void sendCompletedEmail(ExecutionContext context, OnboardingWorkflow onboardingWorkflow) {
public void sendCompletedEmail(OnboardingWorkflow onboardingWorkflow) {
Onboarding onboarding = onboardingWorkflow.getOnboarding();
List<String> destinationMails = getDestinationMails(onboarding);
destinationMails.add(onboarding.getInstitution().getDigitalAddress());
Product product = productService.getProductIsValid(onboarding.getProductId());
try {
notificationService.sendCompletedEmail(onboarding.getInstitution().getDescription(),
destinationMails, product, onboarding.getInstitution().getInstitutionType(),
onboardingWorkflow);
telemetryClient.trackEvent(EVENT_ONBOARDING_FN_NAME, onboardingEventMap(onboarding), Map.of(EVENT_SEND_COMPLETION_FN_SUCCESS, 1D));
} catch (Throwable e) {
telemetryClient.trackEvent(EVENT_ONBOARDING_FN_NAME, onboardingEventFailureMap(onboarding, new Exception(e)), Map.of(EVENT_SEND_COMPLETION_FN_FAILURE, 1D));
context.getLogger().severe(String.format("Impossible to send completion email for onboarding with ID %s %s", onboarding.getId(), Arrays.toString(e.getStackTrace())));
}
notificationService.sendCompletedEmail(onboarding.getInstitution().getDescription(),
destinationMails, product, onboarding.getInstitution().getInstitutionType(),
onboardingWorkflow);
}

@Override
Expand Down Expand Up @@ -191,13 +177,7 @@ public String createDelegation(Onboarding onboarding) {
public void sendMailRejection(ExecutionContext context, Onboarding onboarding) {
List<String> destinationMails = Collections.singletonList(onboarding.getInstitution().getDigitalAddress());
Product product = productService.getProductIsValid(onboarding.getProductId());
try {
notificationService.sendMailRejection(destinationMails, product, onboarding.getReasonForReject());
telemetryClient.trackEvent(EVENT_ONBOARDING_FN_NAME, onboardingEventMap(onboarding), Map.of(EVENT_SEND_REJECTION_FN_SUCCESS, 1D));
} catch (Throwable e) {
telemetryClient.trackEvent(EVENT_ONBOARDING_FN_NAME, onboardingEventFailureMap(onboarding, new Exception(e)), Map.of(EVENT_SEND_REJECTION_FN_FAILURE, 1D));
context.getLogger().severe(String.format("Impossible to send rejection email for onboarding with ID %s %s", onboarding.getId(), Arrays.toString(e.getStackTrace())));
}
notificationService.sendMailRejection(destinationMails, product, onboarding.getReasonForReject());
}

@Override
Expand Down Expand Up @@ -236,16 +216,10 @@ public void persistActivatedAt(Onboarding onboarding) {
}

@Override
public void sendCompletedEmailAggregate(ExecutionContext context, Onboarding onboarding) {
public void sendCompletedEmailAggregate(Onboarding onboarding) {
List<String> destinationMails = getDestinationMails(onboarding);
destinationMails.add(onboarding.getInstitution().getDigitalAddress());
try {
notificationService.sendCompletedEmailAggregate(onboarding.getAggregator().getDescription(), destinationMails);
telemetryClient.trackEvent(EVENT_ONBOARDING_FN_NAME, onboardingEventMap(onboarding), Map.of(EVENT_SEND_COMPLETION_AGGREGATE_FN_SUCCESS, 1D));
} catch (Throwable e) {
telemetryClient.trackEvent(EVENT_ONBOARDING_FN_NAME, onboardingEventFailureMap(onboarding, new Exception(e)), Map.of(EVENT_SEND_COMPLETION_AGGREGATE_FN_FAILURE, 1D));
context.getLogger().severe(String.format("Impossible to send completion aggregate email for onboarding with ID %s %s", onboarding.getId(), Arrays.toString(e.getStackTrace())));
}
notificationService.sendCompletedEmailAggregate(onboarding.getAggregator().getDescription(), destinationMails);
}

@Override
Expand All @@ -259,7 +233,7 @@ public String createAggregateOnboardingRequest(OnboardingAggregateOrchestratorIn

@Override
public void sendTestEmail(ExecutionContext context) {
notificationService.sendTestEmail(context);
notificationService.sendTestEmail(context);
}

private static DelegationRequest getDelegationRequest(Onboarding onboarding) {
Expand Down
Loading

0 comments on commit f3a3c47

Please sign in to comment.