Skip to content

Commit

Permalink
fix: fixing some pending issues (#718)
Browse files Browse the repository at this point in the history
* 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
paulushcgcj authored Jan 8, 2024
1 parent 0c54610 commit d65d86c
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 116 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/merge-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ jobs:
-p COGNITO_ENVIRONMENT=TEST
-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: Conventional Changelog Update
uses: TriPSs/conventional-changelog-action@v5
Expand Down Expand Up @@ -370,6 +371,7 @@ jobs:
-p COGNITO_ENVIRONMENT=PROD
-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 }}

prod-deploy:
name: PROD Deployment
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/pr-open.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public static class ChesConfiguration {
private String clientId;
private String clientSecret;
private String scope;
private List<String> copyEmail;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public Flux<CodeNameDto> listClientContactTypeCodes(
Integer size
) {
return clientService
.listClientContactTypeCodes(page, size);
.listClientContactTypeCodes(LocalDate.now(),page, size);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package ca.bc.gov.app.repository.client;

import ca.bc.gov.app.entity.client.ContactTypeCodeEntity;
import java.time.LocalDate;
import org.springframework.data.domain.Pageable;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.data.repository.reactive.ReactiveSortingRepository;
import org.springframework.stereotype.Repository;
Expand All @@ -13,7 +15,14 @@ public interface ContactTypeCodeRepository
extends ReactiveCrudRepository<ContactTypeCodeEntity, String>,
ReactiveSortingRepository<ContactTypeCodeEntity, String> {

Flux<ContactTypeCodeEntity> findBy(Pageable pageable);
@Query("""
SELECT * FROM
nrfc.contact_type_code
WHERE
(expiry_date is null or expiry_date > :activeDate)
and effective_date <= :activeDate
ORDER BY description""")
Flux<ContactTypeCodeEntity> findActiveAt(LocalDate activeDate, Pageable pageable);

Mono<ContactTypeCodeEntity> findByOrDescription(String id, String description);
}
196 changes: 106 additions & 90 deletions backend/src/main/java/ca/bc/gov/app/service/ches/ChesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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);
}
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ public Flux<CodeNameDto> listProvinces(String countryCode, int page, int size) {
* @param size The amount of entries per page.
* @return A list of {@link CodeNameDto} entries.
*/
public Flux<CodeNameDto> listClientContactTypeCodes(int page, int size) {
public Flux<CodeNameDto> listClientContactTypeCodes(LocalDate activeDate,int page, int size) {
return contactTypeCodeRepository
.findBy(PageRequest.of(page, size))
.findActiveAt(activeDate, PageRequest.of(page, size))
.map(entity -> new CodeNameDto(
entity.getContactTypeCode(),
entity.getDescription()));
Expand Down
1 change: 1 addition & 0 deletions backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
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);
Original file line number Diff line number Diff line change
Expand Up @@ -560,11 +560,8 @@ private static Stream<Arguments> provinceCode() {
private static Stream<Arguments> contactTypeCodes() {
return
Stream.of(
Arguments.of(null, null, "AP", "Accounts Payable"),
Arguments.of(0, 1, "AP", "Accounts Payable"),
Arguments.of(1, 1, "AR", "Accounts Receivable"),
Arguments.of(11, 1, "GP", "General Partner"),
Arguments.of(22, 1, "TP", "EDI Trading Partner")
Arguments.of(null, null, "TC", "BCTS Contractor"),
Arguments.of(0, 1, "TC", "BCTS Contractor")
);
}

Expand Down
1 change: 1 addition & 0 deletions backend/src/test/resources/application-default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions common/openshift.init.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ parameters:
- name: COGNITO_LOGOUT_URI
description: Cognito redirect url once logged out
required: true
- name: CHES_MAIL_COPY
description: Email address to copy all CHES emails to
required: true
objects:
- apiVersion: v1
kind: Secret
Expand Down
Loading

0 comments on commit d65d86c

Please sign in to comment.