Skip to content

Commit

Permalink
Engagement/jessica/15406 download report cleanup (#15847)
Browse files Browse the repository at this point in the history
* Capturing the report not existing error so that we can return a pretty one. Fixing a condition in the pii removal yml. Returning a prettier body for a report
  • Loading branch information
JessicaWNava authored Sep 11, 2024
1 parent 84c0de4 commit 86c4b77
Show file tree
Hide file tree
Showing 6 changed files with 3,884 additions and 22 deletions.
17 changes: 11 additions & 6 deletions prime-router/src/main/kotlin/azure/ReportFunction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import gov.cdc.prime.router.UniversalPipelineReceiver
import gov.cdc.prime.router.azure.BlobAccess.Companion.defaultBlobMetadata
import gov.cdc.prime.router.azure.BlobAccess.Companion.getBlobContainer
import gov.cdc.prime.router.azure.db.enums.TaskAction
import gov.cdc.prime.router.azure.db.tables.pojos.ReportFile
import gov.cdc.prime.router.azure.observability.event.IReportStreamEventService
import gov.cdc.prime.router.azure.observability.event.ReportStreamEventName
import gov.cdc.prime.router.azure.observability.event.ReportStreamEventProperties
Expand All @@ -40,7 +41,9 @@ import gov.cdc.prime.router.tokens.AuthenticatedClaims
import gov.cdc.prime.router.tokens.Scope
import gov.cdc.prime.router.tokens.authenticationFailure
import gov.cdc.prime.router.tokens.authorizationFailure
import kotlinx.serialization.json.Json
import org.apache.logging.log4j.kotlin.Logging
import java.nio.charset.StandardCharsets
import java.util.UUID

private const val PROCESSING_TYPE_PARAMETER = "processing"
Expand Down Expand Up @@ -199,7 +202,12 @@ class ReportFunction(
databaseAccess: DatabaseAccess = DatabaseAccess(),
piiRemovalCommands: PIIRemovalCommands = PIIRemovalCommands(),
): HttpResponseMessage {
val requestedReport = databaseAccess.fetchReportFile(reportId)
var requestedReport = ReportFile()
try {
requestedReport = databaseAccess.fetchReportFile(reportId)
} catch (e: Exception) {
HttpUtilities.badRequestResponse(request, "The requested report does not exist.")
}

return if (requestedReport.bodyUrl != null && requestedReport.bodyUrl.toString().lowercase().endsWith("fhir")) {
val contents = BlobAccess.downloadBlobAsByteArray(requestedReport.bodyUrl)
Expand All @@ -210,13 +218,10 @@ class ReportFunction(
if (envName == "prod") {
return HttpUtilities.badRequestResponse(request, "Must remove PII for messages from prod.")
}

val jsonObject = JacksonMapperUtilities.defaultMapper
.readValue(contents.toString(Charsets.UTF_8), Any::class.java)
JacksonMapperUtilities.defaultMapper.writeValueAsString(jsonObject)
String(contents, StandardCharsets.UTF_8)
}

HttpUtilities.okJSONResponse(request, content)
HttpUtilities.okJSONResponse(request, Json.parseToJsonElement(content))
} else if (requestedReport.bodyUrl == null) {
HttpUtilities.badRequestResponse(request, "The requested report does not exist.")
} else {
Expand Down
3 changes: 2 additions & 1 deletion prime-router/src/main/kotlin/cli/PIIRemovalCommands.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gov.cdc.prime.router.cli

import com.fasterxml.jackson.databind.json.JsonMapper
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.core.CliktError
import com.github.ajalt.clikt.parameters.options.option
Expand Down Expand Up @@ -117,7 +118,7 @@ class PIIRemovalCommands : CliktCommand(

val jsonObject = JacksonMapperUtilities.defaultMapper
.readValue(FhirTranscoder.encode(bundleAfterTransform), Any::class.java)
val prettyText = JacksonMapperUtilities.defaultMapper.writeValueAsString(jsonObject)
val prettyText = JsonMapper.builder().build().writeValueAsString(jsonObject)
return replaceIds(bundleAfterTransform, prettyText)
}

Expand Down
Loading

0 comments on commit 86c4b77

Please sign in to comment.