-
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.
fix: fixing some pending issues (#718)
* fix(FSADT1-1052): fixing list of client types * fix(FSADT1-1101|FSADT1-1103|FSADT1-1106): fixing review email * fix(FSADT1-1106): adding environment and mail list - added a mail list as a parameter instead of hardcoded - setting environment name on email subject when not in prod * fix(FSADT1-1104): removing limit to active clients only * chore: updating deployment file
- Loading branch information
1 parent
0c54610
commit d65d86c
Showing
15 changed files
with
133 additions
and
116 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -182,6 +182,7 @@ jobs: | |
-p COGNITO_ENVIRONMENT=DEV | ||
-p COGNITO_REDIRECT_URI=https://${{ env.PREFIX }}-frontend.${{ env.DOMAIN }}/dashboard | ||
-p COGNITO_LOGOUT_URI=https://${{ env.PREFIX }}-frontend.${{ env.DOMAIN }} | ||
-p CHES_MAIL_COPY=${{ secrets.CHES_MAIL_COPY }} | ||
|
||
- name: Deploy Database | ||
uses: bcgov-nr/[email protected] | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,13 +11,27 @@ | |
import ca.bc.gov.app.dto.ches.CommonExposureJwtDto; | ||
import ca.bc.gov.app.dto.client.EmailLogDto; | ||
import ca.bc.gov.app.entity.client.EmailLogEntity; | ||
import ca.bc.gov.app.exception.*; | ||
import ca.bc.gov.app.exception.BadRequestException; | ||
import ca.bc.gov.app.exception.InvalidAccessTokenException; | ||
import ca.bc.gov.app.exception.InvalidRequestObjectException; | ||
import ca.bc.gov.app.exception.InvalidRoleException; | ||
import ca.bc.gov.app.exception.UnableToProcessRequestException; | ||
import ca.bc.gov.app.exception.UnexpectedErrorException; | ||
import ca.bc.gov.app.repository.client.EmailLogRepository; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import io.r2dbc.postgresql.codec.Json; | ||
import freemarker.template.Configuration; | ||
import freemarker.template.Template; | ||
import freemarker.template.TemplateException; | ||
import io.r2dbc.postgresql.codec.Json; | ||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.http.HttpHeaders; | ||
|
@@ -29,14 +43,6 @@ | |
import org.springframework.web.reactive.function.client.ClientResponse; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
import reactor.core.publisher.Mono; | ||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@Slf4j | ||
|
@@ -48,9 +54,9 @@ public class ChesService { | |
|
||
private final WebClient authApi; | ||
private final Configuration freeMarkerConfiguration; | ||
|
||
private final EmailLogRepository emailLogRepository; | ||
|
||
private final Jackson2ObjectMapperBuilder builder; | ||
|
||
public ChesService( | ||
|
@@ -71,102 +77,112 @@ public ChesService( | |
} | ||
|
||
public Mono<String> sendEmail(String templateName, | ||
String emailAddress, | ||
String subject, | ||
Map<String, Object> variables, | ||
Integer emailLogId) { | ||
String emailAddress, | ||
String subject, | ||
Map<String, Object> variables, | ||
Integer emailLogId) { | ||
|
||
List<String> emails = new ArrayList<>(); | ||
emails.add(emailAddress); | ||
emails.addAll(configuration.getChes().getCopyEmail()); | ||
|
||
String processedSubject = | ||
configuration.getCognito().getEnvironment().equalsIgnoreCase("prod") | ||
? subject | ||
: String.format("[%s] %s", configuration.getCognito().getEnvironment(), subject); | ||
|
||
return this | ||
.buildTemplate(templateName, variables) | ||
.flatMap(body -> { | ||
ChesRequestDto chesRequestDto = new ChesRequestDto(List.of(emailAddress, | ||
"[email protected]", | ||
"[email protected]", | ||
"[email protected]"), | ||
body); | ||
|
||
return this.sendEmail(chesRequestDto, subject).flatMap(emailId -> { | ||
log.info("Mail sent, transaction ID is {}", emailId); | ||
|
||
EmailLogDto emailLogDto = new EmailLogDto( | ||
emailLogId, | ||
templateName, | ||
emailAddress, | ||
subject, | ||
"Y", | ||
emailId, | ||
"", | ||
variables); | ||
return saveEmailLog(emailLogDto, "Email sent successfully. Transaction ID: " + emailId); | ||
}) | ||
.onErrorResume(throwable -> { | ||
log.error("Error occurred while building/sending the email: {}", throwable.getMessage()); | ||
|
||
EmailLogDto emailLogDto = new EmailLogDto( | ||
emailLogId, | ||
templateName, | ||
emailAddress, | ||
subject, | ||
"N", | ||
"", | ||
throwable.getMessage(), | ||
variables); | ||
return saveEmailLog(emailLogDto, "Error sending email"); | ||
}); | ||
}); | ||
.map(body -> new ChesRequestDto(emails, body)) | ||
.flatMap(chesRequestDto -> | ||
this | ||
.sendEmail(chesRequestDto, processedSubject) | ||
.doOnNext(emailId -> log.info("Mail sent, transaction ID is {}", emailId)) | ||
.flatMap(emailId -> | ||
saveEmailLog( | ||
new EmailLogDto( | ||
emailLogId, | ||
templateName, | ||
emailAddress, | ||
processedSubject, | ||
"Y", | ||
emailId, | ||
"", | ||
variables), | ||
"Email sent successfully. Transaction ID: " + emailId | ||
) | ||
) | ||
) | ||
.doOnError(throwable -> log.error("Error occurred while building/sending the email: {}", | ||
throwable.getMessage())) | ||
.onErrorResume(throwable -> | ||
saveEmailLog( | ||
new EmailLogDto( | ||
emailLogId, | ||
templateName, | ||
emailAddress, | ||
processedSubject, | ||
"N", | ||
"", | ||
throwable.getMessage(), | ||
variables), | ||
"Error sending email" | ||
) | ||
); | ||
} | ||
|
||
private Mono<String> saveEmailLog(EmailLogDto emailLogDto, String transactionMsg) { | ||
if (emailLogDto.emailLogId() != null) { | ||
return emailLogRepository.findById(emailLogDto.emailLogId()) | ||
.flatMap(existingLogEntity -> updateExistingLogEntity( | ||
existingLogEntity, | ||
emailLogDto, | ||
transactionMsg)); | ||
return emailLogRepository.findById(emailLogDto.emailLogId()) | ||
.flatMap(existingLogEntity -> updateExistingLogEntity( | ||
existingLogEntity, | ||
emailLogDto, | ||
transactionMsg)); | ||
} else { | ||
EmailLogEntity logEntity = createNewLogEntity(emailLogDto); | ||
return emailLogRepository.save(logEntity) | ||
.thenReturn(transactionMsg); | ||
EmailLogEntity logEntity = createNewLogEntity(emailLogDto); | ||
return emailLogRepository.save(logEntity) | ||
.thenReturn(transactionMsg); | ||
} | ||
} | ||
|
||
private Mono<String> updateExistingLogEntity( | ||
EmailLogEntity existingLogEntity, | ||
EmailLogDto emailLogDto, | ||
String transactionMsg) { | ||
String exceptionMessage = "Y".equals(existingLogEntity.getEmailSentInd()) | ||
? "" | ||
: emailLogDto.exceptionMessage(); | ||
existingLogEntity.setEmailSentInd(emailLogDto.emailSentInd()); | ||
existingLogEntity.setExceptionMessage(exceptionMessage); | ||
existingLogEntity.setUpdateDate(LocalDateTime.now()); | ||
return emailLogRepository | ||
.save(existingLogEntity) | ||
.thenReturn(transactionMsg); | ||
EmailLogEntity existingLogEntity, | ||
EmailLogDto emailLogDto, | ||
String transactionMsg) { | ||
|
||
String exceptionMessage = "Y".equals(existingLogEntity.getEmailSentInd()) | ||
? "" | ||
: emailLogDto.exceptionMessage(); | ||
existingLogEntity.setEmailSentInd(emailLogDto.emailSentInd()); | ||
existingLogEntity.setExceptionMessage(exceptionMessage); | ||
existingLogEntity.setUpdateDate(LocalDateTime.now()); | ||
|
||
return emailLogRepository | ||
.save(existingLogEntity) | ||
.thenReturn(transactionMsg); | ||
} | ||
|
||
private EmailLogEntity createNewLogEntity(EmailLogDto emailLogDto) { | ||
EmailLogEntity logEntity = new EmailLogEntity(); | ||
logEntity.setCreateDate(LocalDateTime.now()); | ||
logEntity.setTemplateName(emailLogDto.templateName()); | ||
logEntity.setEmailAddress(emailLogDto.emailAddress()); | ||
logEntity.setEmailSubject(emailLogDto.subject()); | ||
logEntity.setEmailSentInd(emailLogDto.emailSentInd()); | ||
logEntity.setEmailId(emailLogDto.emailId()); | ||
logEntity.setExceptionMessage(emailLogDto.exceptionMessage()); | ||
logEntity.setEmailVariables(convertTo(emailLogDto.variables())); | ||
return logEntity; | ||
EmailLogEntity logEntity = new EmailLogEntity(); | ||
logEntity.setCreateDate(LocalDateTime.now()); | ||
logEntity.setTemplateName(emailLogDto.templateName()); | ||
logEntity.setEmailAddress(emailLogDto.emailAddress()); | ||
logEntity.setEmailSubject(emailLogDto.subject()); | ||
logEntity.setEmailSentInd(emailLogDto.emailSentInd()); | ||
logEntity.setEmailId(emailLogDto.emailId()); | ||
logEntity.setExceptionMessage(emailLogDto.exceptionMessage()); | ||
logEntity.setEmailVariables(convertTo(emailLogDto.variables())); | ||
|
||
return logEntity; | ||
} | ||
|
||
private Json convertTo(Map<String, Object> variables) { | ||
String json = "{}"; | ||
|
||
try { | ||
json = builder | ||
.build() | ||
.writeValueAsString(variables); | ||
.build() | ||
.writeValueAsString(variables); | ||
} catch (JsonProcessingException e) { | ||
log.error("Error while converting matchers to json", e); | ||
} | ||
|
@@ -247,7 +263,7 @@ public Mono<String> sendEmail(ChesRequestDto requestContent, String subject) { | |
* @param variables a map of variable names and their corresponding values to be used when | ||
* processing the template | ||
* @return a Mono that emits the String representation of the processed template, or an error if | ||
* an exception occurs during template processing | ||
* an exception occurs during template processing | ||
*/ | ||
public Mono<String> buildTemplate(String templateName, Map<String, Object> variables) { | ||
StringWriter writer = new StringWriter(); | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,6 +63,7 @@ ca: | |
clientId: ${CHES_CLIENT_ID:clientId} | ||
clientSecret: ${CHES_CLIENT_SECRET:secret} | ||
scope: scope | ||
copyEmail: ${CHES_COPY_EMAIL:[email protected]} | ||
bcregistry: | ||
uri: ${BCREGISTRY_URI:https://bcregistry-sandbox.apigee.net} | ||
apiKey: ${BCREGISTRY_KEY:123456} | ||
|
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 @@ | ||
ALTER TABLE nrfc.email_log ALTER COLUMN email_subject TYPE varchar(50) USING email_subject::varchar(50); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ ca: | |
clientId: clientId | ||
clientSecret: secret | ||
scope: scope | ||
copyEmail: [email protected],[email protected],[email protected] | ||
bcregistry: | ||
uri: 'http://127.0.0.1:10040' | ||
apiKey: abc1234 | ||
|
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.