Skip to content

Commit

Permalink
Added endpoints to send notification email
Browse files Browse the repository at this point in the history
  • Loading branch information
jfkonecn committed Sep 12, 2024
1 parent 73a81d4 commit 80ba4bb
Show file tree
Hide file tree
Showing 10 changed files with 389 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ public enum MailType {
, LIST_OF_UNAPPROVED_USERS(new MailTypeBuilder()
.templateName("listOfUnapprovedUsers")
.description("List of unapproved users"))
, TERMINATION_NOTIFICATION_EMAIL(new MailTypeBuilder()
.templateName("terminationNotificationEmail")
.description("Notifies admins about companies with licenses about to expire"))
, TEST(new MailTypeBuilder()
.templateName("testEmail")
.description("Test"))
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/mskcc/cbio/oncokb/service/MailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,21 @@ public void sendEmailFromTemplate(UserDTO user, MailType mailType, String subjec
}
}

public void sendInternalEmailFromTemplate(MailType mailType, String subject, String to, Context additionalContext) {
Context context = new Context(Locale.ENGLISH);

if (additionalContext != null)
additionalContext.getVariableNames().forEach(name -> context.setVariable(name, additionalContext.getVariable(name)));

String from = jHipsterProperties.getMail().getFrom();
String content = templateEngine.process("mail/" + mailType.getTemplateName(), context);
try {
sendEmail(to, from, null, subject, content, null, false, true);
} catch (MailException | MessagingException e) {
log.warn("Internal email could not be sent to '{}'", to, e);
}
}

