Skip to content

Commit

Permalink
Added validating in for invalid phone numbers or email addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Edwards-cgi committed Aug 28, 2024
1 parent 686155e commit d49b99f
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 117 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,10 @@ checkstyle {
}

pmdTest {
maxFailures = 294
maxFailures = 292
}
pmdMain {
maxFailures = 776
maxFailures = 752
}
pmd {
maxFailures = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
));

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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)
));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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())
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,6 +55,7 @@ public SchedulerServiceClient.Result process() {

int infoCommsSent = 0;
int noEmailAddress = 0;
int invalidEmailAddress = 0;
int infoCommsfailed = 0;
for (JurorPool jurorDetail : jurordetailList) {

Expand All @@ -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++;
}
}
Expand All @@ -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)
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,14 @@ public SchedulerServiceClient.Result process() {
List<SurveyResponse> surveyResponseList;
int dbInsertCount = 0;
int dbSkipCount = 0;
int errorCount = 0;

SmartSurveyConfigurationProperties.Proxy proxyProperties = smartSurveyConfigurationProperties.getProxy();

// Log the settings retrieved from application.yml
// 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");
Expand Down Expand Up @@ -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");
Expand All @@ -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
Expand All @@ -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);
}

Expand All @@ -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
Expand All @@ -178,18 +176,24 @@ public SchedulerServiceClient.Result process() {

log.info("Records inserted: {}", dbInsertCount);
log.info("Records skipped: {}", dbSkipCount);
log.info("Records with error: {}", errorCount);

}

}

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)
));
}

Expand Down Expand Up @@ -244,9 +248,7 @@ private String getExportDownloadUrl(String smartSurveyUrl, Map<String, String> v

// Find the latest survey export matching the name set in the config
List<JSONObject> jsonList = new ArrayList<JSONObject>();
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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,25 @@
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: {}";
private static final String MESSAGE_3 = "Unexpected exception: {}";

@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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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) {
Expand Down
Loading

0 comments on commit d49b99f

Please sign in to comment.