Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SELC-5423] Refactor notification #455

Merged
merged 7 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,34 +87,29 @@ public void send(ExecutionContext context, Onboarding onboarding, QueueEvent que
return;
}

try {
context.getLogger().info(() -> String.format("Getting product info for onboarding with ID %s and productId %s", onboarding.getId(), onboarding.getProductId()));
Product product = productService.getProduct(onboarding.getProductId());
if (product.getConsumers() == null || product.getConsumers().isEmpty()) {
context.getLogger().warning(() -> String.format("Node consumers is null or empty for product with ID %s", onboarding.getProductId()));
return;
}
context.getLogger().info(() -> String.format("Getting product info for onboarding with ID %s and productId %s", onboarding.getId(), onboarding.getProductId()));
Product product = productService.getProduct(onboarding.getProductId());
if (product.getConsumers() == null || product.getConsumers().isEmpty()) {
context.getLogger().warning(() -> String.format("Node consumers is null or empty for product with ID %s", onboarding.getProductId()));
return;
}

if(Objects.isNull(queueEvent)) {
queueEvent = queueEventExaminer.determineEventType(onboarding);
}
if(Objects.isNull(queueEvent)) {
queueEvent = queueEventExaminer.determineEventType(onboarding);
}

context.getLogger().info(() -> String.format("Retrieving institution having ID %s", onboarding.getInstitution().getId()));
InstitutionResponse institution = institutionApi.retrieveInstitutionByIdUsingGET(onboarding.getInstitution().getId());
context.getLogger().info(() -> String.format("Retrieving institution having ID %s", onboarding.getInstitution().getId()));
InstitutionResponse institution = institutionApi.retrieveInstitutionByIdUsingGET(onboarding.getInstitution().getId());

Token token = tokenRepository.findByOnboardingId(onboarding.getId()).orElse(null);
NotificationsResources notificationsResources = new NotificationsResources(onboarding, institution, token, queueEvent);
for (String consumer : product.getConsumers()) {
NotificationConfig.Consumer consumerConfig = notificationConfig.consumers().get(consumer.toLowerCase());
prepareAndSendNotification(context, product, consumerConfig, notificationsResources, notificationEventTraceId);
}
} catch (Exception e) {
context.getLogger().severe(String.format("Error sending notification for onboarding with ID %s %s", onboarding.getId(), Arrays.toString(e.getStackTrace())));
telemetryClient.trackEvent(EVENT_ONBOARDING_FN_NAME, onboardingEventFailureMap(onboarding, e, notificationEventTraceId), Map.of(EVENT_ONBOARDING_INSTTITUTION_FN_FAILURE, 1D));
Token token = tokenRepository.findByOnboardingId(onboarding.getId()).orElse(null);
NotificationsResources notificationsResources = new NotificationsResources(onboarding, institution, token, queueEvent);
for (String consumer : product.getConsumers()) {
NotificationConfig.Consumer consumerConfig = notificationConfig.consumers().get(consumer.toLowerCase());
prepareAndSendNotification(context, product, consumerConfig, notificationsResources, notificationEventTraceId);
}
}

