Skip to content

Commit

Permalink
WIP fixed tests and api starts up. Pending cleanup of code and removi…
Browse files Browse the repository at this point in the history
…ng references to Pen coordinators
  • Loading branch information
soletsdev committed Feb 2, 2024
1 parent 1f6b2d1 commit 312b039
Show file tree
Hide file tree
Showing 22 changed files with 185 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ private void processFileUnProcessableException(@NonNull final String guid, @NonN
val notifySchoolForFileFormatErrorsOptional = this.notifySchoolForFileFormatErrors(guid, penWebBlobEntity, fileUnProcessableException);
final PenRequestBatchEntity entity = mapper.toPenReqBatchEntityForBusinessException(penWebBlobEntity, fileUnProcessableException.getReason(), fileUnProcessableException.getPenRequestBatchStatusCode(), batchFile, persistStudentRecords(fileUnProcessableException.getFileError())); // batch file can be processed further and persisted.
final Optional<School> school = this.restUtils.getSchoolByMincode(penWebBlobEntity.getMincode());
school.ifPresent(value -> entity.setSchoolName(value.getSchoolName()));
school.ifPresent(value -> entity.setSchoolName(value.getDisplayNameNoSpecialChars())); //TODO should this be no special characters?
//wait here if notification was sent, if there was any error this file will be picked up again as it wont be persisted.
if (notifySchoolForFileFormatErrorsOptional.isPresent()) {
final boolean isNotified = this.waitForNotificationToCompleteIfPresent(guid, notifySchoolForFileFormatErrorsOptional.get());
Expand Down Expand Up @@ -271,10 +271,10 @@ private Optional<CompletableFuture<Boolean>> notifySchoolForFileFormatErrors(fin
Optional<CompletableFuture<Boolean>> isSchoolNotifiedFutureOptional = Optional.empty();
if (this.isNotificationToSchoolRequired(fileUnProcessableException)) {
log.info("notification to school is required :: {}", guid);
val coordinatorEmailOptional = this.penCoordinatorService.getPenCoordinatorEmailByMinCode(penWebBlobEntity.getMincode());
if (coordinatorEmailOptional.isPresent()) {
log.info("pen coordinator email found :: {}, for guid :: {}", coordinatorEmailOptional.get(), guid);
isSchoolNotifiedFutureOptional = Optional.ofNullable(this.notificationService.notifySchoolForLoadFailed(guid, penWebBlobEntity.getFileName(), penWebBlobEntity.getSubmissionNumber(), fileUnProcessableException.getReason(), coordinatorEmailOptional.get()));
val coordinatorEmailOptional = this.penCoordinatorService.getStudentRegistrationContactEmailsByMincode(penWebBlobEntity.getMincode());
if (!coordinatorEmailOptional.isEmpty()) {
log.info("pen coordinator email found :: {}, for guid :: {}", coordinatorEmailOptional.stream().toList(), guid);
isSchoolNotifiedFutureOptional = Optional.ofNullable(this.notificationService.notifySchoolForLoadFailed(guid, penWebBlobEntity.getFileName(), penWebBlobEntity.getSubmissionNumber(), fileUnProcessableException.getReason(), coordinatorEmailOptional.stream().toString()));
}
}
return isSchoolNotifiedFutureOptional;
Expand Down Expand Up @@ -310,7 +310,7 @@ private void processLoadedRecordsInBatchFile(@NonNull final String guid, @NonNul
log.info("going to persist data for batch :: {}", guid);
final PenRequestBatchEntity entity = mapper.toPenReqBatchEntityLoaded(penWebBlobEntity, batchFile); // batch file can be processed further and persisted.
final Optional<School> school = this.restUtils.getSchoolByMincode(penWebBlobEntity.getMincode());
school.ifPresent(value -> entity.setSchoolName(value.getSchoolName()));
school.ifPresent(value -> entity.setSchoolName(value.getDisplayNameNoSpecialChars())); //TODO check if we need to make sure to have a safe display name
for (final var student : batchFile.getStudentDetails()) { // set the object so that PK/FK relationship will be auto established by hibernate.
final var penRequestBatchStudentEntity = mapper.toPenRequestBatchStudentEntity(student, entity);
penRequestBatchStudentEntity.setRecordNumber(counter++);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ public void validateMincode(final String guid, final String mincode) throws File
if (school.isEmpty()) {
throw new FileUnProcessableException(INVALID_MINCODE_HEADER, guid, PenRequestBatchStatusCodes.LOAD_FAIL);
}
final String openedDate = school.get().getDateOpened();
final String closedDate = school.get().getDateClosed();
final String openedDate = school.get().getOpenedDate();
final String closedDate = school.get().getClosedDate();
if (openedDate == null || LocalDate.parse(openedDate, DateTimeFormatter.ISO_LOCAL_DATE_TIME).isAfter(LocalDate.now()) || (closedDate != null && LocalDate.parse(closedDate, DateTimeFormatter.ISO_LOCAL_DATE_TIME).isBefore(LocalDate.now()))) {
throw new FileUnProcessableException(INVALID_MINCODE_SCHOOL_CLOSED, guid, PenRequestBatchStatusCodes.LOAD_FAIL);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package ca.bc.gov.educ.penreg.api.mappers.v1;

import ca.bc.gov.educ.penreg.api.constants.PenRequestBatchStudentStatusCodes;
import ca.bc.gov.educ.penreg.api.constants.StudentDemogCode;
import ca.bc.gov.educ.penreg.api.helpers.PenRegBatchHelper;
import ca.bc.gov.educ.penreg.api.struct.Student;
import ca.bc.gov.educ.penreg.api.struct.*;
import ca.bc.gov.educ.penreg.api.struct.v1.BasePenRequestBatchReturnFilesSagaData;
import ca.bc.gov.educ.penreg.api.struct.v1.PenCoordinator;
import ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchStudent;
import ca.bc.gov.educ.penreg.api.struct.v1.reportstructs.PenRequestBatchReportData;
import ca.bc.gov.educ.penreg.api.struct.v1.reportstructs.ReportListItem;
Expand Down Expand Up @@ -83,7 +81,7 @@ public PenRequestBatchReportData toReportData(final BasePenRequestBatchReturnFil
reportData.setProcessDate(processDateTime.format(DateTimeFormatter.ofPattern("yyyy/MM/dd")));
reportData.setProcessTime(processDateTime.format(DateTimeFormatter.ofPattern("HH:mm")));
reportData.setReportDate(processDateTime.format(DateTimeFormatter.ofPattern("yyyy-MMM-dd")).toUpperCase().replace(".", ""));
reportData.setReviewer(this.setReviewer(data.getPenCoordinator()));
reportData.setReviewer(this.setReviewer(data.getStudentRegistrationContacts())); //TODO figure out what to do here if we have more than 1 reviewer
}


Expand Down Expand Up @@ -147,8 +145,8 @@ private void addToSysMatchOrDiffList(final List<ReportListItem> sysMatchedList,
}


private String setReviewer(final PenCoordinator penCoordinator) {
return (penCoordinator != null && StringUtils.isNotBlank(penCoordinator.getPenCoordinatorName())) ? penCoordinator.getPenCoordinatorName() : "School PEN Coordinator";
private String setReviewer(final List<SchoolContact> studentRegistrationContacts) { //TODO check for potential bug last name could be blank?
return (!studentRegistrationContacts.isEmpty() && StringUtils.isNotBlank(studentRegistrationContacts.get(0).getFirstName() + " " + studentRegistrationContacts.get(0).getLastName())) ? studentRegistrationContacts.get(0).getFirstName() + " " + studentRegistrationContacts.get(0).getLastName() : "School PEN Coordinator";
}

private Map<String, Student> setStudents(final List<Student> students) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public interface PenRequestBatchReportDataMapper {
@Mapping(target = "penCordinatorEmail", source = "fromEmail")
@Mapping(target = "mincode", expression = "java(data.getPenRequestBatch() == null || data.getPenRequestBatch().getMincode() == null || data.getPenRequestBatch().getMincode().isEmpty() || data.getPenRequestBatch().getMincode().length()<3 ? \"\" : data.getPenRequestBatch().getMincode().substring(0, 3) + \" \" + data.getPenRequestBatch().getMincode().substring(3))")
@Mapping(target = "submissionNumber", source = "data.penRequestBatch.submissionNumber")
@Mapping(target = "reviewer", source = "data.penCoordinator.penCoordinatorName")
@Mapping(target = "processDate", ignore = true)
@Mapping(target = "processTime", ignore = true)
@Mapping(target = "reportDate", ignore = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,19 @@
import ca.bc.gov.educ.penreg.api.properties.PenCoordinatorProperties;
import ca.bc.gov.educ.penreg.api.rest.RestUtils;
import ca.bc.gov.educ.penreg.api.service.*;
import ca.bc.gov.educ.penreg.api.struct.Event;
import ca.bc.gov.educ.penreg.api.struct.Student;
import ca.bc.gov.educ.penreg.api.struct.*;
import ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchStudentValidationIssueTypeCode;
import ca.bc.gov.educ.penreg.api.struct.v1.*;
import ca.bc.gov.educ.penreg.api.util.JsonUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.*;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import lombok.val;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -110,7 +106,8 @@ protected void gatherReportData(final Event event, final Saga saga, final BasePe
final List<PenRequestBatchStudent> studentRequests = penRequestBatch.get().getPenRequestBatchStudentEntities().stream().map(studentMapper::toStructure).collect(Collectors.toList());
penRequestBatchReturnFilesSagaData.setPenRequestBatchStudents(studentRequests);
penRequestBatchReturnFilesSagaData.setPenRequestBatch(mapper.toStructure(penRequestBatch.get()));
penRequestBatchReturnFilesSagaData.setPenCoordinator(this.getPenCoordinator(penRequestBatch.get()));
// penRequestBatchReturnFilesSagaData.setPenCoordinator(this.getPenCoordinator(penRequestBatch.get()));
penRequestBatchReturnFilesSagaData.setStudentRegistrationContacts(this.getPenCoordinator(penRequestBatch.get()));
penRequestBatchReturnFilesSagaData.setFromEmail(this.penCoordinatorProperties.getFromEmail());
penRequestBatchReturnFilesSagaData.setTelephone(this.penCoordinatorProperties.getTelephone());
penRequestBatchReturnFilesSagaData.setFacsimile(this.penCoordinatorProperties.getFacsimile());
Expand Down Expand Up @@ -232,10 +229,10 @@ protected void sendArchivedEmail(final Event event, final Saga saga, final BaseP
//set toEmail and email type depending on whether a penCoordinator exists for the mincode
if (eventType.equals(NOTIFY_PEN_REQUEST_BATCH_ARCHIVE_HAS_NO_SCHOOL_CONTACT)) {
nextEvent.setEventType(NOTIFY_PEN_REQUEST_BATCH_ARCHIVE_HAS_NO_SCHOOL_CONTACT);
penRequestBatchArchivedEmailEvent.setToEmail(this.getPenCoordinatorProperties().getFromEmail());
penRequestBatchArchivedEmailEvent.setToEmail(new ArrayList<>(List.of(this.getPenCoordinatorProperties().getFromEmail())));
} else {
nextEvent.setEventType(NOTIFY_PEN_REQUEST_BATCH_ARCHIVE_HAS_CONTACT);
penRequestBatchArchivedEmailEvent.setToEmail(penRequestBatchReturnFilesSagaData.getPenCoordinator().getPenCoordinatorEmail());
penRequestBatchArchivedEmailEvent.setToEmail(penRequestBatchReturnFilesSagaData.getStudentRegistrationContacts().stream().map(SchoolContact::getEmail).toList());
}
nextEvent.setEventPayload(JsonUtil.getJsonStringFromObject(penRequestBatchArchivedEmailEvent));

Expand All @@ -261,9 +258,7 @@ protected boolean hasPenCoordinatorEmail(final BasePenRequestBatchReturnFilesSag
}

protected boolean hasNoPenCoordinatorEmail(final BasePenRequestBatchReturnFilesSagaData penRequestBatchReturnFilesSagaData) {
return penRequestBatchReturnFilesSagaData.getPenCoordinator() == null ||
penRequestBatchReturnFilesSagaData.getPenCoordinator().getPenCoordinatorEmail() == null ||
penRequestBatchReturnFilesSagaData.getPenCoordinator().getPenCoordinatorEmail().isEmpty();
return penRequestBatchReturnFilesSagaData.getStudentRegistrationContacts().isEmpty();
}

protected boolean isSupportingPDFGeneration(final BasePenRequestBatchReturnFilesSagaData penRequestBatchReturnFilesSagaData) {
Expand All @@ -288,12 +283,14 @@ protected void sendHasCoordinatorEmail(final Event event, final Saga saga, final
this.sendArchivedEmail(event, saga, penRequestBatchReturnFilesSagaData, NOTIFY_PEN_REQUEST_BATCH_ARCHIVE_HAS_CONTACT);
}

protected PenCoordinator getPenCoordinator(final PenRequestBatchEntity penRequestBatchEntity) {
protected List<SchoolContact> getPenCoordinator(final PenRequestBatchEntity penRequestBatchEntity) {
try {
final var penCoordinatorOptional = this.getPenCoordinatorService().getPenCoordinatorByMinCode(penRequestBatchEntity.getMincode());
return penCoordinatorOptional.orElse(null);
// TODO refactor name to getStudentRegistrationContacts
final var penCoordinatorOptional = this.getPenCoordinatorService().getStudentRegistrationContactsByMincode(penRequestBatchEntity.getMincode());

return penCoordinatorOptional;
} catch (final NullPointerException e) {
log.error("Error while trying to get get pen coordinator. The pen coordinator map is null", e);
log.error("Error while trying to get get pen coordinator. The pen coordinator map is null", e); //TODO check if we still need to check this map
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ public Optional<PenCoordinator> getPenCoordinator(final String mincode) {

public List<SchoolContact> getStudentRegistrationContactList(final String mincode) {
try {
var school = schoolMap.get(mincode);
var school = schoolMap.get(mincode); //TODO investigate if this is correct should we throw an exception?
if(school == null){
return new ArrayList<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import ca.bc.gov.educ.penreg.api.repository.PenRequestBatchStudentRepository;
import ca.bc.gov.educ.penreg.api.repository.PenWebBlobRepository;
import ca.bc.gov.educ.penreg.api.rest.RestUtils;
import ca.bc.gov.educ.penreg.api.struct.Student;
import ca.bc.gov.educ.penreg.api.struct.*;
import ca.bc.gov.educ.penreg.api.struct.v1.PenCoordinator;
import ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchStudent;
import ca.bc.gov.educ.penreg.api.struct.v1.reportstructs.PenRequestBatchReportData;
Expand Down Expand Up @@ -76,7 +76,7 @@ public class ResponseFileGeneratorService {

@Autowired
public ResponseFileGeneratorService(final StudentRegistrationContactService penCoordinatorService, final PenWebBlobRepository penWebBlobRepository, final PenRequestBatchStudentRepository penRequestBatchStudentRepository, final RestUtils restUtils, final SpringTemplateEngine templateEngine) {
this.penCoordinatorService = penCoordinatorService;
this.penCoordinatorService = penCoordinatorService; //TODO change this to StudentRegistrationContactService
this.penWebBlobRepository = penWebBlobRepository;
this.penRequestBatchStudentRepository = penRequestBatchStudentRepository;
this.restUtils = restUtils;
Expand Down Expand Up @@ -140,7 +140,7 @@ public PENWebBlobEntity getIDSBlob(final PenRequestBatchEntity penRequestBatchEn
public PENWebBlobEntity getTxtBlob(final PenRequestBatchEntity penRequestBatchEntity, final List<PenRequestBatchStudent> penRequestBatchStudentEntities) {

final List<PenRequestBatchStudent> filteredStudents = penRequestBatchStudentEntities.stream().filter(x ->
(x.getPenRequestBatchStudentStatusCode().equals(PenRequestBatchStudentStatusCodes.ERROR.getCode()))).collect(Collectors.toList());
(x.getPenRequestBatchStudentStatusCode().equals(PenRequestBatchStudentStatusCodes.ERROR.getCode()))).toList();

byte[] bFile;

Expand Down Expand Up @@ -258,15 +258,17 @@ private void saveReports(final List<PENWebBlobEntity> reports, PenRequestBatchEn
private String createHeader(final PenRequestBatchEntity penRequestBatchEntity, String applicationCode) {
final StringBuilder header = new StringBuilder();
// retrieved from PEN_COORDINATOR table
Optional<PenCoordinator> penCoordinator = this.getPenCoordinatorService().getPenCoordinatorByMinCode(penRequestBatchEntity.getMincode());
List<SchoolContact> penCoordinator = this.getPenCoordinatorService().getStudentRegistrationContactsByMincode(penRequestBatchEntity.getMincode());

//TODO check how this header can accept multiple registration contacts?
header.append("FFI")
.append(String.format("%-8.8s", print(penRequestBatchEntity.getMincode())))
.append(String.format("%-40.40s", print(penRequestBatchEntity.getSchoolName())))
.append(String.format("%-8.8s", penRequestBatchEntity.getProcessDate().format(DateTimeFormatter.ofPattern("yyyyMMdd"))))
.append(String.format("%-100.100s", print(penCoordinator.isPresent()? penCoordinator.get().getPenCoordinatorEmail() : "")))
.append(String.format("%-10.10s", print(penCoordinator.map(coordinator -> coordinator.getPenCoordinatorFax().replaceAll("[^0-9]+", "")).orElse(""))))
.append(String.format("%-40.40s", print(penCoordinator.isPresent()? penCoordinator.get().getPenCoordinatorName() : "")))
.append(String.format("%-100.100s", print(!penCoordinator.isEmpty() ? penCoordinator.get(0).getEmail() : "")))
// .append(String.format("%-10.10s", print(penCoordinator.map(coordinator -> coordinator.getPenCoordinatorFax().replaceAll("[^0-9]+", "")).orElse("")))) //TODO SchoolContact doesn't have a FAX number
.append(String.format("%-10.10s", print(!penCoordinator.isEmpty() ? penCoordinator.get(0).getPhoneNumber() : ""))) //TODO SchoolContact doesn't have a FAX number
.append(String.format("%-40.40s", print(!penCoordinator.isEmpty()? penCoordinator.get(0).getFirstName() : "")))
.append(" ")
.append(String.format("%-4.4s", print(applicationCode)))
.append("\n");
Expand Down Expand Up @@ -310,13 +312,16 @@ private String createBody(final PenRequestBatchStudent penRequestBatchStudentEnt
return body.toString();
}

private String getApplicationCode(String mincode) {
private String getApplicationCode(String mincode) { //TODO How are we going to handle schools that have been moved. We cache the schools.
var applicationCode = "PEN";
var school = this.restUtils.getSchoolByMincode(mincode).
orElseThrow(() -> new PenRegAPIRuntimeException("Cannot find the school data by mincode :: " + mincode));
if(school.getDistNo().equals("104")) {

String districtNumber = school.getMincode().substring(0,3);

if(districtNumber.equals("104")) {
applicationCode = "MISC";
} else if(school.getDistNo().equals("102") && school.getSchlNo().equals("00030")) {
} else if(districtNumber.equals("102") && school.getSchoolNumber().equals("00030")) {
applicationCode = "SFAS";
} else if(school.getFacilityTypeCode().equals("12")) {
applicationCode = "SS";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ public StudentRegistrationContactService(final RestUtils restUtils) {
}

public List<SchoolContact> getStudentRegistrationContactsByMincode(final String mincode) {
if (StringUtils.length(mincode) != 8 || !StringUtils.isNumeric(mincode)) {
if (StringUtils.length(mincode) != 8 || !StringUtils.isNumeric(mincode)) { //TODO why does this need to check? I guess mincode could be wrong?
log.info("getStudentREgistrationContacsByMincode :: mincode is invalid {} returning empty array list", mincode);
return new ArrayList<>();
}

return this.restUtils.getStudentRegistrationContactList(mincode);
}

public List<String> getStudentRegistrationContactEmailsByMincode(final String mincode) {
log.debug("getting pen coordinator email for mincode :: {}", mincode);
log.debug("getting pen coordinator email for mincode :: {}", mincode); //TODO why does this need to check? I guess mincode could be wrong?
if (StringUtils.length(mincode) != 8 || !StringUtils.isNumeric(mincode)) {
log.info("getStudentRegistrationContactEmailsByMincode :: mincode is invalid {} returning empty array list", mincode);
return new ArrayList<>();
}
var contacts = this.restUtils.getStudentRegistrationContactList(mincode);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package ca.bc.gov.educ.penreg.api.struct;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.*;

import javax.validation.constraints.*;
import java.io.Serializable;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class SchoolContact implements Serializable {
/**
Expand Down
Loading

0 comments on commit 312b039

Please sign in to comment.