diff --git a/application/aam-backend-service/src/main/kotlin/com/aamdigital/aambackendservice/reporting/reportcalculation/controller/ReportCalculationController.kt b/application/aam-backend-service/src/main/kotlin/com/aamdigital/aambackendservice/reporting/reportcalculation/controller/ReportCalculationController.kt index 5ff9a32..48aac5f 100644 --- a/application/aam-backend-service/src/main/kotlin/com/aamdigital/aambackendservice/reporting/reportcalculation/controller/ReportCalculationController.kt +++ b/application/aam-backend-service/src/main/kotlin/com/aamdigital/aambackendservice/reporting/reportcalculation/controller/ReportCalculationController.kt @@ -27,6 +27,7 @@ import reactor.kotlin.core.publisher.toFlux import java.time.ZoneOffset import java.time.format.DateTimeFormatter import java.util.* +import kotlin.jvm.optionals.getOrElse @RestController @@ -114,25 +115,41 @@ class ReportCalculationController( val fileContent = reportingStorage .fetchData(DomainReference(id = calculationId)) - val prefix = """ - { - "calculation": "$calculationId", - "data": - """.trimIndent().toByteArray() - val prefixBuffer = DefaultDataBufferFactory().allocateBuffer(prefix.size) - prefixBuffer.write(prefix) - - val suffix = """ - } - """.trimIndent().toByteArray() - val suffixBuffer = DefaultDataBufferFactory().allocateBuffer(suffix.size) - suffixBuffer.write(suffix) - - return@flatMap Flux.concat( - Flux.just(prefixBuffer), - fileContent, - Flux.just(suffixBuffer), - ) + reportingStorage.fetchCalculation(DomainReference(calculationId)) + .toFlux() + .flatMap { + + val calculation = it.getOrElse { + return@flatMap Flux.error { NotFoundException("No data available") } + } + + val prefix = """ + { + "_id": "${calculationId}_data.json", + "report": { + "id": "${calculation.report.id}" + }, + "calculation": { + "id": "$calculationId" + }, + "dataHash": "${calculation.attachments["data.json"]?.digest}", + "data": + """.trimIndent().toByteArray() + val prefixBuffer = DefaultDataBufferFactory().allocateBuffer(prefix.size) + prefixBuffer.write(prefix) + + val suffix = """ + } + """.trimIndent().toByteArray() + val suffixBuffer = DefaultDataBufferFactory().allocateBuffer(suffix.size) + suffixBuffer.write(suffix) + + Flux.concat( + Flux.just(prefixBuffer), + fileContent, + Flux.just(suffixBuffer), + ) + } } } diff --git a/application/aam-backend-service/src/test/resources/cucumber/features/reporting/reportcalculation/fetch-calculation-data.feature b/application/aam-backend-service/src/test/resources/cucumber/features/reporting/reportcalculation/fetch-calculation-data.feature index 570cd74..0a7878d 100644 --- a/application/aam-backend-service/src/test/resources/cucumber/features/reporting/reportcalculation/fetch-calculation-data.feature +++ b/application/aam-backend-service/src/test/resources/cucumber/features/reporting/reportcalculation/fetch-calculation-data.feature @@ -10,7 +10,7 @@ Feature: the report calculation data endpoint persist to database When the client calls GET /v1/reporting/report-calculation/ReportCalculation:1/data Then the client receives an json object Then the client receives status code of 200 - Then the client receives value ReportCalculation:1_data.json for property id + Then the client receives value ReportCalculation:1_data.json for property _id # ReportCalculation not available Scenario: client makes call to GET /reporting/report-calculation/ReportCalculation:42/data and receives NotFound