diff --git a/backend/pkg/utils/ips/render.go b/backend/pkg/utils/ips/render.go index 3f69c5b4..fb1d855c 100644 --- a/backend/pkg/utils/ips/render.go +++ b/backend/pkg/utils/ips/render.go @@ -11,6 +11,7 @@ import ( "github.com/fastenhealth/gofhir-models/fhir401" "gorm.io/datatypes" "html/template" + "math" ) //go:embed templates @@ -66,6 +67,19 @@ func NewNarrative() (*Narrative, error) { } return values }, + "roundList": func(precision uint, listVals []interface{}) []interface{} { + results := []interface{}{} + for ndx, _ := range listVals { + switch listVal := listVals[ndx].(type) { + case float64: + ratio := math.Pow(10, float64(precision)) + results = append(results, math.Round(listVal*ratio)/ratio) + default: + results = append(results, listVal) + } + } + return results + }, }).Funcs(sprig.FuncMap()).ParseFS(ipsTemplates, "templates/*.gohtml", "templates/includes/*.gohtml") if err != nil { diff --git a/backend/pkg/utils/ips/templates/allergies_intolerances.gohtml b/backend/pkg/utils/ips/templates/allergies_intolerances.gohtml index 4d0dda7f..9f2d7b43 100644 --- a/backend/pkg/utils/ips/templates/allergies_intolerances.gohtml +++ b/backend/pkg/utils/ips/templates/allergies_intolerances.gohtml @@ -32,7 +32,7 @@ Comments: AllergyIntolerance.note[x].text (separated by
) {{pluckList "text" ($entry.Manifestation | parseList) | uniq | join "," }} {{/* Reaction */}} {{pluckList "code" ($entry.Criticality | parseList) | uniq | join ","}} {{/* Severity */}} Comments {{/* Comments - TODO: use FHIRPath */}} - {{$entry.Onset | date "2006-01-02"}} {{/* Onset */}} + {{default "unknown" $entry.Onset }} {{/* Onset */}} {{end}} diff --git a/backend/pkg/utils/ips/templates/includes/observation-unit.gohtml b/backend/pkg/utils/ips/templates/includes/observation-unit.gohtml new file mode 100644 index 00000000..d6dac18d --- /dev/null +++ b/backend/pkg/utils/ips/templates/includes/observation-unit.gohtml @@ -0,0 +1,9 @@ +{{if not (empty .ValueString) }} + {{.ValueString}} +{{else if not (empty (.ValueQuantity | parseList)) }} + {{.ValueQuantity | parseList | pluckList "unit" | join "," }} +{{else if not (empty .ValueDate) }} + {{.ValueDate | date "2006-01-02" }} +{{else if not (empty (.ValueConcept | parseList)) }} + {{.ValueConcept}} +{{end}} diff --git a/backend/pkg/utils/ips/templates/includes/observation-value.gohtml b/backend/pkg/utils/ips/templates/includes/observation-value.gohtml new file mode 100644 index 00000000..409f62e9 --- /dev/null +++ b/backend/pkg/utils/ips/templates/includes/observation-value.gohtml @@ -0,0 +1,12 @@ +{{if not (empty .entry.ValueString) }} + {{.ValueString}} +{{else if not (empty (.entry.ValueQuantity | parseList)) }} + {{.entry.ValueQuantity | parseList | pluckList "value" | roundList 2 | join "," }} + {{if (get . "include_unit")}} + {{.entry.ValueQuantity | parseList | pluckList "unit" | join "," }} + {{end}} +{{else if not (empty .entry.ValueDate) }} + {{.entry.ValueDate | date "2006-01-02" }} +{{else if not (empty (.entry.ValueConcept | parseList)) }} + {{.entry.ValueConcept}} +{{end}} diff --git a/backend/pkg/utils/ips/templates/pregnancy.gohtml b/backend/pkg/utils/ips/templates/pregnancy.gohtml index ccb6f642..8e9dd53a 100644 --- a/backend/pkg/utils/ips/templates/pregnancy.gohtml +++ b/backend/pkg/utils/ips/templates/pregnancy.gohtml @@ -21,17 +21,7 @@ Date: Observation.effectiveDateTime || Observation.effectivePeriod.start {{range $index, $entry := .Observation }} {{pluckList "text" ($entry.Code | parseList) | uniq | join "," }} {{/* Code */}} - - {{if not (empty $entry.ValueString) }} - {{$entry.ValueString}} - {{else if not (empty ($entry.ValueQuantity | parseList)) }} - {{$entry.ValueQuantity}} - {{else if not (empty $entry.ValueDate) }} - {{$entry.ValueDate | date "2006-01-02" }} - {{else if not (empty ($entry.ValueConcept | parseList)) }} - {{$entry.ValueConcept}} - {{end}} - {{/* Result */}} + {{template "observation-value.gohtml" (dict "entry" $entry "include_unit" true)}} {{/* Result */}} Comments {{$entry.Date | date "2006-01-02"}} {{/* Date */}} diff --git a/backend/pkg/utils/ips/templates/social_history.gohtml b/backend/pkg/utils/ips/templates/social_history.gohtml index 17bab782..842806f9 100644 --- a/backend/pkg/utils/ips/templates/social_history.gohtml +++ b/backend/pkg/utils/ips/templates/social_history.gohtml @@ -23,17 +23,7 @@ Date: Observation.effectiveDateTime || Observation.effectivePeriod.start {{range $index, $entry := .Observation }} {{pluckList "text" ($entry.Code | parseList) | uniq | join "," }} {{/* Code */}} - - {{if not (empty $entry.ValueString) }} - {{$entry.ValueString}} - {{else if not (empty ($entry.ValueQuantity | parseList)) }} - {{$entry.ValueQuantity}} - {{else if not (empty $entry.ValueDate) }} - {{$entry.ValueDate | date "2006-01-02" }} - {{else if not (empty ($entry.ValueConcept | parseList)) }} - {{$entry.ValueConcept}} - {{end}} - {{/* Result */}} + {{template "observation-value.gohtml" (dict "entry" $entry "include_unit" false)}} {{/* Result */}} Unit Comments {{$entry.Date | date "2006-01-02"}} {{/* Date */}} diff --git a/backend/pkg/utils/ips/templates/vital_signs.gohtml b/backend/pkg/utils/ips/templates/vital_signs.gohtml index 2950a5d8..3176f280 100644 --- a/backend/pkg/utils/ips/templates/vital_signs.gohtml +++ b/backend/pkg/utils/ips/templates/vital_signs.gohtml @@ -26,18 +26,8 @@ Date: Observation.effectiveDateTime || Observation.effectivePeriod.start {{range $index, $entry := .Observation }} {{pluckList "text" ($entry.Code | parseList) | uniq | join "," }} {{/* Code */}} - - {{if not (empty $entry.ValueString) }} - {{$entry.ValueString}} - {{else if not (empty ($entry.ValueQuantity | parseList)) }} - {{$entry.ValueQuantity}} - {{else if not (empty $entry.ValueDate) }} - {{$entry.ValueDate | date "2006-01-02" }} - {{else if not (empty ($entry.ValueConcept | parseList)) }} - {{$entry.ValueConcept}} - {{end}} - {{/* Result */}} - Unit + {{template "observation-value.gohtml" (dict "entry" $entry "include_unit" false)}} {{/* Result */}} + {{template "observation-unit.gohtml" $entry}} Interpretation Comments {{$entry.Date | date "2006-01-02"}} {{/* Date */}}