From 5c0e48bb68ea39d82e67294a0645e6bd3a07324d Mon Sep 17 00:00:00 2001 From: Lucero Velasco <165824244+lucero-v@users.noreply.github.com> Date: Thu, 14 Nov 2024 10:36:22 -0700 Subject: [PATCH] added transform for converting datetimes to mountain time (#16542) --- .../hl7/utils/CustomFHIRFunctions.kt | 43 ++++++++++++++----- .../common/datetime-to-local-mtz.yml | 12 ++++++ 2 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 prime-router/src/main/resources/metadata/fhir_transforms/common/datetime-to-local-mtz.yml diff --git a/prime-router/src/main/kotlin/fhirengine/translation/hl7/utils/CustomFHIRFunctions.kt b/prime-router/src/main/kotlin/fhirengine/translation/hl7/utils/CustomFHIRFunctions.kt index c6900d23a94..819df5d2def 100644 --- a/prime-router/src/main/kotlin/fhirengine/translation/hl7/utils/CustomFHIRFunctions.kt +++ b/prime-router/src/main/kotlin/fhirengine/translation/hl7/utils/CustomFHIRFunctions.kt @@ -3,6 +3,7 @@ package gov.cdc.prime.router.fhirengine.translation.hl7.utils import ca.uhn.fhir.model.api.TemporalPrecisionEnum import fhirengine.translation.hl7.utils.FhirPathFunctions import fhirengine.translation.hl7.utils.helpers.convertDateToAge +import gov.cdc.prime.router.common.DateUtilities import gov.cdc.prime.router.fhirengine.translation.hl7.SchemaException import org.hl7.fhir.r4.fhirpath.FHIRPathUtilityClasses.FunctionDetails import org.hl7.fhir.r4.model.Base @@ -445,11 +446,6 @@ object CustomFHIRFunctions : FhirPathFunctions { throw SchemaException("Must call changeTimezone on a single element") } - val inputDate = focus[0] as? BaseDateTimeType ?: throw SchemaException( - "Must call changeTimezone on a dateTime, instant, or date; " + - "was attempted on a ${focus[0].fhirType()}" - ) - if (parameters == null || parameters[0].size != 1) { throw SchemaException("Must pass a timezone as the parameter") } @@ -465,14 +461,41 @@ object CustomFHIRFunctions : FhirPathFunctions { ) } - return when (inputDate.precision) { - TemporalPrecisionEnum.YEAR, TemporalPrecisionEnum.MONTH, TemporalPrecisionEnum.DAY, null -> mutableListOf( - inputDate + return if (focus[0] is StringType) { + if (focus[0].toString().length <= 8) { // we don't want to convert Date-only strings + return mutableListOf(StringType(focus[0].toString())) + } + + // TODO: find a way to pass in these values from receiver settings + + val dateTimeFormat = null + val convertPositiveDateTimeOffsetToNegative = null + val useHighPrecisionHeaderDateTimeFormat = null + + val formattedDate = DateUtilities.formatDateForReceiver( + DateUtilities.parseDate((focus[0].toString())), + ZoneId.of(inputTimeZone), + dateTimeFormat ?: DateUtilities.DateTimeFormat.OFFSET, + convertPositiveDateTimeOffsetToNegative ?: false, + useHighPrecisionHeaderDateTimeFormat ?: false ) - TemporalPrecisionEnum.MINUTE, TemporalPrecisionEnum.SECOND, TemporalPrecisionEnum.MILLI -> mutableListOf( - DateTimeType(inputDate.value, inputDate.precision, timezonePassed) + mutableListOf( + StringType(formattedDate) ) + } else { + val inputDate = focus[0] as? BaseDateTimeType ?: throw SchemaException( + "Must call changeTimezone on a dateTime, instant, or date; " + + "was attempted on a ${focus[0].fhirType()}" + ) + + when (inputDate.precision) { + TemporalPrecisionEnum.YEAR, TemporalPrecisionEnum.MONTH, TemporalPrecisionEnum.DAY, null -> + mutableListOf(inputDate) + + TemporalPrecisionEnum.MINUTE, TemporalPrecisionEnum.SECOND, TemporalPrecisionEnum.MILLI -> + mutableListOf(DateTimeType(inputDate.value, inputDate.precision, timezonePassed)) + } } } } \ No newline at end of file diff --git a/prime-router/src/main/resources/metadata/fhir_transforms/common/datetime-to-local-mtz.yml b/prime-router/src/main/resources/metadata/fhir_transforms/common/datetime-to-local-mtz.yml new file mode 100644 index 00000000000..f87b4e3c7df --- /dev/null +++ b/prime-router/src/main/resources/metadata/fhir_transforms/common/datetime-to-local-mtz.yml @@ -0,0 +1,12 @@ +elements: + - name: hl7-datetime-to-local + resource: "Bundle.descendants().where(url='https://reportstream.cdc.gov/fhir/StructureDefinition/hl7v2-date-time')" + bundleProperty: "%resource.value[x]" + value: ["%resource.value.changeTimezone('America/Denver')"] + + + - name: message-date-time-to-local + resource: 'Bundle.entry.resource.ofType(MessageHeader).extension("https://reportstream.cdc.gov/fhir/StructureDefinition/msh-message-header").extension("MSH.7")' + condition: '%resource.value.exists()' + bundleProperty: '%resource.value[x]' + value: ["%resource.value.changeTimezone('America/Denver')"]