private void prepareAndSendNotification(ExecutionContext context, Product product, NotificationConfig.Consumer consumer, NotificationsResources notificationsResources, String notificationEventTraceId) throws JsonProcessingException {
private void prepareAndSendNotification(ExecutionContext context, Product product, NotificationConfig.Consumer consumer, NotificationsResources notificationsResources, String notificationEventTraceId) {
NotificationBuilder notificationBuilder = notificationBuilderFactory.create(consumer);
if (notificationBuilder.shouldSendNotification(notificationsResources.getOnboarding(), notificationsResources.getInstitution())) {
NotificationToSend notificationToSend = notificationBuilder.buildNotificationToSend(notificationsResources.getOnboarding(), notificationsResources.getToken(), notificationsResources.getInstitution(), notificationsResources.getQueueEvent());
Expand All @@ -125,19 +120,22 @@ private void prepareAndSendNotification(ExecutionContext context, Product produc
}
}

private void sendNotification(ExecutionContext context, String topic, NotificationToSend notificationToSend, String notificationEventTraceId) throws JsonProcessingException {
String message = mapper.writeValueAsString(notificationToSend);
context.getLogger().info(() -> String.format("Sending notification on topic: %s with message: %s", topic, message));

private void sendNotification(ExecutionContext context, String topic, NotificationToSend notificationToSend, String notificationEventTraceId) {
String message = null;
try {
eventHubRestClient.sendMessage(topic, message);
telemetryClient.trackEvent(EVENT_ONBOARDING_FN_NAME, notificationEventMap(notificationToSend, topic, notificationEventTraceId), Map.of(EVENT_ONBOARDING_INSTTITUTION_FN_SUCCESS, 1D));
} catch (Exception e) {
throw new NotificationException(e.getMessage());
message = mapper.writeValueAsString(notificationToSend);
} catch (JsonProcessingException e) {
throw new NotificationException("Notification cannot be serialized");
} finally {
String finalMessage = message;
context.getLogger().info(() -> String.format("Sending notification on topic: %s with message: %s", topic, finalMessage));
}

eventHubRestClient.sendMessage(topic, message);
telemetryClient.trackEvent(EVENT_ONBOARDING_FN_NAME, notificationEventMap(notificationToSend, topic, notificationEventTraceId), Map.of(EVENT_ONBOARDING_INSTTITUTION_FN_SUCCESS, 1D));
}

private void sendTestEnvProductsNotification(ExecutionContext context, Product product, String topic, NotificationToSend notificationToSend, String notificationEventTraceId) throws JsonProcessingException {
private void sendTestEnvProductsNotification(ExecutionContext context, Product product, String topic, NotificationToSend notificationToSend, String notificationEventTraceId) {
context.getLogger().info(() -> String.format("Starting sendTestEnvProductsNotification with testEnv %s", product.getTestEnvProductIds()));
if (product.getTestEnvProductIds() != null) {
for (String testEnvProductId : product.getTestEnvProductIds()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,13 @@ private PaymentServiceProvider toSetPaymentServiceProvider(PaymentServiceProvide

@Override
public void retrieveAndSetGeographicData(InstitutionToNotify institution) {
try {
InstitutionResource institutionProxyInfo = proxyRegistryInstitutionApi.findInstitutionUsingGET(institution.getTaxCode(), null, null);
institution.setIstatCode(institutionProxyInfo.getIstatCode());
institution.setCategory(institutionProxyInfo.getCategory());
GeographicTaxonomyResource geographicTaxonomies = geographicTaxonomiesApi.retrieveGeoTaxonomiesByCodeUsingGET(institutionProxyInfo.getIstatCode());
institution.setCounty(geographicTaxonomies.getProvinceAbbreviation());
institution.setCountry(geographicTaxonomies.getCountryAbbreviation());
institution.setCity(geographicTaxonomies.getDesc().replace(DESCRIPTION_TO_REPLACE_REGEX, ""));
} catch (Exception e) {
log.warn("Error while searching institution {} on IPA, {} ", institution.getTaxCode(), e.getMessage());
institution.setIstatCode(null);
}
InstitutionResource institutionProxyInfo = proxyRegistryInstitutionApi.findInstitutionUsingGET(institution.getTaxCode(), null, null);
institution.setIstatCode(institutionProxyInfo.getIstatCode());
institution.setCategory(institutionProxyInfo.getCategory());
GeographicTaxonomyResource geographicTaxonomies = geographicTaxonomiesApi.retrieveGeoTaxonomiesByCodeUsingGET(institutionProxyInfo.getIstatCode());
institution.setCounty(geographicTaxonomies.getProvinceAbbreviation());
institution.setCountry(geographicTaxonomies.getCountryAbbreviation());
institution.setCity(geographicTaxonomies.getDesc().replace(DESCRIPTION_TO_REPLACE_REGEX, ""));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,40 +80,35 @@ public InstitutionToNotify retrieveInstitution(InstitutionResponse institution)
}
@Override
public void retrieveAndSetGeographicData(InstitutionToNotify institutionToNotify) {
try {
GeographicTaxonomyResource geographicTaxonomies;
if (institutionToNotify.getSubUnitType() != null) {
switch (Objects.requireNonNull(institutionToNotify.getSubUnitType())) {
case "UO" -> {
UOResource organizationUnit = proxyRegistryUoApi.findByUnicodeUsingGET1(institutionToNotify.getSubUnitCode(), null);
institutionToNotify.setIstatCode(organizationUnit.getCodiceComuneISTAT());
geographicTaxonomies = geographicTaxonomiesApi.retrieveGeoTaxonomiesByCodeUsingGET(organizationUnit.getCodiceComuneISTAT());
}
case "AOO" -> {
AOOResource homogeneousOrganizationalArea = proxyRegistryAooApi.findByUnicodeUsingGET(institutionToNotify.getSubUnitCode(), null);
institutionToNotify.setIstatCode(homogeneousOrganizationalArea.getCodiceComuneISTAT());
geographicTaxonomies = geographicTaxonomiesApi.retrieveGeoTaxonomiesByCodeUsingGET(homogeneousOrganizationalArea.getCodiceComuneISTAT());
}
default -> {
InstitutionResource proxyInfo = proxyRegistryInstitutionApi.findInstitutionUsingGET(institutionToNotify.getTaxCode(), null, null);
institutionToNotify.setIstatCode(proxyInfo.getIstatCode());
geographicTaxonomies = geographicTaxonomiesApi.retrieveGeoTaxonomiesByCodeUsingGET(proxyInfo.getIstatCode());
}
GeographicTaxonomyResource geographicTaxonomies;
if (institutionToNotify.getSubUnitType() != null) {
switch (Objects.requireNonNull(institutionToNotify.getSubUnitType())) {
case "UO" -> {
UOResource organizationUnit = proxyRegistryUoApi.findByUnicodeUsingGET1(institutionToNotify.getSubUnitCode(), null);
institutionToNotify.setIstatCode(organizationUnit.getCodiceComuneISTAT());
geographicTaxonomies = geographicTaxonomiesApi.retrieveGeoTaxonomiesByCodeUsingGET(organizationUnit.getCodiceComuneISTAT());
}
case "AOO" -> {
AOOResource homogeneousOrganizationalArea = proxyRegistryAooApi.findByUnicodeUsingGET(institutionToNotify.getSubUnitCode(), null);
institutionToNotify.setIstatCode(homogeneousOrganizationalArea.getCodiceComuneISTAT());
geographicTaxonomies = geographicTaxonomiesApi.retrieveGeoTaxonomiesByCodeUsingGET(homogeneousOrganizationalArea.getCodiceComuneISTAT());
}
default -> {
InstitutionResource proxyInfo = proxyRegistryInstitutionApi.findInstitutionUsingGET(institutionToNotify.getTaxCode(), null, null);
institutionToNotify.setIstatCode(proxyInfo.getIstatCode());
geographicTaxonomies = geographicTaxonomiesApi.retrieveGeoTaxonomiesByCodeUsingGET(proxyInfo.getIstatCode());
}
} else {
InstitutionResource proxyInfo = proxyRegistryInstitutionApi.findInstitutionUsingGET(institutionToNotify.getTaxCode(), null, null);
institutionToNotify.setIstatCode(proxyInfo.getIstatCode());
geographicTaxonomies = geographicTaxonomiesApi.retrieveGeoTaxonomiesByCodeUsingGET(proxyInfo.getIstatCode());
}
} else {
InstitutionResource proxyInfo = proxyRegistryInstitutionApi.findInstitutionUsingGET(institutionToNotify.getTaxCode(), null, null);
institutionToNotify.setIstatCode(proxyInfo.getIstatCode());
geographicTaxonomies = geographicTaxonomiesApi.retrieveGeoTaxonomiesByCodeUsingGET(proxyInfo.getIstatCode());
}

if (geographicTaxonomies != null) {
institutionToNotify.setCounty(geographicTaxonomies.getProvinceAbbreviation());
institutionToNotify.setCountry(geographicTaxonomies.getCountryAbbreviation());
institutionToNotify.setCity(geographicTaxonomies.getDesc().replace(DESCRIPTION_TO_REPLACE_REGEX, ""));
}
} catch (Exception e) {
log.warn("Error while searching institution {} on IPA, {} ", institutionToNotify.getDescription(), e.getMessage());
institutionToNotify.setIstatCode(null);
if (geographicTaxonomies != null) {
institutionToNotify.setCounty(geographicTaxonomies.getProvinceAbbreviation());
institutionToNotify.setCountry(geographicTaxonomies.getCountryAbbreviation());
institutionToNotify.setCity(geographicTaxonomies.getDesc().replace(DESCRIPTION_TO_REPLACE_REGEX, ""));
}
}
@Override
Expand Down
Loading
Loading