|
| 1 | +package org.mifos.loanrisk.external.controller; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 6 | +import io.r2dbc.postgresql.codec.Json; |
| 7 | +import lombok.AllArgsConstructor; |
| 8 | +import org.mifos.loanrisk.document.storage.ObjectStorageClient; |
| 9 | +import org.mifos.loanrisk.external.bsa.domain.BankStatementAnalysisResult; |
| 10 | +import org.mifos.loanrisk.external.bsa.repository.BankStatementAnalysisResultRepository; |
| 11 | +import org.mifos.loanrisk.external.cb.domain.CreditBureauResult; |
| 12 | +import org.mifos.loanrisk.external.cb.repository.CreditBureauResultRepository; |
| 13 | +import org.springframework.http.HttpHeaders; |
| 14 | +import org.springframework.http.MediaType; |
| 15 | +import org.springframework.http.ResponseEntity; |
| 16 | +import org.springframework.web.bind.annotation.GetMapping; |
| 17 | +import org.springframework.web.bind.annotation.PathVariable; |
| 18 | +import org.springframework.web.bind.annotation.RestController; |
| 19 | +import reactor.core.publisher.Mono; |
| 20 | + |
| 21 | +import java.util.List; |
| 22 | +import java.util.stream.Collectors; |
| 23 | + |
| 24 | +@RestController |
| 25 | +@AllArgsConstructor |
| 26 | +public class AnalysisResultUIController { |
| 27 | + |
| 28 | + private final BankStatementAnalysisResultRepository bsaRepository; |
| 29 | + private final CreditBureauResultRepository cbRepository; |
| 30 | + private final ObjectMapper objectMapper; |
| 31 | + private final ObjectStorageClient storageClient; |
| 32 | + |
| 33 | + @GetMapping(value = "/bsa", produces = MediaType.TEXT_HTML_VALUE) |
| 34 | + public Mono<String> getAllBsaResults() { |
| 35 | + return bsaRepository.findAll() |
| 36 | + .map(this::toPrettyJson) |
| 37 | + .collectList() |
| 38 | + .map(this::asHtml); |
| 39 | + } |
| 40 | + |
| 41 | + @GetMapping(value = "/bsa/loan/{loanId}", produces = MediaType.TEXT_HTML_VALUE) |
| 42 | + public Mono<String> getBsaResultsByLoan(@PathVariable("loanId") Long loanId) { |
| 43 | + return bsaRepository.findAllByLoanId(loanId) |
| 44 | + .map(this::toPrettyJson) |
| 45 | + .collectList() |
| 46 | + .map(this::asHtml); |
| 47 | + } |
| 48 | + |
| 49 | + @GetMapping(value = "/bsa/report/{id}", produces = MediaType.APPLICATION_PDF_VALUE) |
| 50 | + public Mono<ResponseEntity<byte[]>> getBsaReport(@PathVariable("id") Long id) { |
| 51 | + return bsaRepository.findById(id) |
| 52 | + .flatMap(result -> { |
| 53 | + if (result.getAttributes() == null) { |
| 54 | + return Mono.empty(); |
| 55 | + } |
| 56 | + try { |
| 57 | + String reportKey = objectMapper.readTree(result.getAttributes().asString()) |
| 58 | + .path("reportKey").asText(null); |
| 59 | + if (reportKey == null || reportKey.isBlank()) { |
| 60 | + return Mono.empty(); |
| 61 | + } |
| 62 | + return storageClient.get(reportKey) |
| 63 | + .map(data -> ResponseEntity.ok() |
| 64 | + .header(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"bsa-report-" + id + ".pdf\"") |
| 65 | + .contentType(MediaType.APPLICATION_PDF) |
| 66 | + .body(data)); |
| 67 | + } catch (JsonProcessingException e) { |
| 68 | + return Mono.error(e); |
| 69 | + } |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + @GetMapping(value = "/cb", produces = MediaType.TEXT_HTML_VALUE) |
| 74 | + public Mono<String> getAllCbResults() { |
| 75 | + return cbRepository.findAll() |
| 76 | + .map(this::toPrettyJson) |
| 77 | + .collectList() |
| 78 | + .map(this::asHtml); |
| 79 | + } |
| 80 | + |
| 81 | + @GetMapping(value = "/cb/loan/{loanId}", produces = MediaType.TEXT_HTML_VALUE) |
| 82 | + public Mono<String> getCbResultsByLoan(@PathVariable("loanId") Long loanId) { |
| 83 | + return cbRepository.findAllByLoanId(loanId) |
| 84 | + .map(this::toPrettyJson) |
| 85 | + .collectList() |
| 86 | + .map(this::asHtml); |
| 87 | + } |
| 88 | + |
| 89 | + private String toPrettyJson(BankStatementAnalysisResult result) { |
| 90 | + return formatResult(result.getId(), result.getLoanId(), result.getAttributes()); |
| 91 | + } |
| 92 | + |
| 93 | + private String toPrettyJson(CreditBureauResult result) { |
| 94 | + return formatResult(result.getId(), result.getLoanId(), result.getAttributes()); |
| 95 | + } |
| 96 | + |
| 97 | + private String formatResult(Long id, Long loanId, Json attributes) { |
| 98 | + ObjectNode node = objectMapper.createObjectNode(); |
| 99 | + node.put("id", id); |
| 100 | + node.put("loanId", loanId); |
| 101 | + if (attributes != null) { |
| 102 | + try { |
| 103 | + node.set("attributes", objectMapper.readTree(attributes.asString())); |
| 104 | + } catch (JsonProcessingException e) { |
| 105 | + node.put("attributes", attributes.asString()); |
| 106 | + } |
| 107 | + } |
| 108 | + try { |
| 109 | + return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(node); |
| 110 | + } catch (JsonProcessingException e) { |
| 111 | + return "{}"; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private String asHtml(List<String> jsons) { |
| 116 | + String joined = jsons.stream().collect(Collectors.joining("\n\n")); |
| 117 | + return "<html><body><pre>" + joined + "</pre></body></html>"; |
| 118 | + } |
| 119 | +} |
| 120 | + |
0 commit comments