Skip to content

Commit

Permalink
14609: findDetailedDeliveryHistory handles actions that produce multi…
Browse files Browse the repository at this point in the history
…ple reports (#16188)

* 14609: findDetailedDeliveryHistory handles actions that produce multiple reports

* fixup! 14609: findDetailedDeliveryHistory handles actions that produce multiple reports

* fixup! 14609: findDetailedDeliveryHistory handles actions that produce multiple reports
  • Loading branch information
mkalish authored Oct 11, 2024
1 parent 80f9da6 commit 031c752
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 11 deletions.
24 changes: 24 additions & 0 deletions prime-router/src/main/kotlin/history/DeliveryHistory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonPropertyOrder
import gov.cdc.prime.router.Receiver
import gov.cdc.prime.router.Topic
import gov.cdc.prime.router.azure.db.tables.pojos.Action
import gov.cdc.prime.router.azure.db.tables.pojos.ReportFile
import java.time.OffsetDateTime

/**
Expand Down Expand Up @@ -65,6 +67,28 @@ class DeliveryHistory(
schema_topic,
itemCount
) {

companion object {
fun createDeliveryHistoryFromReportAndAction(
reportFile: ReportFile,
action: Action,
): DeliveryHistory {
return DeliveryHistory(
actionId = action.actionId,
createdAt = action.createdAt,
receivingOrg = reportFile.receivingOrg,
receivingOrgSvc = reportFile.receivingOrgSvc,
externalName = action.externalName,
reportId = reportFile.reportId.toString(),
schema_topic = reportFile.schemaTopic,
itemCount = reportFile.itemCount,
bodyUrl = reportFile.bodyUrl,
schemaName = reportFile.schemaName,
bodyFormat = reportFile.bodyFormat
)
}
}

@JsonIgnore
private val DAYS_TO_SHOW = 30L

Expand Down
29 changes: 23 additions & 6 deletions prime-router/src/main/kotlin/history/azure/DeliveryFacade.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import java.util.*
*/
class DeliveryFacade(
private val dbDeliveryAccess: DatabaseDeliveryAccess = DatabaseDeliveryAccess(),
dbAccess: DatabaseAccess = BaseEngine.databaseAccessSingleton,
private val dbAccess: DatabaseAccess = BaseEngine.databaseAccessSingleton,
val reportService: ReportService = ReportService(),
) : ReportFileFacade(
dbAccess
Expand Down Expand Up @@ -91,17 +91,34 @@ class DeliveryFacade(
/**
* Get expanded details for a single report
*
* @param id either a report id (UUID) or action id (Long)
* @param deliveryId id for the delivery being used
* @return Report details
*/
fun findDetailedDeliveryHistory(
id: String,
deliveryId: Long,
): DeliveryHistory? {
val deliveryHistory = dbDeliveryAccess.fetchAction(
deliveryId,
orgName = null,
DeliveryHistory::class.java
)
// This functionality is handling the fact that the calling function supports loading the history either
// by the action id or report id
val reportFileId = try {
UUID.fromString(id)
} catch (ex: IllegalArgumentException) {
null
}

val deliveryHistory = if (reportFileId != null) {
val action = dbAccess.fetchAction(deliveryId)
val reportFile = dbAccess.fetchReportFile(reportFileId)
DeliveryHistory.createDeliveryHistoryFromReportAndAction(reportFile, action!!)
} else {
dbDeliveryAccess.fetchAction(
deliveryId,
orgName = null,
DeliveryHistory::class.java
)
}

val reportId = deliveryHistory?.reportId
if (reportId != null) {
val roots = reportService.getRootReports(UUID.fromString(reportId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class DeliveryFunction(
* @return
*/
override fun singleDetailedHistory(id: String, txn: DataAccessTransaction, action: Action): DeliveryHistory? {
return deliveryFacade.findDetailedDeliveryHistory(action.actionId)
return deliveryFacade.findDetailedDeliveryHistory(id, action.actionId)
}

@FunctionName("getDeliveriesV1")
Expand Down
48 changes: 48 additions & 0 deletions prime-router/src/test/kotlin/history/azure/DeliveryFacadeTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,53 @@ class DeliveryFacadeTests {
)
}

@Test
fun `test findDetailedDeliveryHistory with reportId`() {
val mockDeliveryAccess = mockk<DatabaseDeliveryAccess>()
val mockReportService = mockk<ReportService>()
val mockDbAccess = mockk<DatabaseAccess>()
val facade = DeliveryFacade(mockDeliveryAccess, mockDbAccess, mockReportService)

val reportFile = ReportFile()
reportFile.createdAt = OffsetDateTime.parse("2022-04-13T17:06:10.534Z")
reportFile.reportId = UUID.fromString("b3c8e304-8eff-4882-9000-3645054a30b7")
reportFile.sendingOrg = "DogCow Associates"
reportFile.schemaTopic = Topic.FULL_ELR
reportFile.itemCount = 1
reportFile.bodyUrl = "body-url"
reportFile.schemaName = ""
reportFile.bodyFormat = "HL7"
reportFile.receivingOrg = "ignore"
reportFile.receivingOrgSvc = "FULL-ELR"

val action = Action()
action.actionId = 1L
action.createdAt = reportFile.createdAt
action.externalName = "external"

every { mockDbAccess.fetchAction(any()) } returns action
every { mockDbAccess.fetchReportFile(any()) } returns reportFile

every {
mockReportService.getRootReports(
any(),
)
} returns listOf(reportFile)

val result = facade.findDetailedDeliveryHistory(
reportFile.reportId.toString(),
action.actionId,
)
assertThat(result?.actionId).isEqualTo(1L)
assertThat(result?.topic).isEqualTo(Topic.FULL_ELR)
assertThat(result?.reportId.toString()).isEqualTo(reportFile.reportId.toString())
assertThat(result?.reportItemCount).isEqualTo(1)
assertThat(result?.fileName).isEqualTo("body-url")
assertThat(result?.originalIngestion?.first()?.get("ingestionTime")).isEqualTo(reportFile.createdAt)
assertThat(result?.originalIngestion?.first()?.get("reportId")).isEqualTo(reportFile.reportId)
assertThat(result?.originalIngestion?.first()?.get("sendingOrg")).isEqualTo(reportFile.sendingOrg)
}

@Test
fun `test findDetailedDeliveryHistory`() {
val mockDeliveryAccess = mockk<DatabaseDeliveryAccess>()
Expand Down Expand Up @@ -258,6 +305,7 @@ class DeliveryFacadeTests {
} returns listOf(reportFile)

val result = facade.findDetailedDeliveryHistory(
delivery.actionId.toString(),
delivery.actionId,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ class DeliveryFunctionTests : Logging {
action.actionName = TaskAction.batch
every { mockDeliveryFacade.fetchActionForReportId(any()) } returns action
every { mockDeliveryFacade.fetchAction(any()) } returns null // not used for a UUID
every { mockDeliveryFacade.findDetailedDeliveryHistory(any()) } returns returnBody
every { mockDeliveryFacade.findDetailedDeliveryHistory(any(), any()) } returns returnBody
every { mockDeliveryFacade.checkAccessAuthorizationForAction(any(), any(), any()) } returns true
response = function.getDeliveryDetails(mockRequest, goodUuid)
assertThat(response.status).isEqualTo(HttpStatus.OK)
Expand Down Expand Up @@ -536,7 +536,7 @@ class DeliveryFunctionTests : Logging {
// Happy path with a good actionId
every { mockDeliveryFacade.fetchActionForReportId(any()) } returns null // not used for an actionId
every { mockDeliveryFacade.fetchAction(any()) } returns action
every { mockDeliveryFacade.findDetailedDeliveryHistory(any()) } returns returnBody
every { mockDeliveryFacade.findDetailedDeliveryHistory(any(), any()) } returns returnBody
every { mockDeliveryFacade.checkAccessAuthorizationForAction(any(), any(), any()) } returns true
response = function.getDeliveryDetails(mockRequest, goodActionId)
assertThat(response.status).isEqualTo(HttpStatus.OK)
Expand Down Expand Up @@ -713,7 +713,7 @@ class DeliveryFunctionTests : Logging {

every { mockDeliveryFacade.fetchActionForReportId(any()) } returns action
every { mockDeliveryFacade.fetchAction(any()) } returns null // not used for a UUID
every { mockDeliveryFacade.findDetailedDeliveryHistory(any()) } returns returnBody
every { mockDeliveryFacade.findDetailedDeliveryHistory(any(), any()) } returns returnBody
every { mockDeliveryFacade.checkAccessAuthorizationForAction(any(), any(), any()) } returns true

val restCreds = mockk<RestCredential>()
Expand Down Expand Up @@ -814,7 +814,7 @@ class DeliveryFunctionTests : Logging {

every { mockDeliveryFacade.fetchActionForReportId(any()) } returns action
every { mockDeliveryFacade.fetchAction(any()) } returns null // not used for a UUID
every { mockDeliveryFacade.findDetailedDeliveryHistory(any()) } returns null
every { mockDeliveryFacade.findDetailedDeliveryHistory(any(), any()) } returns null
every { mockDeliveryFacade.checkAccessAuthorizationForAction(any(), any(), any()) } returns true

val restCreds = mockk<RestCredential>()
Expand Down

0 comments on commit 031c752

Please sign in to comment.