Skip to content

Commit

Permalink
Merge branch 'feature/SELC-5423' into releases/0.3.3_SELC-5423
Browse files Browse the repository at this point in the history
# Conflicts:
#	apps/onboarding-functions/src/main/java/it/pagopa/selfcare/onboarding/service/NotificationEventResenderServiceDefault.java
  • Loading branch information
andrea-putzu committed Aug 27, 2024
2 parents 0f3e6e4 + 94c6263 commit 7dc911a
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 90 deletions.
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 RuntimeException(e);
} 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
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ void sendMessageWithError() {
.when(eventHubRestClient).sendMessage(anyString(), anyString());
ExecutionContext context = mock(ExecutionContext.class);
doReturn(Logger.getGlobal()).when(context).getLogger();
assertDoesNotThrow(() -> messageServiceDefault.send(context, onboarding, QueueEvent.ADD));
assertThrows(NotificationException.class, () -> messageServiceDefault.send(context, onboarding, QueueEvent.ADD));
verify(eventHubRestClient, times(1))
.sendMessage(anyString(), anyString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,21 +263,22 @@ void toNotificationToSendForEcWhenPartyRegistryThrowsException() {
when(registryProxyInstitutionsApi.findInstitutionUsingGET(any(), any(), any()))
.thenThrow(new RuntimeException("Error"));

NotificationToSend notification = sapNotificationBuilder.buildNotificationToSend(onboarding, token, institution, QueueEvent.ADD);

assertNotNull(notification);
assertNull(notification.getClosedAt());
assertEquals("ACTIVE", notification.getState());
assertNotEquals(onboarding.getId(), notification.getId());
assertEquals(onboarding.getActivatedAt(), notification.getCreatedAt().toLocalDateTime());
assertEquals(onboarding.getActivatedAt(), notification.getUpdatedAt().toLocalDateTime());
assertNull(notification.getBilling().getTaxCodeInvoicing());
assertNull(notification.getBilling().isPublicServices());
assertNull(notification.getNotificationType());
assertEquals(NotificationType.ADD_INSTITUTE, notification.getType());
assertNull(notification.getBilling().getTaxCodeInvoicing());
assertEquals("taxCodeInvoicing", notification.getInstitution().getTaxCode());
assertNull(notification.getInstitution().getIstatCode());
// NotificationToSend notification = sapNotificationBuilder.buildNotificationToSend(onboarding, token, institution, QueueEvent.ADD);
assertThrows(RuntimeException.class, () -> sapNotificationBuilder.buildNotificationToSend(onboarding, token, institution, QueueEvent.ADD));

// assertNotNull(notification);
// assertNull(notification.getClosedAt());
// assertEquals("ACTIVE", notification.getState());
// assertNotEquals(onboarding.getId(), notification.getId());
// assertEquals(onboarding.getActivatedAt(), notification.getCreatedAt().toLocalDateTime());
// assertEquals(onboarding.getActivatedAt(), notification.getUpdatedAt().toLocalDateTime());
// assertNull(notification.getBilling().getTaxCodeInvoicing());
// assertNull(notification.getBilling().isPublicServices());
// assertNull(notification.getNotificationType());
// assertEquals(NotificationType.ADD_INSTITUTE, notification.getType());
// assertNull(notification.getBilling().getTaxCodeInvoicing());
// assertEquals("taxCodeInvoicing", notification.getInstitution().getTaxCode());
// assertNull(notification.getInstitution().getIstatCode());
}

@Test
Expand Down

0 comments on commit 7dc911a

Please sign in to comment.