-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #344 from sanger/addcostcode
x1112 Addcostcode
- Loading branch information
Showing
46 changed files
with
562 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/uk/ac/sanger/sccp/stan/service/AdminNotifyService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package uk.ac.sanger.sccp.stan.service; | ||
|
||
/** | ||
* Service for sending email notifications to admin users | ||
* @author dr6 | ||
*/ | ||
public interface AdminNotifyService { | ||
/** | ||
* Is the indicated admin notification enabled? | ||
* @param name the name of the notification | ||
* @return true if it is enabled; false if it is disabled | ||
*/ | ||
boolean isNotificationEnabled(String name); | ||
|
||
/** | ||
* Replaces {@code %service} with the name of the service, for clearer email messages. | ||
* @param string the string to modify | ||
* @return the modified string | ||
*/ | ||
String substitute(String string); | ||
|
||
/** | ||
* Issues the specified notification to admin users, if it is enabled. | ||
* {@link #substitute} is used on the {@code heading} and {@code body} arguments. | ||
* @param name the name of the notification | ||
* @param heading the email heading | ||
* @param body the text of the email | ||
* @return true if the email was sent; false otherwise | ||
*/ | ||
boolean issue(String name, String heading, String body); | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/uk/ac/sanger/sccp/stan/service/AdminNotifyServiceImp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package uk.ac.sanger.sccp.stan.service; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import uk.ac.sanger.sccp.stan.config.MailConfig; | ||
import uk.ac.sanger.sccp.stan.model.User; | ||
import uk.ac.sanger.sccp.stan.repo.UserRepo; | ||
|
||
import java.util.List; | ||
|
||
import static uk.ac.sanger.sccp.utils.BasicUtils.nullOrEmpty; | ||
|
||
/** | ||
* @author dr6 | ||
*/ | ||
@Service | ||
public class AdminNotifyServiceImp implements AdminNotifyService { | ||
private final MailConfig mailConfig; | ||
private final UserRepo userRepo; | ||
private final EmailService emailService; | ||
|
||
@Autowired | ||
public AdminNotifyServiceImp(MailConfig mailConfig, UserRepo userRepo, EmailService emailService) { | ||
this.mailConfig = mailConfig; | ||
this.userRepo = userRepo; | ||
this.emailService = emailService; | ||
} | ||
|
||
@Override | ||
public boolean isNotificationEnabled(String name) { | ||
return this.mailConfig.isAdminNotificationEnabled(name); | ||
} | ||
|
||
@Override | ||
public String substitute(String string) { | ||
if (!nullOrEmpty(string)) { | ||
string = string.replace("%service", mailConfig.getServiceDescription()); | ||
} | ||
return string; | ||
} | ||
|
||
@Override | ||
public boolean issue(String name, String heading, String body) { | ||
if (!isNotificationEnabled(name)) { | ||
return false; | ||
} | ||
List<User> admins = userRepo.findAllByRole(User.Role.admin); | ||
if (admins.isEmpty()) { | ||
return false; | ||
} | ||
List<String> recipients = admins.stream().map(User::getUsername).toList(); | ||
return sendNotification(recipients, heading, body); | ||
} | ||
|
||
public boolean sendNotification(List<String> recipients, String heading, String body) { | ||
heading = substitute(heading); | ||
body = substitute(body); | ||
return emailService.tryEmail(recipients, heading, body); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.