Skip to content
This repository has been archived by the owner on Sep 30, 2022. It is now read-only.

Commit

Permalink
Merge pull request #452 from dBucik/mails
Browse files Browse the repository at this point in the history
fix: 🐛 Fix mail templates
  • Loading branch information
Dominik František Bučík authored Sep 7, 2022
2 parents ecdb256 + 336ece2 commit 6d8df10
Showing 1 changed file with 58 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,18 @@ public boolean authoritiesApproveProductionTransferNotify(@NonNull String approv
}

MailTemplate template = getTemplate(PRODUCTION_AUTHORITIES_KEY);
if (template == null) {
log.warn("No template for action '{}'", PRODUCTION_AUTHORITIES_KEY);
return false;
}

String message = constructMessage(template);
String subject = constructSubject(template);

if (!StringUtils.hasText(message) || !StringUtils.hasText(subject)) {
log.debug("No subject ({}) or message ({}) configured for notification", subject, message);
return false;
}
message = replaceApprovalLink(message, approvalLink);
message = replacePlaceholders(message, req);
subject = replacePlaceholders(subject, req);
Expand All @@ -190,9 +199,19 @@ public boolean adminAddRemoveNotify(@NonNull String approvalLink, @NonNull Provi
}

MailTemplate template = getTemplate(ADD_ADMIN_KEY);
if (template == null) {
log.warn("No template for action '{}'", ADD_ADMIN_KEY);
return false;
}

String message = constructMessage(template);
String subject = constructSubject(template);

if (!StringUtils.hasText(message) || !StringUtils.hasText(subject)) {
log.debug("No subject ({}) or message ({}) configured for notification", subject, message);
return false;
}

subject = replacePlaceholders(subject, service);

message = replacePlaceholders(message, service);
Expand All @@ -209,10 +228,18 @@ public void notifyUser(@NonNull RequestDTO req, @NonNull String action) {
return;
}
MailTemplate template = getTemplate(action, ROLE_USER);
if (template == null) {
log.warn("No template for action '{}' and role '{}'", action, ROLE_USER);
}

String message = constructMessage(template);
String subject = constructSubject(template);

if (!StringUtils.hasText(message) || !StringUtils.hasText(subject)) {
log.debug("No subject ({}) or message ({}) configured for notification", subject, message);
return;
}

subject = replacePlaceholders(subject, req);
message = replacePlaceholders(message, req);

Expand All @@ -233,9 +260,18 @@ public void notifyAppAdmins(@NonNull RequestDTO req, @NonNull String action) {
return;
}
MailTemplate template = getTemplate(action, ROLE_ADMIN);
if (template == null) {
log.warn("No template for action '{}' and role '{}'", action, ROLE_ADMIN);
return;
}
String subject = constructSubject(template);
String message = constructMessage(template);

if (!StringUtils.hasText(message) || !StringUtils.hasText(subject)) {
log.debug("No subject ({}) or message ({}) configured for notification", subject, message);
return;
}

subject = replacePlaceholders(subject, req);
message = replacePlaceholders(message, req);

Expand All @@ -256,9 +292,18 @@ public void notifyClientSecretChanged(@NonNull Facility facility) {
return;
}
MailTemplate template = getTemplate(CLIENT_SECRET_CHANGED_KEY);
if (template == null) {
log.warn("No template for action '{}'", CLIENT_SECRET_CHANGED_KEY);
return;
}
String subject = constructSubject(template);
String message = constructMessage(template);

if (!StringUtils.hasText(message) || !StringUtils.hasText(subject)) {
log.debug("No subject ({}) or message ({}) configured for notification", subject, message);
return;
}

subject = replacePlaceholders(subject, facility);
message = replacePlaceholders(message, facility);

Expand Down Expand Up @@ -365,8 +410,7 @@ private MailTemplate getTemplate(String action, String role) {
String key = getMailTemplateKey(role, action);
MailTemplate template = templates.getOrDefault(key, null);
if (template == null) {
log.error("Could not fetch mail template for key {} ", key);
throw new IllegalArgumentException("Unrecognized property for mail");
log.warn("Could not fetch mail template for key {} ", key);
}

return template;
Expand All @@ -375,8 +419,7 @@ private MailTemplate getTemplate(String action, String role) {
private MailTemplate getTemplate(String key) {
MailTemplate template = templates.getOrDefault(key, null);
if (template == null) {
log.error("Could not fetch mail template for key {} ", key);
throw new IllegalArgumentException("Unrecognized property for mail");
log.warn("Could not fetch mail template for key {} ", key);
}

return template;
Expand Down Expand Up @@ -442,8 +485,11 @@ private String constructSubject(MailTemplate template) {
joiner.add(subj);
}
}

return mailProperties.getSubjectPrefix() + joiner;
if (joiner.length() > 0) {
return mailProperties.getSubjectPrefix() + joiner;
} else {
return null;
}
}

private String constructMessage(MailTemplate template) {
Expand All @@ -454,8 +500,12 @@ private String constructMessage(MailTemplate template) {
joiner.add(msg);
}
}
joiner.add(mailProperties.getFooter());
return MSG_WRAP_START + joiner + MSG_WRAP_END;
if (joiner.length() > 0) {
joiner.add(mailProperties.getFooter());
return MSG_WRAP_START + joiner + MSG_WRAP_END;
} else {
return null;
}
}

private boolean sendMail(String to, String subject, String msg) {
Expand Down

0 comments on commit 6d8df10

Please sign in to comment.