Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate manage income evidence stored procedure #130

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crime-evidence/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jacoco {
def versions = [
pitest : "1.16.0",
crimeCommonsClasses : "3.29.3",
commonsModSchemas : "1.8.0",
commonsModSchemas : "1.16.0",
commonsRestClient : "3.4.0",
wmStubRunnerVersion : "4.1.2",
springDocWebMVCVersion: "2.5.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package uk.gov.justice.laa.crime.evidence.builder;

import java.math.BigDecimal;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;
import uk.gov.justice.laa.crime.common.model.evidence.ApiUpdateIncomeEvidenceRequest;
import uk.gov.justice.laa.crime.evidence.dto.UpdateEvidenceDTO;

@Component
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class UpdateEvidenceDTOBuilder {

public static UpdateEvidenceDTO build(final ApiUpdateIncomeEvidenceRequest request) {
return UpdateEvidenceDTO.builder()
.financialAssessmentId(request.getFinancialAssessmentId())
.magCourtOutcome(request.getMagCourtOutcome())
.applicantDetails(request.getApplicantEvidenceItems() != null ? request.getApplicantEvidenceItems().getApplicantDetails() : null)
.applicantPensionAmount(request.getApplicantPensionAmount() != null ? request.getApplicantPensionAmount() : BigDecimal.ZERO)
.applicationReceivedDate(request.getMetadata().getApplicationReceivedDate())
.partnerDetails(request.getPartnerEvidenceItems() != null ? request.getPartnerEvidenceItems().getApplicantDetails() : null)
.partnerPensionAmount(request.getPartnerPensionAmount() != null ? request.getPartnerPensionAmount() : BigDecimal.ZERO)
.applicantIncomeEvidenceItems(request.getApplicantEvidenceItems() != null ? request.getApplicantEvidenceItems().getIncomeEvidenceItems() : null)
.partnerIncomeEvidenceItems(request.getPartnerEvidenceItems() != null ? request.getPartnerEvidenceItems().getIncomeEvidenceItems() : null)
.evidencePending(request.getMetadata().getEvidencePending())
.evidenceDueDate(request.getEvidenceDueDate())
.evidenceReceivedDate(request.getEvidenceReceivedDate())
.previousEvidenceDueDate(request.getPreviousEvidenceDueDate())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import uk.gov.justice.laa.crime.common.model.evidence.ApiCreateIncomeEvidenceResponse;
import uk.gov.justice.laa.crime.common.model.evidence.ApiUpdateIncomeEvidenceRequest;
import uk.gov.justice.laa.crime.common.model.evidence.ApiUpdateIncomeEvidenceResponse;
import uk.gov.justice.laa.crime.evidence.builder.UpdateEvidenceDTOBuilder;
import uk.gov.justice.laa.crime.evidence.dto.UpdateEvidenceDTO;
import uk.gov.justice.laa.crime.evidence.service.EvidenceService;

@Slf4j
@RestController
Expand All @@ -21,13 +24,17 @@
@Tag(name = "Income Evidence", description = "Rest API for Income Evidence")
public class IncomeEvidenceController implements IncomeEvidenceApi {

private final EvidenceService evidenceService;

@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ApiCreateIncomeEvidenceResponse> createEvidence(ApiCreateIncomeEvidenceRequest request) {
return ResponseEntity.ok().build();
}

@PutMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ApiUpdateIncomeEvidenceResponse> updateEvidence(ApiUpdateIncomeEvidenceRequest request) {
return ResponseEntity.ok().build();
UpdateEvidenceDTO updateEvidenceDTO = UpdateEvidenceDTOBuilder.build(request);

return ResponseEntity.ok(evidenceService.updateEvidence(updateEvidenceDTO));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package uk.gov.justice.laa.crime.evidence.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

@AllArgsConstructor
@Data
@Builder
public class EvidenceReceivedResultDTO {
private boolean evidenceReceived;
private int incomeEvidenceRequiredId;
private int minimumEvidenceItemsRequired;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package uk.gov.justice.laa.crime.evidence.dto;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import lombok.Builder;
import lombok.Data;
import uk.gov.justice.laa.crime.common.model.evidence.ApiApplicantDetails;
import uk.gov.justice.laa.crime.common.model.evidence.ApiIncomeEvidence;
import uk.gov.justice.laa.crime.enums.MagCourtOutcome;

@Data
@Builder
public class UpdateEvidenceDTO {
private int financialAssessmentId;
private List<ApiIncomeEvidence> applicantIncomeEvidenceItems;
private List<ApiIncomeEvidence> partnerIncomeEvidenceItems;
private MagCourtOutcome magCourtOutcome;
private ApiApplicantDetails applicantDetails;
private ApiApplicantDetails partnerDetails;
private BigDecimal applicantPensionAmount;
private BigDecimal partnerPensionAmount;
private LocalDate applicationReceivedDate;
private boolean evidencePending;
private LocalDateTime evidenceDueDate;
private LocalDateTime evidenceReceivedDate;
private LocalDateTime previousEvidenceDueDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package uk.gov.justice.laa.crime.evidence.repository;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import uk.gov.justice.laa.crime.evidence.staticdata.entity.IncomeEvidenceRequiredItemEntity;
import uk.gov.justice.laa.crime.evidence.staticdata.projection.IncomeEvidenceRequiredItemProjection;

@Repository
public interface IncomeEvidenceRequiredItemRepository extends JpaRepository<IncomeEvidenceRequiredItemEntity, Integer> {
List<IncomeEvidenceRequiredItemProjection> findByIncomeEvidenceRequiredId(Integer id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package uk.gov.justice.laa.crime.evidence.repository;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import uk.gov.justice.laa.crime.evidence.staticdata.entity.IncomeEvidenceRequiredEntity;

@Repository
public interface IncomeEvidenceRequiredRepository extends CrudRepository<IncomeEvidenceRequiredEntity, String> {
@Query(
value =
"""
select ID,
EVIDENCE_ITEMS_REQUIRED
from income_evidence_required i
where mcoo_outcome = ?1
and applicant_EMST_CODE = ?2
and ((partner_emst_code is null and ?3 is null) or (partner_emst_code = ?3))
and I.APPLICANT_PARTNER = ?4
and I.ANNUAL_PENSION_AMOUNT <= nvl(?5,0)
and not exists (select 1
from income_evidence_required i2
where i2.mcoo_outcome = ?1
and i2.applicant_EMST_CODE = ?2
and ((i2.partner_emst_code is null and ?3 is null) or (i2.partner_emst_code = ?3))
and I2.APPLICANT_PARTNER = ?4
and i2.ANNUAL_PENSION_AMOUNT <= nvl(?5,0)
and i2.ANNUAL_PENSION_AMOUNT > I.ANNUAL_PENSION_AMOUNT)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a good candidate for adding some indexes to the DB for - imagine it's slow to run!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be, I didn't check if it has any indexes already but this is the same query that's currently in one of the nested stored procedures, so should at least be no worse than it currently is.

""",
nativeQuery = true)
IncomeEvidenceRequiredEntity getNumberOfEvidenceItemsRequired(String mcooOutcome, String applicantEmstCode, String partnerEmstCode, String applicantPartner, Double annualPensionAmount);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,30 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import uk.gov.justice.laa.crime.common.model.evidence.ApiCalculateEvidenceFeeResponse;
import uk.gov.justice.laa.crime.common.model.evidence.ApiEvidenceFee;
import uk.gov.justice.laa.crime.common.model.evidence.ApiIncomeEvidence;
import uk.gov.justice.laa.crime.common.model.evidence.ApiIncomeEvidenceItems;
import uk.gov.justice.laa.crime.common.model.evidence.ApiUpdateIncomeEvidenceResponse;
import uk.gov.justice.laa.crime.enums.EvidenceFeeLevel;
import uk.gov.justice.laa.crime.evidence.builder.EvidenceFeeRulesDTOBuilder;
import uk.gov.justice.laa.crime.evidence.common.Constants;
import uk.gov.justice.laa.crime.evidence.dto.CrimeEvidenceDTO;
import uk.gov.justice.laa.crime.evidence.dto.EvidenceFeeRulesDTO;
import uk.gov.justice.laa.crime.common.model.evidence.ApiCalculateEvidenceFeeResponse;
import uk.gov.justice.laa.crime.common.model.evidence.ApiEvidenceFee;
import uk.gov.justice.laa.crime.enums.EvidenceFeeLevel;
import uk.gov.justice.laa.crime.evidence.dto.UpdateEvidenceDTO;
import uk.gov.justice.laa.crime.evidence.staticdata.enums.ApplicantType;
import uk.gov.justice.laa.crime.evidence.staticdata.enums.EvidenceFeeRules;
import uk.gov.justice.laa.crime.util.DateUtil;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class EvidenceService {
private final IncomeEvidenceService incomeEvidenceService;
private final IncomeEvidenceValidationService incomeEvidenceValidationService;
private final MaatCourtDataService maatCourtDataService;

public ApiCalculateEvidenceFeeResponse calculateEvidenceFee(CrimeEvidenceDTO crimeEvidenceDTO) {
Expand Down Expand Up @@ -64,9 +75,86 @@ public ApiCalculateEvidenceFeeResponse calculateEvidenceFee(CrimeEvidenceDTO cri
return apiProcessRepOrderResponse;
}

public ApiUpdateIncomeEvidenceResponse updateEvidence(UpdateEvidenceDTO updateEvidenceDTO) {
List<ApiIncomeEvidence> applicantEvidenceItems = updateEvidenceDTO.getApplicantIncomeEvidenceItems();
List<ApiIncomeEvidence> partnerEvidenceItems = updateEvidenceDTO.getPartnerIncomeEvidenceItems();

if (applicantEvidenceItems.isEmpty() && partnerEvidenceItems.isEmpty()) {
throw new IllegalArgumentException("No income evidence items provided");
}

incomeEvidenceValidationService.checkEvidenceReceivedDate(
DateUtil.parseLocalDate(updateEvidenceDTO.getEvidenceReceivedDate()),
updateEvidenceDTO.getApplicationReceivedDate());

incomeEvidenceValidationService.checkExtraEvidenceDescriptions(applicantEvidenceItems);
incomeEvidenceValidationService.checkExtraEvidenceDescriptions(partnerEvidenceItems);

incomeEvidenceValidationService.checkEvidenceDueDates(
DateUtil.parseLocalDate(updateEvidenceDTO.getEvidenceDueDate()),
DateUtil.parseLocalDate(updateEvidenceDTO.getPreviousEvidenceDueDate()),
updateEvidenceDTO.isEvidencePending());

boolean applicantEvidenceItemsReceived = true;
boolean partnerEvidenceItemsReceived = true;

if (!applicantEvidenceItems.isEmpty()) {
applicantEvidenceItemsReceived = incomeEvidenceService.checkEvidenceReceived(
applicantEvidenceItems,
updateEvidenceDTO.getMagCourtOutcome(),
updateEvidenceDTO.getApplicantDetails().getEmploymentStatus(),
updateEvidenceDTO.getPartnerDetails() != null
? updateEvidenceDTO.getPartnerDetails().getEmploymentStatus() : null,
updateEvidenceDTO.getApplicantPensionAmount(),
ApplicantType.APPLICANT);
}

if (!partnerEvidenceItems.isEmpty()) {
partnerEvidenceItemsReceived = incomeEvidenceService.checkEvidenceReceived(
partnerEvidenceItems,
updateEvidenceDTO.getMagCourtOutcome(),
updateEvidenceDTO.getApplicantDetails().getEmploymentStatus(),
updateEvidenceDTO.getPartnerDetails().getEmploymentStatus(),
updateEvidenceDTO.getPartnerPensionAmount(),
ApplicantType.PARTNER);
}

boolean allEvidenceItemsReceived = applicantEvidenceItemsReceived && partnerEvidenceItemsReceived;

updateEvidenceReceivedDate(updateEvidenceDTO, allEvidenceItemsReceived);
updateEvidenceDueDate(updateEvidenceDTO);

ApiUpdateIncomeEvidenceResponse response = new ApiUpdateIncomeEvidenceResponse()
.withApplicantEvidenceItems(new ApiIncomeEvidenceItems(updateEvidenceDTO.getApplicantDetails(), applicantEvidenceItems))
.withDueDate(DateUtil.parseLocalDate(updateEvidenceDTO.getEvidenceDueDate()))
.withAllEvidenceReceivedDate(DateUtil.parseLocalDate(updateEvidenceDTO.getEvidenceReceivedDate()));

if (!partnerEvidenceItems.isEmpty()) {
response.setPartnerEvidenceItems(new ApiIncomeEvidenceItems(updateEvidenceDTO.getPartnerDetails(), partnerEvidenceItems));
}

return response;
}

protected boolean isCalcRequired(CrimeEvidenceDTO crimeEvidenceDTO) {
return (crimeEvidenceDTO.getMagCourtOutcome().equalsIgnoreCase(Constants.SENT_FOR_TRIAL)
|| crimeEvidenceDTO.getMagCourtOutcome().equalsIgnoreCase(Constants.COMMITTED_FOR_TRIAL)) &&
(crimeEvidenceDTO.getEvidenceFee() == null || crimeEvidenceDTO.getEvidenceFee().getFeeLevel() == null);
}

private void updateEvidenceDueDate(UpdateEvidenceDTO updateEvidenceDTO) {
LocalDateTime previousEvidenceDueDate = updateEvidenceDTO.getPreviousEvidenceDueDate();

if (updateEvidenceDTO.getEvidenceDueDate() == null && previousEvidenceDueDate != null) {
updateEvidenceDTO.setEvidenceDueDate(previousEvidenceDueDate);
}
}

private void updateEvidenceReceivedDate(UpdateEvidenceDTO updateEvidenceDTO, boolean evidenceReceived) {
if (evidenceReceived && updateEvidenceDTO.getEvidenceReceivedDate() == null) {
updateEvidenceDTO.setEvidenceReceivedDate(LocalDateTime.now());
} else if (!evidenceReceived && updateEvidenceDTO.getEvidenceReceivedDate() != null) {
updateEvidenceDTO.setEvidenceReceivedDate(null);
}
}
}
Loading
Loading