From d49b99ff7efd3f2168d71772005e5ab1907b6406 Mon Sep 17 00:00:00 2001 From: benedwards Date: Wed, 28 Aug 2024 11:03:07 +0100 Subject: [PATCH] Added validating in for invalid phone numbers or email addresses --- build.gradle | 4 +- .../scheduler/UrgentStatusScheduler.java | 9 +- .../service/JurorCommsLetterServiceImpl.java | 26 +++-- .../JurorCommsSentToCourtServiceImpl.java | 51 ++++++---- .../JurorCommsWeeklyInfoServiceImpl.java | 27 +++--- .../JurorDashboardSmartSurveyImportImpl.java | 28 +++--- .../api/juror/notify/NotifyAdapterImpl.java | 9 -- ...ExcusedCompletedCourtCommsServiceImpl.java | 95 ++++++++++--------- .../juror/service/MessagesServiceImpl.java | 29 ++++-- .../hmcts/juror/api/moj/utils/NotifyUtil.java | 28 ++++++ 10 files changed, 189 insertions(+), 117 deletions(-) create mode 100644 src/main/java/uk/gov/hmcts/juror/api/moj/utils/NotifyUtil.java diff --git a/build.gradle b/build.gradle index b95193338..10419590e 100644 --- a/build.gradle +++ b/build.gradle @@ -284,10 +284,10 @@ checkstyle { } pmdTest { - maxFailures = 294 + maxFailures = 292 } pmdMain { - maxFailures = 776 + maxFailures = 752 } pmd { maxFailures = 0 diff --git a/src/main/java/uk/gov/hmcts/juror/api/bureau/scheduler/UrgentStatusScheduler.java b/src/main/java/uk/gov/hmcts/juror/api/bureau/scheduler/UrgentStatusScheduler.java index 26491ea2b..1ddc01fc0 100644 --- a/src/main/java/uk/gov/hmcts/juror/api/bureau/scheduler/UrgentStatusScheduler.java +++ b/src/main/java/uk/gov/hmcts/juror/api/bureau/scheduler/UrgentStatusScheduler.java @@ -73,7 +73,8 @@ public SchedulerServiceClient.Result process() { SecurityUtil.BUREAU_OWNER); if (jurorDetails == null) { - log.error("Can not find active bureau owned juror pool for juror: " + backlogItem.getJurorNumber()); + log.error("Can not find active bureau owned juror pool for juror: {}", + backlogItem.getJurorNumber()); failedToFindJurorCount++; continue; } @@ -104,9 +105,9 @@ public SchedulerServiceClient.Result process() { ? SchedulerServiceClient.Result.Status.SUCCESS : SchedulerServiceClient.Result.Status.PARTIAL_SUCCESS, null, Map.of( - "TOTAL_PROCESSED", "" + totalResponsesProcessed, - "TOTAL_MARKED_URGENT", "" + totalUrgentResponses, - "TOTAL_FAILED_TO_FIND", "" + failedToFindJurorCount + "TOTAL_PROCESSED", String.valueOf(totalResponsesProcessed), + "TOTAL_MARKED_URGENT", String.valueOf(totalUrgentResponses), + "TOTAL_FAILED_TO_FIND", String.valueOf(failedToFindJurorCount) )); } diff --git a/src/main/java/uk/gov/hmcts/juror/api/bureau/service/JurorCommsLetterServiceImpl.java b/src/main/java/uk/gov/hmcts/juror/api/bureau/service/JurorCommsLetterServiceImpl.java index 264b59f26..98d11e69a 100644 --- a/src/main/java/uk/gov/hmcts/juror/api/bureau/service/JurorCommsLetterServiceImpl.java +++ b/src/main/java/uk/gov/hmcts/juror/api/bureau/service/JurorCommsLetterServiceImpl.java @@ -15,6 +15,7 @@ import uk.gov.hmcts.juror.api.moj.domain.JurorPool; import uk.gov.hmcts.juror.api.moj.repository.BulkPrintDataRepository; import uk.gov.hmcts.juror.api.moj.repository.JurorPoolRepository; +import uk.gov.hmcts.juror.api.moj.utils.NotifyUtil; import uk.gov.hmcts.juror.api.moj.utils.SecurityUtil; import java.text.SimpleDateFormat; @@ -54,6 +55,7 @@ public SchedulerServiceClient.Result process() { log.debug("jurorCommsPrintFiles {}", bulkPrintDataNotifyCommsList.size()); int commsSent = 0; int commsfailed = 0; + int invalidEmailAddress = 0; if (!bulkPrintDataNotifyCommsList.isEmpty()) { for (BulkPrintDataNotifyComms printFile : bulkPrintDataNotifyCommsList) { try { @@ -74,18 +76,19 @@ public SchedulerServiceClient.Result process() { updatePrintFiles(printFile); commsSent++; } catch (JurorCommsNotificationServiceException e) { - log.error( - "Unable to send Letter comms for {} : {} {}", - printFile.getJurorNo(), - e.getMessage(), - e.getCause().toString() - ); - commsfailed++; + if (NotifyUtil.isInvalidEmailAddressError(e.getCause())) { + invalidEmailAddress++; + } else { + log.error( + "Unable to send Letter comms for {}", + printFile.getJurorNo(), e + ); + commsfailed++; + } } catch (Exception e) { commsfailed++; log.error("Letter Comms Processing : Juror Comms failed : {}", e.getMessage()); } - } log.info("LetterService : Summary, identified:{}, sent:{}, failed:{},", bulkPrintDataNotifyCommsList.size(), commsSent, commsfailed @@ -99,8 +102,11 @@ public SchedulerServiceClient.Result process() { commsfailed == 0 ? SchedulerServiceClient.Result.Status.SUCCESS : SchedulerServiceClient.Result.Status.PARTIAL_SUCCESS, null, - Map.of("COMMS_FAILED", "" + commsfailed, - "COMMNS_SENT", "" + commsSent)); + Map.of( + "COMMS_FAILED", String.valueOf(commsfailed), + "COMMNS_SENT", String.valueOf(commsSent), + "INVALID_EMAIL_ADDRESS", String.valueOf(invalidEmailAddress) + )); } /** diff --git a/src/main/java/uk/gov/hmcts/juror/api/bureau/service/JurorCommsSentToCourtServiceImpl.java b/src/main/java/uk/gov/hmcts/juror/api/bureau/service/JurorCommsSentToCourtServiceImpl.java index 232213cd2..cf7c07a19 100644 --- a/src/main/java/uk/gov/hmcts/juror/api/bureau/service/JurorCommsSentToCourtServiceImpl.java +++ b/src/main/java/uk/gov/hmcts/juror/api/bureau/service/JurorCommsSentToCourtServiceImpl.java @@ -16,6 +16,7 @@ import uk.gov.hmcts.juror.api.moj.repository.JurorPoolQueries; import uk.gov.hmcts.juror.api.moj.repository.JurorPoolRepository; import uk.gov.hmcts.juror.api.moj.service.AppSettingService; +import uk.gov.hmcts.juror.api.moj.utils.NotifyUtil; import java.text.SimpleDateFormat; import java.util.Date; @@ -59,6 +60,8 @@ public SchedulerServiceClient.Result process() { Integer notificationsSent; int errorCount = 0; + int errorInvalidEmailCount = 0; + int errorInvalidPhoneCount = 0; int successCountEmail = 0; int successCountSms = 0; int errorCountEmail = 0; @@ -118,18 +121,29 @@ public SchedulerServiceClient.Result process() { successCount++; } catch (JurorCommsNotificationServiceException e) { - log.error( - "Unable to send sent to court comms for {} : {} {}", - jurorDetail.getJurorNumber(), - e.getMessage(), - e.getCause().toString() - ); - errorCount++; + boolean isError = false; if (isEmail) { - errorCountEmail++; + if (NotifyUtil.isInvalidEmailAddressError(e.getCause())) { + errorInvalidEmailCount++; + } else { + isError = true; + errorCountEmail++; + } } if (isSms) { - errorCountSms++; + if (NotifyUtil.isInvalidPhoneNumberError(e.getCause())) { + errorInvalidPhoneCount++; + } else { + isError = true; + errorCountSms++; + } + } + if (isError) { + errorCount++; + log.error( + "Unable to send sent to court comms for {}", + jurorDetail.getJurorNumber(), e + ); } if (notificationsSent.equals(EMAIL_NOTIFICATION_SENT)) { jurorDetail.getJuror().setNotifications(notificationsSent); @@ -152,14 +166,17 @@ public SchedulerServiceClient.Result process() { ? SchedulerServiceClient.Result.Status.SUCCESS : SchedulerServiceClient.Result.Status.PARTIAL_SUCCESS, null, Map.of( - "SUCCESS_COUNT_EMAIL", "" + successCountEmail, - "SUCCESS_COUNT_SMS", "" + successCountSms, - "ERROR_COUNT_EMAIL", "" + errorCountEmail, - "ERROR_COUNT_SMS", "" + errorCountSms, - - "SUCCESS_COUNT", "" + successCount, - "ERROR_COUNT", "" + errorCount, - "TOTAL_JURORS", "" + jurordetailList.size() + "SUCCESS_COUNT_EMAIL", String.valueOf(successCountEmail), + "SUCCESS_COUNT_SMS", String.valueOf(successCountSms), + "ERROR_COUNT_EMAIL", String.valueOf(errorCountEmail), + "ERROR_COUNT_SMS", String.valueOf(errorCountSms), + + "COUNT_INVALID_EMAIL", String.valueOf(errorInvalidEmailCount), + "COUNT_INVALID_PHONE", String.valueOf(errorInvalidPhoneCount), + + "SUCCESS_COUNT", String.valueOf(successCount), + "ERROR_COUNT", String.valueOf(errorCount), + "TOTAL_JURORS", String.valueOf(jurordetailList.size()) )); } diff --git a/src/main/java/uk/gov/hmcts/juror/api/bureau/service/JurorCommsWeeklyInfoServiceImpl.java b/src/main/java/uk/gov/hmcts/juror/api/bureau/service/JurorCommsWeeklyInfoServiceImpl.java index 8e3727c1a..b8121d96e 100644 --- a/src/main/java/uk/gov/hmcts/juror/api/bureau/service/JurorCommsWeeklyInfoServiceImpl.java +++ b/src/main/java/uk/gov/hmcts/juror/api/bureau/service/JurorCommsWeeklyInfoServiceImpl.java @@ -15,6 +15,7 @@ import uk.gov.hmcts.juror.api.moj.domain.JurorPool; import uk.gov.hmcts.juror.api.moj.repository.JurorPoolQueries; import uk.gov.hmcts.juror.api.moj.repository.JurorPoolRepository; +import uk.gov.hmcts.juror.api.moj.utils.NotifyUtil; import java.text.SimpleDateFormat; import java.util.Date; @@ -54,6 +55,7 @@ public SchedulerServiceClient.Result process() { int infoCommsSent = 0; int noEmailAddress = 0; + int invalidEmailAddress = 0; int infoCommsfailed = 0; for (JurorPool jurorDetail : jurordetailList) { @@ -75,15 +77,17 @@ public SchedulerServiceClient.Result process() { update(jurorDetail); } catch (JurorCommsNotificationServiceException e) { - log.error( - "Unable to send Informational comms for " - + jurorDetail.getJurorNumber() - + " : " + e.getMessage() - + " " + e.getCause().toString(), e - ); - infoCommsfailed++; + if (NotifyUtil.isInvalidEmailAddressError(e.getCause())) { + invalidEmailAddress++; + } else { + log.error( + "Unable to send Informational comms for {}", + jurorDetail.getJurorNumber(), e + ); + infoCommsfailed++; + } } catch (Exception e) { - log.error("Informational Comms Processing : Juror Comms failed : " + e.getMessage(), e); + log.error("Informational Comms Processing : Juror Comms failed", e); infoCommsfailed++; } } @@ -96,9 +100,10 @@ public SchedulerServiceClient.Result process() { ? SchedulerServiceClient.Result.Status.SUCCESS : SchedulerServiceClient.Result.Status.PARTIAL_SUCCESS, null, Map.of( - "INFO_COMMS_SENT", "" + infoCommsSent, - "INFO_COMMS_FAILED", "" + infoCommsfailed, - "NO_EMAIL_ADDRESS", "" + noEmailAddress + "INFO_COMMS_SENT", String.valueOf(infoCommsSent), + "INFO_COMMS_FAILED", String.valueOf(infoCommsfailed), + "NO_EMAIL_ADDRESS", String.valueOf(noEmailAddress), + "INVALID_EMAIL_ADDRESS", String.valueOf(invalidEmailAddress) )); } diff --git a/src/main/java/uk/gov/hmcts/juror/api/bureau/service/JurorDashboardSmartSurveyImportImpl.java b/src/main/java/uk/gov/hmcts/juror/api/bureau/service/JurorDashboardSmartSurveyImportImpl.java index 66896fcca..760521fc4 100644 --- a/src/main/java/uk/gov/hmcts/juror/api/bureau/service/JurorDashboardSmartSurveyImportImpl.java +++ b/src/main/java/uk/gov/hmcts/juror/api/bureau/service/JurorDashboardSmartSurveyImportImpl.java @@ -79,6 +79,7 @@ public SchedulerServiceClient.Result process() { List surveyResponseList; int dbInsertCount = 0; int dbSkipCount = 0; + int errorCount = 0; SmartSurveyConfigurationProperties.Proxy proxyProperties = smartSurveyConfigurationProperties.getProxy(); @@ -86,8 +87,6 @@ public SchedulerServiceClient.Result process() { // these settings are required for the process to continue log.info("Smart Survey config enabled: {}", smartSurveyEnabled); log.info("Smart Survey config exports url: {}", smartSurveyExportsUrl); - //log.info("Smart Survey config token: {}", smartSurveyToken); - //log.info("Smart Survey config secret: {}", smartSurveyTokenSecret); if (!smartSurveyEnabled) { log.info("Smart Survey data import disabled in application settings"); @@ -115,7 +114,7 @@ public SchedulerServiceClient.Result process() { log.info("Smart Survey proxy port: {}", proxyPort); log.info("Smart Survey proxy type: {}", proxyType); - proxy = new Proxy(proxyType, new InetSocketAddress(proxyHost, Integer.valueOf(proxyPort))); + proxy = new Proxy(proxyType, new InetSocketAddress(proxyHost, Integer.parseInt(proxyPort))); } else { proxy = null; log.info("Smart Survey proxy settings ignored"); @@ -124,6 +123,7 @@ public SchedulerServiceClient.Result process() { } catch (Exception e) { log.error("Smart Survey unable to create proxy using application settings"); proxy = null; + errorCount++; } // Output settings retrieved from APP_SETTINGS table @@ -144,9 +144,7 @@ public SchedulerServiceClient.Result process() { log.error("Unable to obtain export download url from smart survey api"); throw new IllegalStateException("unable to obtain export download url from smart survey api"); } else { - exportUrl = exportUrl + smartSurveyCredentials; - surveyResponseList = getExportData(exportUrl, vars, startDate, surveyId); } @@ -166,8 +164,8 @@ public SchedulerServiceClient.Result process() { this.surveyResponseRepository.save(objSurveyResponse); dbInsertCount++; } catch (Exception e) { - log.error("Error inserting survey record: {}", e.getMessage()); - log.error("Error inserting survey record: {}", objSurveyResponse); + errorCount++; + log.error("Error inserting survey record: {} - {}", e.getMessage(), objSurveyResponse); } } else { // record already exists @@ -178,6 +176,7 @@ public SchedulerServiceClient.Result process() { log.info("Records inserted: {}", dbInsertCount); log.info("Records skipped: {}", dbSkipCount); + log.info("Records with error: {}", errorCount); } @@ -185,11 +184,16 @@ public SchedulerServiceClient.Result process() { log.info("Smart Survey Processing : FINISHED- {}", dateFormatSurvey.format(new Date())); - return new SchedulerServiceClient.Result(SchedulerServiceClient.Result.Status.SUCCESS, - "Successfully loaded survey records", + return new SchedulerServiceClient.Result(errorCount == 0 + ? SchedulerServiceClient.Result.Status.SUCCESS + : SchedulerServiceClient.Result.Status.PARTIAL_SUCCESS, + errorCount == 0 + ? "Successfully loaded survey records" + : "Error loading some survey records", Map.of( "RECORDS_INSERTED", String.valueOf(dbInsertCount), - "RECORDS_SKIPPED", String.valueOf(dbSkipCount) + "RECORDS_SKIPPED", String.valueOf(dbSkipCount), + "ERROR_COUNT", String.valueOf(errorCount) )); } @@ -244,9 +248,7 @@ private String getExportDownloadUrl(String smartSurveyUrl, Map v // Find the latest survey export matching the name set in the config List jsonList = new ArrayList(); - for (int i = 0; - i < jsonArr.length(); - i++) { + for (int i = 0; i < jsonArr.length(); i++) { JSONObject obj = jsonArr.getJSONObject(i); String exportName = obj.getString("name"); diff --git a/src/main/java/uk/gov/hmcts/juror/api/juror/notify/NotifyAdapterImpl.java b/src/main/java/uk/gov/hmcts/juror/api/juror/notify/NotifyAdapterImpl.java index 9bf7f74dd..28a6edd92 100644 --- a/src/main/java/uk/gov/hmcts/juror/api/juror/notify/NotifyAdapterImpl.java +++ b/src/main/java/uk/gov/hmcts/juror/api/juror/notify/NotifyAdapterImpl.java @@ -9,14 +9,12 @@ import uk.gov.service.notify.NotificationClientException; import uk.gov.service.notify.SendEmailResponse; import uk.gov.service.notify.SendSmsResponse; -//import uk.gov.service.notify.NotificationClient; @Component @Slf4j public class NotifyAdapterImpl implements NotifyAdapter { private final NotifyConfigurationProperties notifyProperties; private final NotificationClientApi notifyClient; - // private final NotificationClient notificationClient; private static final String MESSAGE_1 = "Notify send is disabled? {}"; private static final String MESSAGE_2 = "Notify http response code: {}"; @@ -24,15 +22,12 @@ public class NotifyAdapterImpl implements NotifyAdapter { @Autowired public NotifyAdapterImpl(final NotifyConfigurationProperties notifyProperties, - // final NotificationClient notificationClient, final NotificationClientApi notifyClient) { Assert.notNull(notifyProperties, "NotifyConfigurationProperties cannot be null"); Assert.notNull(notifyClient, "NotificationClient cannot be null"); - // Assert.notNull(notificationClient, "NotificationClient cannot be null"); this.notifyProperties = notifyProperties; this.notifyClient = notifyClient; - // this.notificationClient = notificationClient; } @Override @@ -115,7 +110,6 @@ public EmailNotificationReceipt sendCommsEmail(final EmailNotification notificat log.warn("Juror Comms Notify response was null!"); } } catch (NotificationClientException e) { - //log.error("Failed to send via Notify: {}", e); log.trace(MESSAGE_2, e.getHttpResult()); throw new NotifyApiException("Failed to send Juror Comms via Notify: {}", e); } catch (Exception e) { @@ -152,8 +146,6 @@ public SmsNotificationReceipt sendCommsSms(final SmsNotification notification) t notification.getPayload(), notification.getReferenceNumber() ); - //final SendEmailResponse sendEmailResponse = notifyClient.sendEmail(notification.getTemplateId(), - // notification.getRecipientEmail(), notification.getPayload(), notification.getReferenceNumber()); if (log.isTraceEnabled()) { log.trace("Juror Comms SMS Notify responded: {}", sendSmsResponse); } @@ -164,7 +156,6 @@ public SmsNotificationReceipt sendCommsSms(final SmsNotification notification) t log.warn("Juror Comms SMS Notify response was null!"); } } catch (NotificationClientException e) { - //log.error("Failed to send via Notify: {}", e); log.trace(MESSAGE_2, e.getHttpResult()); throw new NotifyApiException("Failed to send Juror Comms SMS via Notify: {}", e); } catch (Exception e) { diff --git a/src/main/java/uk/gov/hmcts/juror/api/juror/service/ExcusedCompletedCourtCommsServiceImpl.java b/src/main/java/uk/gov/hmcts/juror/api/juror/service/ExcusedCompletedCourtCommsServiceImpl.java index b768c70bd..09309fbcf 100644 --- a/src/main/java/uk/gov/hmcts/juror/api/juror/service/ExcusedCompletedCourtCommsServiceImpl.java +++ b/src/main/java/uk/gov/hmcts/juror/api/juror/service/ExcusedCompletedCourtCommsServiceImpl.java @@ -20,6 +20,7 @@ import uk.gov.hmcts.juror.api.moj.repository.JurorPoolRepository; import uk.gov.hmcts.juror.api.moj.repository.RegionNotifyTemplateQueriesMod; import uk.gov.hmcts.juror.api.moj.repository.RegionNotifyTemplateRepositoryMod; +import uk.gov.hmcts.juror.api.moj.utils.NotifyUtil; import uk.gov.service.notify.NotificationClient; import uk.gov.service.notify.NotificationClientException; import uk.gov.service.notify.SendEmailResponse; @@ -99,24 +100,32 @@ public SchedulerServiceClient.Result process() { excusalMetrics.errorCount == 0 && completedMetrics.errorCount == 0 ? SchedulerServiceClient.Result.Status.SUCCESS : SchedulerServiceClient.Result.Status.PARTIAL_SUCCESS, null, - Map.of( - "EXCUSAL_IDENTIFIED", "" + excusalMetrics.jurorPoolsIdentified, - "EXCUSAL_ERROR_COUNT", "" + excusalMetrics.errorCount, - "EXCUSAL_SUCCESS_COUNT", "" + excusalMetrics.successCount, - "EXCUSAL_MISSING_EMAIL_PHONE", "" + excusalMetrics.missingEmailAndPhone, - - "COMPLETED_IDENTIFIED", "" + completedMetrics.jurorPoolsIdentified, - "COMPLETED_ERROR_COUNT", "" + completedMetrics.errorCount, - "COMPLETED_SUCCESS_COUNT", "" + completedMetrics.successCount, - "COMPLETED_MISSING_EMAIL_PHONE", "" + completedMetrics.missingEmailAndPhone - )); + getMetaData(excusalMetrics, completedMetrics)); + } + + private static Map getMetaData(Metrics excusalMetrics, Metrics completedMetrics) { + Map metaData = new HashMap<>(); + metaData.put("EXCUSAL_IDENTIFIED", String.valueOf(excusalMetrics.jurorPoolsIdentified)); + metaData.put("EXCUSAL_ERROR_COUNT", String.valueOf(excusalMetrics.errorCount)); + metaData.put("EXCUSAL_INVALID_PHONE_COUNT", String.valueOf(excusalMetrics.invalidPhoneCount)); + metaData.put("EXCUSAL_INVALID_EMAIL_COUNT", String.valueOf(excusalMetrics.invalidEmailAddressCount)); + metaData.put("EXCUSAL_SUCCESS_COUNT", String.valueOf(excusalMetrics.successCount)); + metaData.put("EXCUSAL_MISSING_EMAIL_PHONE", String.valueOf(excusalMetrics.missingEmailAndPhone)); + + metaData.put("COMPLETED_IDENTIFIED", String.valueOf(completedMetrics.jurorPoolsIdentified)); + metaData.put("COMPLETED_ERROR_COUNT", String.valueOf(completedMetrics.errorCount)); + metaData.put("COMPLETED_INVALID_PHONE_COUNT", String.valueOf(completedMetrics.invalidPhoneCount)); + metaData.put("COMPLETED_INVALID_EMAIL_COUNT", String.valueOf(completedMetrics.invalidEmailAddressCount)); + metaData.put("COMPLETED_SUCCESS_COUNT", String.valueOf(completedMetrics.successCount)); + metaData.put("COMPLETED_MISSING_EMAIL_PHONE", String.valueOf(completedMetrics.missingEmailAndPhone)); + return metaData; } public Proxy setUpConnection() { final NotifyConfigurationProperties.Proxy setUpProxy = notifyConfigurationProperties.getProxy(); if (setUpProxy != null && setUpProxy.isEnabled()) { String setupHost = setUpProxy.getHost(); - Integer setupPort = Integer.valueOf(setUpProxy.getPort()); + int setupPort = Integer.parseInt(setUpProxy.getPort()); Proxy.Type setupType = setUpProxy.getType(); final InetSocketAddress socketAddress = new InetSocketAddress(setupHost, setupPort); proxy = new Proxy(setupType, socketAddress); @@ -134,14 +143,6 @@ private void updateCommsStatusFlagCompleted(JurorPool poolCourtDetailCompletedLi } catch (TransactionSystemException e) { Throwable cause = e.getRootCause(); if (poolCourtDetailCompletedList.getJuror().getServiceCompCommsStatus() == null) { - log.trace( - "ServiceCompCommsStatus is : {} - logging error", - poolCourtDetailCompletedList.getJuror().getServiceCompCommsStatus() - ); - log.info( - "ServiceCompCommsStatus is : {} - logging error", - poolCourtDetailCompletedList.getJuror().getServiceCompCommsStatus() - ); log.error( "Failed to update db to {}. Manual update required. {}", poolCourtDetailCompletedList.getJuror().getServiceCompCommsStatus(), @@ -170,14 +171,6 @@ private void updateCommsStatusFlagExcusal(JurorPool poolCourtDetailExcusalList) } catch (TransactionSystemException e) { Throwable cause = e.getRootCause(); if (poolCourtDetailExcusalList.getJuror().getServiceCompCommsStatus() == null) { - log.trace( - "ServiceCompCommsStatus is : {} - logging error", - poolCourtDetailExcusalList.getJuror().getServiceCompCommsStatus() - ); - log.info( - "ServiceCompCommsStatus is : {} - logging error", - poolCourtDetailExcusalList.getJuror().getServiceCompCommsStatus() - ); log.error( "Failed to update db to {}. Manual update required. {}", poolCourtDetailExcusalList.getJuror().getServiceCompCommsStatus(), @@ -212,7 +205,6 @@ public List setUpRegionIds() { for (CourtRegionMod courtRegion : courtRegions) { String courtregionIds = courtRegion.getRegionId(); regionIds.add(courtregionIds); - log.info("CourtRegions {}", courtRegion.getRegionId()); } @@ -230,6 +222,8 @@ public Metrics processExcusalList(Proxy gotProxy, Map myRegionMa ); int errorCount = 0; + int invalidPhoneCount = 0; + int invalidEmailCount = 0; int successCount = 0; int missingEmailAndPhone = 0; int missingApiKeyCount = 0; @@ -375,20 +369,26 @@ public Metrics processExcusalList(Proxy gotProxy, Map myRegionMa } } } catch (NotificationClientException e) { - log.error("Failed to send via Notify: {}", e.getMessage(), e); - log.trace("Unable to send notify: {}", e.getHttpResult()); - log.info("Unable to send notify: {}", e.getHttpResult()); + log.info("Unable to send notify: {}", e.getMessage()); jurorCourtDetailExcusalList.getJuror().setServiceCompCommsStatus(updateMessageStatusNotSent); updateCommsStatusFlagExcusal(jurorCourtDetailExcusalList); - errorCount++; + if (NotifyUtil.isInvalidPhoneNumberError(e)) { + invalidPhoneCount++; + } else if (NotifyUtil.isInvalidEmailAddressError(e)) { + invalidEmailCount++; + } else { + log.error("Failed to send via Notify: {}", e.getMessage(), e); + errorCount++; + } } catch (Exception e) { - log.info("Unexpected exception: {}", e.getMessage(), e); + log.error("Unexpected exception: {}", e.getMessage(), e); errorCount++; } } return new Metrics(jurorCourtDetailListExcusal.size(), errorCount, successCount, - missingEmailAndPhone, missingApiKeyCount); + missingEmailAndPhone, missingApiKeyCount, + invalidPhoneCount, invalidEmailCount); } private Metrics processCompleted(Proxy gotProxy, Map myRegionMap) { @@ -401,6 +401,8 @@ private Metrics processCompleted(Proxy gotProxy, Map myRegionMap jurorCourtDetailListCompleted.size() ); int errorCount = 0; + int invalidPhoneCount = 0; + int invalidEmailCount = 0; int successCount = 0; int missingEmailAndPhone = 0; int missingApiKeyCount = 0; @@ -537,25 +539,32 @@ private Metrics processCompleted(Proxy gotProxy, Map myRegionMap } } } catch (NotificationClientException e) { - - log.error("Failed to send via Notify: {}", e); - log.trace("Unable to send notify: {}", e.getHttpResult()); - log.info("Unable to send notify: {}", e.getHttpResult()); + log.info("Unable to send notify: {}", e.getMessage()); jurorCourtDetailCompletedList.getJuror().setServiceCompCommsStatus(updateMessageStatusNotSent); - updateCommsStatusFlagCompleted(jurorCourtDetailCompletedList); errorCount++; + if (NotifyUtil.isInvalidPhoneNumberError(e)) { + invalidPhoneCount++; + } else if (NotifyUtil.isInvalidEmailAddressError(e)) { + invalidEmailCount++; + } else { + log.error("Failed to send via Notify: {}", e.getMessage(), e); + errorCount++; + } + } catch (Exception e) { - log.info("Unexpected exception: {}", e); + log.error("Unexpected exception:", e); errorCount++; } } return new Metrics(jurorCourtDetailListCompleted.size(), errorCount, successCount, - missingEmailAndPhone, missingApiKeyCount); + missingEmailAndPhone, missingApiKeyCount, + invalidPhoneCount, invalidEmailCount); } public record Metrics(int jurorPoolsIdentified, int errorCount, int successCount, - int missingEmailAndPhone, int missingApiKeyCount) { + int missingEmailAndPhone, int missingApiKeyCount, + int invalidPhoneCount, int invalidEmailAddressCount) { } } \ No newline at end of file diff --git a/src/main/java/uk/gov/hmcts/juror/api/juror/service/MessagesServiceImpl.java b/src/main/java/uk/gov/hmcts/juror/api/juror/service/MessagesServiceImpl.java index 8774d2ead..378424657 100644 --- a/src/main/java/uk/gov/hmcts/juror/api/juror/service/MessagesServiceImpl.java +++ b/src/main/java/uk/gov/hmcts/juror/api/juror/service/MessagesServiceImpl.java @@ -22,6 +22,7 @@ import uk.gov.hmcts.juror.api.moj.repository.RegionNotifyTemplateQueriesMod; import uk.gov.hmcts.juror.api.moj.repository.RegionNotifyTemplateRepositoryMod; import uk.gov.hmcts.juror.api.moj.service.AppSettingService; +import uk.gov.hmcts.juror.api.moj.utils.NotifyUtil; import uk.gov.service.notify.NotificationClient; import uk.gov.service.notify.NotificationClientException; import uk.gov.service.notify.SendEmailResponse; @@ -99,6 +100,8 @@ public SchedulerServiceClient.Result process() { Map templateDetailsMap = new HashMap<>(); int errorCount = 0; + int invalidPhoneCount = 0; + int invalidEmailCount = 0; int missingApiKeyCount = 0; int missingEmailAndPhone = 0; int emailSuccess = 0; @@ -201,10 +204,17 @@ public SchedulerServiceClient.Result process() { log.trace("Court Comms sms messaging Service : response {}", response); } } catch (NotificationClientException e) { - log.error("Failed to send via Notify", e); + log.info("Failed to send via Notify - {}", e.getMessage()); messagesDetail.setMessageRead(MESSAGE_READ_APP_ERROR); updateMessageFlag(messagesDetail); - errorCount++; + if (NotifyUtil.isInvalidPhoneNumberError(e)) { + invalidPhoneCount++; + } else if (NotifyUtil.isInvalidEmailAddressError(e)) { + invalidEmailCount++; + } else { + log.error("Failed to send via Notify", e); + errorCount++; + } } catch (Exception e) { log.error("Unexpected exception when sending details to notify", e); errorCount++; @@ -217,12 +227,15 @@ public SchedulerServiceClient.Result process() { ? SchedulerServiceClient.Result.Status.SUCCESS : SchedulerServiceClient.Result.Status.PARTIAL_SUCCESS, null, Map.of( - "TOTAL_MESSAGES_TO_SEND","" + messageDetailList.size(), - "ERROR_COUNT", "" + errorCount, - "MISSING_API_KEY_COUNT", "" + missingApiKeyCount, - "MISSING_EMAIL_AND_PHONE", "" + missingEmailAndPhone, - "EMAIL_SUCCESS", "" + emailSuccess, - "SMS_SUCCESS", "" + smsSuccess + "TOTAL_MESSAGES_TO_SEND", String.valueOf(messageDetailList.size()), + "ERROR_COUNT", String.valueOf(errorCount), + "MISSING_API_KEY_COUNT", String.valueOf(missingApiKeyCount), + "MISSING_EMAIL_AND_PHONE", String.valueOf(missingEmailAndPhone), + "EMAIL_SUCCESS", String.valueOf(emailSuccess), + "SMS_SUCCESS", String.valueOf(smsSuccess), + + "INVALID_PHONE_COUNT", String.valueOf(invalidPhoneCount), + "INVALID_EMAIL_COUNT", String.valueOf(invalidEmailCount) )); } diff --git a/src/main/java/uk/gov/hmcts/juror/api/moj/utils/NotifyUtil.java b/src/main/java/uk/gov/hmcts/juror/api/moj/utils/NotifyUtil.java new file mode 100644 index 000000000..bc764fcc4 --- /dev/null +++ b/src/main/java/uk/gov/hmcts/juror/api/moj/utils/NotifyUtil.java @@ -0,0 +1,28 @@ +package uk.gov.hmcts.juror.api.moj.utils; + +import uk.gov.service.notify.NotificationClientException; + +public final class NotifyUtil { + + private NotifyUtil() { + + } + + public static boolean isInvalidPhoneNumberError(Throwable e) { + return doesMessageContain(e, "phone_number is a required property") + || doesMessageContain(e, "InvalidPhoneError"); + } + + public static boolean isInvalidEmailAddressError(Throwable e) { + return doesMessageContain(e, "email_address is a required property") + || doesMessageContain(e, "InvalidEmailAddressError"); + } + + public static boolean doesMessageContain(Throwable e, String message) { + if (e instanceof NotificationClientException notificationClientException) { + return notificationClientException.getMessage() != null + && notificationClientException.getMessage().contains(message); + } + return false; + } +}