@Async
public void sendActivationEmail(UserDTO user) {
log.debug("Sending activation email to '{}'", user.getEmail());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class CompanyTermination implements Serializable {
private Integer notificationDays;
private Instant date;
private String notes;
private Boolean hasBeenNotified;

public Integer getNotificationDays() {
return notificationDays;
Expand All @@ -31,4 +32,12 @@ public String getNotes() {
public void setNotes(String note) {
this.notes = note;
}

public Boolean getHasBeenNotified() {
return hasBeenNotified;
}

public void setHasBeenNotified(Boolean hasBeenNotified) {
this.hasBeenNotified = hasBeenNotified;
}
}
37 changes: 37 additions & 0 deletions src/main/java/org/mskcc/cbio/oncokb/web/rest/CompanyResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import org.mskcc.cbio.oncokb.web.rest.vm.VerifyCompanyNameVM;
import org.mskcc.cbio.oncokb.service.dto.CompanyDTO;
import org.mskcc.cbio.oncokb.service.dto.UserDTO;
import org.mskcc.cbio.oncokb.service.dto.companyadditionalinfo.CompanyAdditionalInfoDTO;
import org.mskcc.cbio.oncokb.service.dto.companyadditionalinfo.CompanyLicense;
import org.mskcc.cbio.oncokb.service.dto.companyadditionalinfo.CompanyTermination;

import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
Expand All @@ -28,6 +31,9 @@
import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -195,4 +201,35 @@ public ResponseEntity<Void> deleteCompany(@PathVariable Long id) {
companyService.delete(id);
return ResponseEntity.ok().build();
}

@GetMapping("/companies/licenses-about-to-expire-pending-notification")
public List<Long> getLicensesAboutToExpirePendingNotification() {
log.debug("Request to get Company license is about to expire");
Instant now = Instant.now();
ArrayList<Long> companies = new ArrayList<>();
for (CompanyDTO company : companyService.findAll()) {
CompanyAdditionalInfoDTO additionalInfo = company.getAdditionalInfo();
if (additionalInfo != null) {
CompanyLicense license = additionalInfo.getLicense();
if (license != null) {
CompanyTermination termination = license.getTermination();
if (termination != null) {
Boolean hasBeenNotified = termination.getHasBeenNotified();
if (hasBeenNotified == null) {
hasBeenNotified = false;
}
Integer notificationDays = termination.getNotificationDays();
Instant terminationDate = termination.getDate();
if (!hasBeenNotified && notificationDays != null && terminationDate != null) {
Instant start = terminationDate.minus(notificationDays, ChronoUnit.DAYS);
if (now.isAfter(start) && now.isBefore(terminationDate)) {
companies.add(company.getId());
}
}
}
}
}
}
return companies;
}
}
62 changes: 62 additions & 0 deletions src/main/java/org/mskcc/cbio/oncokb/web/rest/MailsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
import org.mskcc.cbio.oncokb.service.dto.CompanyDTO;
import org.mskcc.cbio.oncokb.service.dto.TerminationEmailDTO;
import org.mskcc.cbio.oncokb.service.dto.UserDTO;
import org.mskcc.cbio.oncokb.service.dto.companyadditionalinfo.CompanyAdditionalInfoDTO;
import org.mskcc.cbio.oncokb.service.dto.companyadditionalinfo.CompanyLicense;
import org.mskcc.cbio.oncokb.service.dto.companyadditionalinfo.CompanyTermination;
import org.mskcc.cbio.oncokb.service.mapper.UserMapper;
import org.mskcc.cbio.oncokb.web.rest.errors.BadRequestAlertException;
import org.mskcc.cbio.oncokb.web.rest.vm.CompanyVM;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
Expand All @@ -40,7 +44,9 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.mskcc.cbio.oncokb.config.Constants;
import org.thymeleaf.context.Context;

/**
* REST controller for managing mails.
Expand Down Expand Up @@ -184,4 +190,60 @@ public TerminationEmailDTO getTerminationWarningEmail(@PathVariable Long id) {
public void sendTerminationWarningEmail(@Valid @RequestBody TerminationEmailDTO terminationEmailDTO) throws MessagingException {
this.mailService.sendEmail(terminationEmailDTO);
}

@PostMapping("mails/termination-notification")
public void sendTerminationNotificationEmail(@Valid @RequestBody List<Long> companyIds) throws MessagingException {
List<CompanyDTO> allCompanies = companyService.findAll();
List<CompanyDTO> companiesToNotify = new ArrayList<>();
for (CompanyDTO company : allCompanies) {
if (companyIds.contains(company.getId())) {
companiesToNotify.add(company);
}
}
EmailAddresses addresses = this.applicationProperties.getEmailAddresses();
Context context = new Context();
final String baseUrl = ServletUriComponentsBuilder.fromCurrentContextPath().build().toUriString();
context.setVariable("baseUrl", baseUrl);
context.setVariable("companies", companiesToNotify);
String to = addresses.getLicenseAddress();
mailService.sendInternalEmailFromTemplate(MailType.TERMINATION_NOTIFICATION_EMAIL, "Licenses are about to expire.", to, context);

for (CompanyDTO company : companiesToNotify) {
if (company.getAdditionalInfo() == null) {
company.setAdditionalInfo(new CompanyAdditionalInfoDTO());
}

CompanyAdditionalInfoDTO additionalInfoDTO = company.getAdditionalInfo();

if (additionalInfoDTO.getLicense() == null) {
additionalInfoDTO.setLicense(new CompanyLicense());
}

CompanyLicense license = additionalInfoDTO.getLicense();

if (license.getTermination() == null) {
license.setTermination(new CompanyTermination());
}

CompanyTermination termination = license.getTermination();

termination.setHasBeenNotified(true);

CompanyVM companyVM = new CompanyVM();
companyVM.setId(company.getId());
companyVM.setName(company.getName());
companyVM.setDescription(company.getDescription());
companyVM.setCompanyType(company.getCompanyType());
companyVM.setLicenseType(company.getLicenseType());
companyVM.setLicenseModel(company.getLicenseModel());
companyVM.setLicenseStatus(company.getLicenseStatus());
companyVM.setBusinessContact(company.getBusinessContact());
companyVM.setLegalContact(company.getLegalContact());
companyVM.setCompanyDomains(company.getCompanyDomains());
companyVM.setAdditionalInfo(company.getAdditionalInfo());

companyService.updateCompany(companyVM);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<title>Termination Notification Email</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>The following companies licenses are about to expire.</p>
<table>
<thead>
<tr style="text-align: left">
<th>Name</th>
<th>Link to Company Page</th>
</tr>
</thead>
<tbody>
<tr th:each="company : ${companies}">
<td th:text="${company.name}"/>
<td>
<a th:with="url=(@{|${baseUrl}/companies/${company.id}|})" th:href="${url}">Company Page</a>
</td>
</tr>
</tbody>
</table>
</body>
</html>
12 changes: 0 additions & 12 deletions src/main/webapp/app/components/newCompanyForm/NewCompanyForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,6 @@ import CompanyAdditionalInfo, {
createDefaultAdditionalInfo,
} from 'app/pages/companyPage/CompanyAdditionalInfo';

const defaultInfo: CompanyAdditionalInfoDTO = {
license: {
autoRenewal: false,
activation: '',
termination: {
date: '',
notificationDays: 60,
notes: '',
},
},
};

type INewCompanyFormProps = {
onValidSubmit: (newCompany: Partial<CompanyVM>) => void;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export default function CompanyAdditionalInfo({
undefined
? additionalInfo?.license?.termination?.notificationDays
: 90,
hasBeenNotified: false,
},
},
});
Expand All @@ -122,6 +123,7 @@ export default function CompanyAdditionalInfo({
notificationDays: value
? +value
: ((undefined as unknown) as number),
hasBeenNotified: false,
},
},
});
Expand Down
92 changes: 90 additions & 2 deletions src/main/webapp/app/shared/api/generated/API-docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,40 @@
"deprecated": false
}
},
"/api/companies/licenses-about-to-expire-pending-notification": {
"get": {
"tags": [
"company-resource"
],
"summary": "getLicensesAboutToExpirePendingNotification",
"operationId": "getLicensesAboutToExpirePendingNotificationUsingGET",
"produces": [
"*/*"
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "array",
"items": {
"type": "integer",
"format": "int64"
}
}
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
},
"404": {
"description": "Not Found"
}
},
"deprecated": false
}
},
"/api/companies/lookup": {
"get": {
"tags": [
Expand Down Expand Up @@ -1656,6 +1690,54 @@
"deprecated": false
}
},
"/api/mails/termination-notification": {
"post": {
"tags": [
"mails-controller"
],
"summary": "sendTerminationNotificationEmail",
"operationId": "sendTerminationNotificationEmailUsingPOST",
"consumes": [
"application/json"
],
"produces": [
"*/*"
],
"parameters": [
{
"in": "body",
"name": "companyIds",
"description": "companyIds",
"required": true,
"schema": {
"type": "array",
"items": {
"type": "integer",
"format": "int64"
}
}
}
],
"responses": {
"200": {
"description": "OK"
},
"201": {
"description": "Created"
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
},
"404": {
"description": "Not Found"
}
},
"deprecated": false
}
},
"/api/mails/termination-warning": {
"post": {
"tags": [
Expand Down Expand Up @@ -1842,6 +1924,7 @@
"TOKEN_HAS_BEEN_EXPOSED_USER",
"SEARCHING_RESPONSE_STRUCTURE_HAS_CHANGED",
"LIST_OF_UNAPPROVED_USERS",
"TERMINATION_NOTIFICATION_EMAIL",
"TEST"
]
},
Expand Down Expand Up @@ -3174,7 +3257,7 @@
"properties": {
"activation": {
"type": "string",
"format": "date"
"format": "date-time"
},
"autoRenewal": {
"type": "boolean"
Expand All @@ -3190,7 +3273,10 @@
"properties": {
"date": {
"type": "string",
"format": "date"
"format": "date-time"
},
"hasBeenNotified": {
"type": "boolean"
},
"notes": {
"type": "string"
Expand Down Expand Up @@ -3402,6 +3488,7 @@
"TOKEN_HAS_BEEN_EXPOSED_USER",
"SEARCHING_RESPONSE_STRUCTURE_HAS_CHANGED",
"LIST_OF_UNAPPROVED_USERS",
"TERMINATION_NOTIFICATION_EMAIL",
"TEST"
]
}
Expand Down Expand Up @@ -3923,6 +4010,7 @@
"TOKEN_HAS_BEEN_EXPOSED_USER",
"SEARCHING_RESPONSE_STRUCTURE_HAS_CHANGED",
"LIST_OF_UNAPPROVED_USERS",
"TERMINATION_NOTIFICATION_EMAIL",
"TEST"
]
},
Expand Down
Loading

0 comments on commit 80ba4bb

Please sign in to comment.