Healthy Route Implementation Guide
0.2.0 - ci-build

Healthy Route Implementation Guide, published by Savannah Informatics. This guide is not an authorized publication; it is the continuous build for version 0.2.0 built by the FHIR (HL7® FHIR® Standard) CI Build. This version is based on the current content of https://github.com/savannahghi/healthy-route-ig/ and changes regularly. See the Directory of published versions

StructureMap: Extract the diet and activity form (retired)

Official URL: https://fhir.optimalhealth.org/ig/healthy-route/StructureMap/HRExtractDietActivity Version: 0.2.0
Draft as of 2026-11-12 Computable Name: HRExtractDietActivity

/// url = 'https://fhir.optimalhealth.org/ig/healthy-route/StructureMap/HRExtractDietActivity'
/// version = '0.2.0'
/// name = 'HRExtractDietActivity'
/// status = 'draft'

uses "http://hl7.org/fhir/StructureDefinition/QuestionnaireResponse" alias QR as source
uses "http://hl7.org/fhir/StructureDefinition/Bundle" alias Bundle as target
uses "http://hl7.org/fhir/StructureDefinition/Observation" alias Observation as target

group HRExtractDietActivity(source qr : QR, target bundle : Bundle) {
  qr -> bundle.type = 'transaction' "type";
  qr.item as it where (linkId = 'fruit-days') then {
    it.answer first as a ->  bundle.entry as e,  e.resource = create('Observation') as obs then {
      qr -> obs then SurveyObservation(qr, it, obs, e) "base";
      qr -> obs.code = create('CodeableConcept') as cv then {
        qr -> cv.coding = create('Coding') as cd then {
          qr -> cd.system = 'https://fhir.optimalhealth.org/ig/healthy-route/CodeSystem/hr-measurement' "codeSystem";
          qr -> cd.code = 'fruit-days' "codeCode";
          qr -> cd.display = 'Days fruit eaten in a usual week' "codeDisplay";
        } "codeCoding";
      } "code";
      a.value as v -> obs.value = v;
    } "obs";
  } "fruit";
  qr.item as it where (linkId = 'veg-days') then {
    it.answer first as a ->  bundle.entry as e,  e.resource = create('Observation') as obs then {
      qr -> obs then SurveyObservation(qr, it, obs, e) "base";
      qr -> obs.code = create('CodeableConcept') as cv then {
        qr -> cv.coding = create('Coding') as cd then {
          qr -> cd.system = 'https://fhir.optimalhealth.org/ig/healthy-route/CodeSystem/hr-measurement' "codeSystem";
          qr -> cd.code = 'veg-days' "codeCode";
          qr -> cd.display = 'Days vegetables eaten in a usual week' "codeDisplay";
        } "codeCoding";
      } "code";
      a.value as v -> obs.value = v;
    } "obs";
  } "veg";
  qr.item as it where (linkId = 'salt-added') then {
    it.answer first as a ->  bundle.entry as e,  e.resource = create('Observation') as obs then {
      qr -> obs then SurveyObservation(qr, it, obs, e) "base";
      qr -> obs.code = create('CodeableConcept') as cv then {
        qr -> cv.coding = create('Coding') as cd then {
          qr -> cd.system = 'https://fhir.optimalhealth.org/ig/healthy-route/CodeSystem/hr-measurement' "codeSystem";
          qr -> cd.code = 'salt-added' "codeCode";
          qr -> cd.display = 'Salt added at the table' "codeDisplay";
        } "codeCoding";
      } "code";
      a.value as v ->  obs.value = create('CodeableConcept') as concept,  concept.coding = v;
    } "obs";
  } "salt";
  qr.item as it where (linkId = 'sugar-drinks') then {
    it.answer first as a ->  bundle.entry as e,  e.resource = create('Observation') as obs then {
      qr -> obs then SurveyObservation(qr, it, obs, e) "base";
      qr -> obs.code = create('CodeableConcept') as cv then {
        qr -> cv.coding = create('Coding') as cd then {
          qr -> cd.system = 'https://fhir.optimalhealth.org/ig/healthy-route/CodeSystem/hr-measurement' "codeSystem";
          qr -> cd.code = 'sugar-drinks' "codeCode";
          qr -> cd.display = 'Sugar-sweetened drinks' "codeDisplay";
        } "codeCoding";
      } "code";
      a.value as v ->  obs.value = create('CodeableConcept') as concept,  concept.coding = v;
    } "obs";
  } "sugar";
  qr.item as it where (linkId = 'work-activity') then {
    it.answer first as a ->  bundle.entry as e,  e.resource = create('Observation') as obs then {
      qr -> obs then SurveyObservation(qr, it, obs, e) "base";
      qr -> obs.code = create('CodeableConcept') as cv then {
        qr -> cv.coding = create('Coding') as cd then {
          qr -> cd.system = 'https://fhir.optimalhealth.org/ig/healthy-route/CodeSystem/hr-measurement' "codeSystem";
          qr -> cd.code = 'work-activity' "codeCode";
          qr -> cd.display = 'Physical activity at work' "codeDisplay";
        } "codeCoding";
      } "code";
      a.value as v ->  obs.value = create('CodeableConcept') as concept,  concept.coding = v;
    } "obs";
  } "work";
  qr.item as it where (linkId = 'activity-days') then {
    it.answer first as a ->  bundle.entry as e,  e.resource = create('Observation') as obs then {
      qr -> obs then SurveyObservation(qr, it, obs, e) "base";
      qr -> obs.code = create('CodeableConcept') as cv then {
        qr -> cv.coding = create('Coding') as cd then {
          qr -> cd.system = 'https://fhir.optimalhealth.org/ig/healthy-route/CodeSystem/hr-measurement' "codeSystem";
          qr -> cd.code = 'activity-days' "codeCode";
          qr -> cd.display = 'Days of moderate or vigorous activity in a usual week' "codeDisplay";
        } "codeCoding";
      } "code";
      a.value as v -> obs.value = v;
    } "obs";
  } "activityDays";
  qr.item as it where (linkId = 'activity-minutes') then {
    it.answer first as a ->  bundle.entry as e,  e.resource = create('Observation') as obs then {
      qr -> obs then SurveyObservation(qr, it, obs, e) "base";
      qr -> obs.code = create('CodeableConcept') as cv then {
        qr -> cv.coding = create('Coding') as cd then {
          qr -> cd.system = 'https://fhir.optimalhealth.org/ig/healthy-route/CodeSystem/hr-measurement' "codeSystem";
          qr -> cd.code = 'activity-minutes' "codeCode";
          qr -> cd.display = 'Minutes of moderate or vigorous activity on those days' "codeDisplay";
        } "codeCoding";
      } "code";
      a.value as v -> obs.value = v;
    } "obs";
  } "activityMinutes";
  qr.item as it where (linkId = 'seated-hours') then {
    it.answer first as a ->  bundle.entry as e,  e.resource = create('Observation') as obs then {
      qr -> obs then SurveyObservation(qr, it, obs, e) "base";
      qr -> obs.code = create('CodeableConcept') as cv then {
        qr -> cv.coding = create('Coding') as cd then {
          qr -> cd.system = 'https://fhir.optimalhealth.org/ig/healthy-route/CodeSystem/hr-measurement' "codeSystem";
          qr -> cd.code = 'seated-hours' "codeCode";
          qr -> cd.display = 'Hours seated in a usual day' "codeDisplay";
        } "codeCoding";
      } "code";
      a.value as v -> obs.value = v;
    } "obs";
  } "seated";
  qr -> evaluate(qr, id.replaceMatches('/_history/.*$', '').replaceMatches('^.*/', '')) as qid then {
    qr.item as it where (linkId = 'fruit-veg-5plus') then {
      it.answer first as a ->  bundle.entry as e,  e.resource = create('Observation') as obs then {
        qr -> obs then SurveyObservation(qr, it, obs, e) "base";
        qr -> obs then Derived(qr, obs) "derivedCategory";
        qr -> obs.code = create('CodeableConcept') as cv then {
          qr -> cv.coding = create('Coding') as cd then {
            qr -> cd.system = 'https://fhir.optimalhealth.org/ig/healthy-route/CodeSystem/hr-measurement' "codeSystem";
            qr -> cd.code = 'fruit-veg-5plus' "codeCode";
            qr -> cd.display = 'Fruit and vegetables eaten five days or more' "codeDisplay";
          } "codeCoding";
        } "code";
        a.value as v -> obs.value = v;
        qr ->  obs.derivedFrom = create('Reference') as r,  r.reference = append('Observation/', qid, '-fruit-days') "fruit";
        qr ->  obs.derivedFrom = create('Reference') as r,  r.reference = append('Observation/', qid, '-veg-days') "veg";
      } "obs";
    } "fruitVeg";
    qr.item as it where (linkId = 'activity-weekly-minutes') then {
      it.answer first as a ->  bundle.entry as e,  e.resource = create('Observation') as obs then {
        qr -> obs then SurveyObservation(qr, it, obs, e) "base";
        qr -> obs then Derived(qr, obs) "derivedCategory";
        qr -> obs.code = create('CodeableConcept') as cv then {
          qr -> cv.coding = create('Coding') as cd then {
            qr -> cd.system = 'https://fhir.optimalhealth.org/ig/healthy-route/CodeSystem/hr-measurement' "codeSystem";
            qr -> cd.code = 'activity-weekly-minutes' "codeCode";
            qr -> cd.display = 'Weekly minutes of moderate or vigorous activity' "codeDisplay";
          } "codeCoding";
        } "code";
        a.value as v -> obs.value = create('Quantity') as q then {
          v -> q.value = v "value";
          qr -> q.system = 'http://unitsofmeasure.org' "system";
          qr -> q.code = 'min' "unitCode";
          qr -> q.unit = 'min' "unit";
        } "quantity";
        qr ->  obs.derivedFrom = create('Reference') as r,  r.reference = append('Observation/', qid, '-activity-days') "days";
        qr ->  obs.derivedFrom = create('Reference') as r,  r.reference = append('Observation/', qid, '-activity-minutes') "minutes";
      } "obs";
    } "weeklyMinutes";
    qr.item as it where (linkId = 'activity-who-met') then {
      it.answer first as a ->  bundle.entry as e,  e.resource = create('Observation') as obs then {
        qr -> obs then SurveyObservation(qr, it, obs, e) "base";
        qr -> obs then Derived(qr, obs) "derivedCategory";
        qr -> obs.code = create('CodeableConcept') as cv then {
          qr -> cv.coding = create('Coding') as cd then {
            qr -> cd.system = 'https://fhir.optimalhealth.org/ig/healthy-route/CodeSystem/hr-measurement' "codeSystem";
            qr -> cd.code = 'activity-who-met' "codeCode";
            qr -> cd.display = 'WHO physical activity recommendation met' "codeDisplay";
          } "codeCoding";
        } "code";
        a.value as v -> obs.value = v;
        qr ->  obs.derivedFrom = create('Reference') as r,  r.reference = append('Observation/', qid, '-activity-weekly-minutes') "weekly";
      } "obs";
    } "whoMet";
  } "derived";
}

