From 031c752af9cd8d1c62d466ea8031d335fb6fad17 Mon Sep 17 00:00:00 2001 From: Michael Kalish Date: Fri, 11 Oct 2024 10:48:53 -0400 Subject: [PATCH] 14609: findDetailedDeliveryHistory handles actions that produce multiple 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 --- .../main/kotlin/history/DeliveryHistory.kt | 24 ++++++++++ .../kotlin/history/azure/DeliveryFacade.kt | 29 ++++++++--- .../kotlin/history/azure/DeliveryFunction.kt | 2 +- .../history/azure/DeliveryFacadeTests.kt | 48 +++++++++++++++++++ .../history/azure/DeliveryFunctionTests.kt | 8 ++-- 5 files changed, 100 insertions(+), 11 deletions(-) diff --git a/prime-router/src/main/kotlin/history/DeliveryHistory.kt b/prime-router/src/main/kotlin/history/DeliveryHistory.kt index f5778d5f5b0..d90227fc436 100644 --- a/prime-router/src/main/kotlin/history/DeliveryHistory.kt +++ b/prime-router/src/main/kotlin/history/DeliveryHistory.kt @@ -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 /** @@ -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 diff --git a/prime-router/src/main/kotlin/history/azure/DeliveryFacade.kt b/prime-router/src/main/kotlin/history/azure/DeliveryFacade.kt index 3b8e11bbf94..17421529b50 100644 --- a/prime-router/src/main/kotlin/history/azure/DeliveryFacade.kt +++ b/prime-router/src/main/kotlin/history/azure/DeliveryFacade.kt @@ -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 @@ -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)) diff --git a/prime-router/src/main/kotlin/history/azure/DeliveryFunction.kt b/prime-router/src/main/kotlin/history/azure/DeliveryFunction.kt index 71fd3deaa46..3a8c9c7065e 100644 --- a/prime-router/src/main/kotlin/history/azure/DeliveryFunction.kt +++ b/prime-router/src/main/kotlin/history/azure/DeliveryFunction.kt @@ -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") diff --git a/prime-router/src/test/kotlin/history/azure/DeliveryFacadeTests.kt b/prime-router/src/test/kotlin/history/azure/DeliveryFacadeTests.kt index 7e71b827185..1f7b45bd440 100644 --- a/prime-router/src/test/kotlin/history/azure/DeliveryFacadeTests.kt +++ b/prime-router/src/test/kotlin/history/azure/DeliveryFacadeTests.kt @@ -218,6 +218,53 @@ class DeliveryFacadeTests { ) } + @Test + fun `test findDetailedDeliveryHistory with reportId`() { + val mockDeliveryAccess = mockk() + val mockReportService = mockk() + val mockDbAccess = mockk() + 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() @@ -258,6 +305,7 @@ class DeliveryFacadeTests { } returns listOf(reportFile) val result = facade.findDetailedDeliveryHistory( + delivery.actionId.toString(), delivery.actionId, ) diff --git a/prime-router/src/test/kotlin/history/azure/DeliveryFunctionTests.kt b/prime-router/src/test/kotlin/history/azure/DeliveryFunctionTests.kt index bcef66ebe44..ec054de8d1c 100644 --- a/prime-router/src/test/kotlin/history/azure/DeliveryFunctionTests.kt +++ b/prime-router/src/test/kotlin/history/azure/DeliveryFunctionTests.kt @@ -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) @@ -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) @@ -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() @@ -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()