Skip to content

Commit

Permalink
MMTC sender transform and getStateFromZipCode custom fhir function (#…
Browse files Browse the repository at this point in the history
…16440)

* added mmtc sender transform and custom fhir path function that grabs the state(s) from the given zip code
  • Loading branch information
lucero-v authored Nov 4, 2024
1 parent 60f2bd1 commit 32b36f0
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class CustomFhirPathFunctions : FhirPathFunctions {
LivdTableLookup,
GetFakeValueForElement,
FIPSCountyLookup,
GetStateFromZipCode,
;

companion object {
Expand Down Expand Up @@ -84,6 +85,14 @@ class CustomFhirPathFunctions : FhirPathFunctions {
)
}

CustomFhirPathFunctionNames.GetStateFromZipCode -> {
FunctionDetails(
"Looks up the states that match the given zip code",
0,
0
)
}

else -> null
}
}
Expand All @@ -110,6 +119,9 @@ class CustomFhirPathFunctions : FhirPathFunctions {
CustomFhirPathFunctionNames.FIPSCountyLookup -> {
fipsCountyLookup(parameters)
}
CustomFhirPathFunctionNames.GetStateFromZipCode -> {
getStateFromZipCode(focus)
}
else -> error(IllegalStateException("Tried to execute invalid FHIR Path function $functionName"))
}
)
Expand Down Expand Up @@ -353,4 +365,24 @@ class CustomFhirPathFunctions : FhirPathFunctions {
mutableListOf(StringType(parameters.first().first().primitiveValue()))
}
}

/**
* Returns a comma-separated string of the states that
* match the zip code stored in the [focus] element.
* @return a mutable list containing the state abbreviations
*/
fun getStateFromZipCode(
focus: MutableList<Base>,
metadata: Metadata = Metadata.getInstance(),
): MutableList<Base> {
val lookupTable = metadata.findLookupTable("zip-code-data")
var filters = lookupTable?.FilterBuilder() ?: error("Could not find table zip-code-data")

val zipCode = focus[0].primitiveValue()
filters = filters.isEqualTo("zipcode", zipCode)
val result = filters.findAllUnique("state_abbr")

val stateList = result.joinToString(",")
return mutableListOf(StringType(stateList))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
extends: classpath:/metadata/fhir_transforms/senders/original-pipeline-transforms.yml

elements:
- name: patient-state-from-zip-code
resource: "Bundle.entry.resource.ofType(Patient).address"
bundleProperty: '%resource.extension("https://reportstream.cdc.gov/fhir/StructureDefinition/state-from-zip-code").value[x]'
value: [ '%resource.postalCode.getStateFromZipCode()' ]

- name: ordering-facility-state-from-zip-code
resource: "Bundle.entry.resource.ofType(ServiceRequest).requester.resolve().organization.resolve().address"
bundleProperty: '%resource.extension("https://reportstream.cdc.gov/fhir/StructureDefinition/state-from-zip-code").value[x]'
value: [ '%resource.postalCode.getStateFromZipCode()' ]
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gov.cdc.prime.router.fhirengine.engine

import assertk.assertFailure
import assertk.assertThat
import assertk.assertions.contains
import assertk.assertions.isEqualTo
import assertk.assertions.isNotNull
import assertk.assertions.isNull
Expand Down Expand Up @@ -323,4 +324,85 @@ class CustomFhirPathFunctionTest {
(result[0] as StringType).value
).isEqualTo("Shasta")
}

@Test
fun `test getting state from zip code - one state`() {
val testTable = Table.create(
"zip-code-data",
StringColumn.create("state_fips", "40", "48", "6"),
StringColumn.create("state", "Oklahoma", "Texas", "California"),
StringColumn.create("state_abbr", "OK", "TX", "CA"),
StringColumn.create("zipcode", "73949", "73949", "92356"),
StringColumn.create("county", "Texas", "Sherman", "San Bernardino"),
StringColumn.create("city", "Texhoma", "", "Lucerne valley")

)
val testLookupTable = LookupTable(name = "zip-code-data", table = testTable)

mockkConstructor(Metadata::class)
every { anyConstructed<Metadata>().findLookupTable("zip-code-data") } returns testLookupTable
mockkObject(Metadata)
every { Metadata.getInstance() } returns UnitTestUtils.simpleMetadata

val result = CustomFhirPathFunctions().getStateFromZipCode(mutableListOf(StringType("92356")))

assertThat(
(result[0] as StringType).value
).isEqualTo("CA")
}

@Test
fun `test getting state from zip code - multiple states`() {
val testTable = Table.create(
"zip-code-data",
StringColumn.create("state_fips", "40", "48", "6"),
StringColumn.create("state", "Oklahoma", "Texas", "California"),
StringColumn.create("state_abbr", "OK", "TX", "CA"),
StringColumn.create("zipcode", "73949", "73949", "92356"),
StringColumn.create("county", "Texas", "Sherman", "San Bernardino"),
StringColumn.create("city", "Texhoma", "", "Lucerne valley")

)
val testLookupTable = LookupTable(name = "zip-code-data", table = testTable)

mockkConstructor(Metadata::class)
every { anyConstructed<Metadata>().findLookupTable("zip-code-data") } returns testLookupTable
mockkObject(Metadata)
every { Metadata.getInstance() } returns UnitTestUtils.simpleMetadata

val result = CustomFhirPathFunctions().getStateFromZipCode(mutableListOf(StringType("73949")))

assertThat(
(result[0] as StringType).value
).contains("OK")
assertThat(
(result[0] as StringType).value
).contains("TX")
}

@Test
fun `test getting state from zip code - no matching state found`() {
val testTable = Table.create(
"zip-code-data",
StringColumn.create("state_fips", "40", "48", "6"),
StringColumn.create("state", "Oklahoma", "Texas", "California"),
StringColumn.create("state_abbr", "OK", "TX", "CA"),
StringColumn.create("zipcode", "73949", "73949", "92356"),
StringColumn.create("county", "Texas", "Sherman", "San Bernardino"),
StringColumn.create("city", "Texhoma", "", "Lucerne valley")

)
val testLookupTable = LookupTable(name = "zip-code-data", table = testTable)

mockkConstructor(Metadata::class)
every { anyConstructed<Metadata>().findLookupTable("zip-code-data") } returns testLookupTable
mockkObject(Metadata)
every { Metadata.getInstance() } returns UnitTestUtils.simpleMetadata

val result = CustomFhirPathFunctions().getStateFromZipCode(mutableListOf(StringType("79902")))

assertThat(
(result[0] as StringType).value
).isEqualTo("")
}
}

0 comments on commit 32b36f0

Please sign in to comment.