55import com .fasterxml .jackson .databind .node .ObjectNode ;
66import io .r2dbc .postgresql .codec .Json ;
77import lombok .AllArgsConstructor ;
8+ import org .mifos .loanrisk .document .storage .ObjectStorageClient ;
89import org .mifos .loanrisk .external .bsa .domain .BankStatementAnalysisResult ;
910import org .mifos .loanrisk .external .bsa .repository .BankStatementAnalysisResultRepository ;
1011import org .mifos .loanrisk .external .cb .domain .CreditBureauResult ;
1112import org .mifos .loanrisk .external .cb .repository .CreditBureauResultRepository ;
13+ import org .springframework .http .HttpHeaders ;
1214import org .springframework .http .MediaType ;
15+ import org .springframework .http .ResponseEntity ;
1316import org .springframework .web .bind .annotation .GetMapping ;
1417import org .springframework .web .bind .annotation .PathVariable ;
1518import org .springframework .web .bind .annotation .RestController ;
@@ -25,6 +28,7 @@ public class AnalysisResultUIController {
2528 private final BankStatementAnalysisResultRepository bsaRepository ;
2629 private final CreditBureauResultRepository cbRepository ;
2730 private final ObjectMapper objectMapper ;
31+ private final ObjectStorageClient storageClient ;
2832
2933 @ GetMapping (value = "/bsa" , produces = MediaType .TEXT_HTML_VALUE )
3034 public Mono <String > getAllBsaResults () {
@@ -42,6 +46,30 @@ public Mono<String> getBsaResultsByLoan(@PathVariable("loanId") Long loanId) {
4246 .map (this ::asHtml );
4347 }
4448
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+
4573 @ GetMapping (value = "/cb" , produces = MediaType .TEXT_HTML_VALUE )
4674 public Mono <String > getAllCbResults () {
4775 return cbRepository .findAll ()
0 commit comments