// The category a value worked out on this form carries beside its FHIR one.
group Derived(source qr : QR, target obs : Observation) {
  qr -> obs.category = create('CodeableConcept') as cv then {
    qr -> cv.coding = create('Coding') as cd then {
      qr -> cd.system = 'https://fhir.optimalhealth.org/ig/healthy-route/CodeSystem/hr-observation-category' "derivedSystem";
      qr -> cd.code = 'derived' "derivedCode";
      qr -> cd.display = 'Derived' "derivedDisplay";
    } "derivedCoding";
  } "category";
}

group SurveyObservation(source qr : QR, source it, target obs : Observation, target e) {
  qr -> evaluate(qr, id.replaceMatches('/_history/.*$', '').replaceMatches('^.*/', '')) as qid then {
    it.linkId as k ->  append(qid, '-', k) as idText,  obs.id = cast(idText, 'id') "id";
    it.linkId as k -> e.fullUrl = append('Observation/', qid, '-', k) "fullUrl";
    it.linkId as k -> e.request as rq then {
      qr -> rq.method = 'PUT' "method";
      qr -> rq.url = append('Observation/', qid, '-', k) "url";
    } "request";
  } "ids";
  qr ->  obs.meta as m,  m.profile = 'https://fhir.optimalhealth.org/ig/healthy-route/StructureDefinition/hr-observation' "profile";
  qr.meta as qm then {
    qm.tag as t ->  obs.meta as m,  m.tag = t;
  } "tags";
  qr.extension as x where (url = 'https://fhir.optimalhealth.org/ig/healthy-route/StructureDefinition/hr-tenant') -> obs.extension = x "tenant";
  qr -> obs.status = 'final' "status";
  qr -> obs.category = create('CodeableConcept') as cv then {
    qr -> cv.coding = create('Coding') as cd then {
      qr -> cd.system = 'http://terminology.hl7.org/CodeSystem/observation-category' "surveySystem";
      qr -> cd.code = 'survey' "surveyCode";
      qr -> cd.display = 'Survey' "surveyDisplay";
    } "surveyCoding";
  } "survey";
  qr.subject as s -> obs.subject = s;
  qr.encounter as en -> obs.encounter = en;
  qr.authored as t -> obs.effective = t "effective";
  qr.author as au -> obs.performer = au "performer";
}