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

DMP-4737: Improve automated task - ApplyRetentionCaseAssociatedObjects #2645

Merged
merged 4 commits into from
Feb 28, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ void findByIsRetentionUpdatedTrueAndRetentionRetriesLessThan_ReturnsResults() {


// when
var result = caseRepository.findByIsRetentionUpdatedTrueAndRetentionRetriesLessThan(3, Limit.of(1000));
var result = caseRepository.findIdsByIsRetentionUpdatedTrueAndRetentionRetriesLessThan(3, Limit.of(1000));

// then
assertThat(result).hasSize(1);
assertThat(result.getFirst().getId()).isEqualTo(matchingCase.getId());
assertThat(result.getFirst()).isEqualTo(matchingCase.getId());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ AND NOT EXISTS (select 1 from CaseRetentionEntity cre
""")
List<Integer> findOpenCasesToClose(OffsetDateTime cutoffDate, Limit limit);

List<CourtCaseEntity> findByIsRetentionUpdatedTrueAndRetentionRetriesLessThan(int maxRetentionRetries, Limit limit);
@Query("""
SELECT cc.id
FROM CourtCaseEntity cc
WHERE cc.isRetentionUpdated = true
AND cc.retentionRetries < :maxRetentionRetries
ORDER BY cc.id ASC
""")
List<Integer> findIdsByIsRetentionUpdatedTrueAndRetentionRetriesLessThan(int maxRetentionRetries, Limit limit);

@Query("""
SELECT courtCase.id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import uk.gov.hmcts.darts.retention.service.ApplyRetentionCaseAssociatedObjectsProcessor;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
Expand All @@ -27,9 +28,15 @@ public class ApplyRetentionCaseAssociatedObjectsProcessorImpl implements ApplyRe
@Override
public void processApplyRetentionToCaseAssociatedObjects(Integer batchSize) {

var cases = findCasesNeedingRetentionAppliedToAssociatedObjects(batchSize);
var casesIds = findCasesNeedingRetentionAppliedToAssociatedObjects(batchSize);

for (var courtCase : cases) {
for (var courtCaseId : casesIds) {
Optional<CourtCaseEntity> courtCaseOpt = caseRepository.findById(courtCaseId);
if (courtCaseOpt.isEmpty()) {
log.error("Case with id '{}' not found", courtCaseId);
continue;
}
CourtCaseEntity courtCase = courtCaseOpt.get();
courtCase.setRetentionUpdated(false);
courtCase.setLastModifiedBy(userIdentity.getUserAccount());
caseRepository.save(courtCase);
Expand All @@ -45,8 +52,8 @@ public void processApplyRetentionToCaseAssociatedObjects(Integer batchSize) {
}
}

private List<CourtCaseEntity> findCasesNeedingRetentionAppliedToAssociatedObjects(Integer batchSize) {
return caseRepository.findByIsRetentionUpdatedTrueAndRetentionRetriesLessThan(maxRetentionRetries, Limit.of(batchSize));
private List<Integer> findCasesNeedingRetentionAppliedToAssociatedObjects(Integer batchSize) {
return caseRepository.findIdsByIsRetentionUpdatedTrueAndRetentionRetriesLessThan(maxRetentionRetries, Limit.of(batchSize));
}

}
Expand Down