SGHI FHIR Profile Implementation Guide
0.1.0 - ci-build

SGHI FHIR Profile Implementation Guide, published by Kathurima Kimathi. This guide is not an authorized publication; it is the continuous build for version 0.1.0 built by the FHIR (HL7® FHIR® Standard) CI Build. This version is based on the current content of https://github.com/savannahghi/sil_fhir_profile_ig/ and changes regularly. See the Directory of published versions

StructureMap: Admission Form Extraction

Official URL: https://fhir.slade360.co.ke/fhir/StructureMap/ExtractAdmissionForm Version: 0.1.0
Active as of 2026-09-14 Computable Name: ExtractAdmissionForm

The measurements and vitals on arrival as their own Observations under their real LOINC codes; the head-to-toe nursing assessment as one panel, because it is read as a picture of the patient on arrival and no single line means much alone; the chronic illness tick list as one Condition per illness with the reported onset; and one ServiceRequest per investigation and treatment ordered. The chronic illnesses carry verificationStatus 'unconfirmed' – a tick against a printed word on an admission sheet is a reported history, not a diagnosis.

/// url = 'https://fhir.slade360.co.ke/fhir/StructureMap/ExtractAdmissionForm'
/// name = 'ExtractAdmissionForm'
/// title = 'Admission Form Extraction'
/// status = 'active'
/// description = 'The measurements and vitals on arrival as their own Observations under their real LOINC codes; the head-to-toe nursing assessment as one panel, because it is read as a picture of the patient on arrival and no single line means much alone; the chronic illness tick list as one Condition per illness with the reported onset; and one ServiceRequest per investigation and treatment ordered. The chronic illnesses carry verificationStatus \'unconfirmed\' -- a tick against a printed word on an admission sheet is a reported history, not a diagnosis.'

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
uses "http://hl7.org/fhir/StructureDefinition/Condition" alias Condition as target
uses "http://hl7.org/fhir/StructureDefinition/ServiceRequest" alias ServiceRequest as target

group ExtractAdmissionForm(source qr : QR, target bundle : Bundle) {
  qr -> bundle.type = 'transaction' "setBundleType";
  // ── On arrival ──────────────────────────────────────────────────────────
  qr.item as g where (linkId = 'on-arrival') then {
    g.item as it where ((linkId = 'on-arrival/height') and answer.value.exists()) ->  bundle.entry as entry,  entry.resource = create('Observation') as obs then {
      qr ->  obs,  entry then BuildObsBase(qr, obs, entry) "base";
      qr -> obs then SetVitalSignsCategory(qr, obs) "category";
      it -> obs.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'http://loinc.org' "sys";
          it -> cg.code = '8302-2' "cd";
          it -> cg.display = 'Body height' "dsp";
        } "coding";
      } "code";
      it.answer first as a then {
        a.value as v -> obs.value = v "set";
      };
    } "measure_8302_2";
    g.item as it where ((linkId = 'on-arrival/weight') and answer.value.exists()) ->  bundle.entry as entry,  entry.resource = create('Observation') as obs then {
      qr ->  obs,  entry then BuildObsBase(qr, obs, entry) "base";
      qr -> obs then SetVitalSignsCategory(qr, obs) "category";
      it -> obs.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'http://loinc.org' "sys";
          it -> cg.code = '29463-7' "cd";
          it -> cg.display = 'Body weight' "dsp";
        } "coding";
      } "code";
      it.answer first as a then {
        a.value as v -> obs.value = v "set";
      };
    } "measure_29463_7";
    g.item as it where ((linkId = 'on-arrival/vitals/temperature') and answer.value.exists()) ->  bundle.entry as entry,  entry.resource = create('Observation') as obs then {
      qr ->  obs,  entry then BuildObsBase(qr, obs, entry) "base";
      qr -> obs then SetVitalSignsCategory(qr, obs) "category";
      it -> obs.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'http://loinc.org' "sys";
          it -> cg.code = '8310-5' "cd";
          it -> cg.display = 'Body temperature' "dsp";
        } "coding";
      } "code";
      it.answer first as a then {
        a.value as v -> obs.value = v "set";
      };
    } "measure_8310_5";
    g.item as it where ((linkId = 'on-arrival/vitals/pulse') and answer.value.exists()) ->  bundle.entry as entry,  entry.resource = create('Observation') as obs then {
      qr ->  obs,  entry then BuildObsBase(qr, obs, entry) "base";
      qr -> obs then SetVitalSignsCategory(qr, obs) "category";
      it -> obs.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'http://loinc.org' "sys";
          it -> cg.code = '8867-4' "cd";
          it -> cg.display = 'Heart rate' "dsp";
        } "coding";
      } "code";
      it.answer first as a then {
        a.value as v -> obs.value = v "set";
      };
    } "measure_8867_4";
    g.item as it where ((linkId = 'on-arrival/vitals/respiratory-rate') and answer.value.exists()) ->  bundle.entry as entry,  entry.resource = create('Observation') as obs then {
      qr ->  obs,  entry then BuildObsBase(qr, obs, entry) "base";
      qr -> obs then SetVitalSignsCategory(qr, obs) "category";
      it -> obs.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'http://loinc.org' "sys";
          it -> cg.code = '9279-1' "cd";
          it -> cg.display = 'Respiratory rate' "dsp";
        } "coding";
      } "code";
      it.answer first as a then {
        a.value as v -> obs.value = v "set";
      };
    } "measure_9279_1";
    g.item as it where ((linkId = 'on-arrival/vitals/blood-pressure') and answer.value.exists()) ->  bundle.entry as entry,  entry.resource = create('Observation') as obs then {
      qr ->  obs,  entry then BuildObsBase(qr, obs, entry) "base";
      qr -> obs then SetVitalSignsCategory(qr, obs) "category";
      it -> obs.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'http://loinc.org' "sys";
          it -> cg.code = '85354-9' "cd";
          it -> cg.display = 'Blood pressure panel with all children optional' "dsp";
        } "coding";
      } "code";
      it.answer first as a then {
        a.value as txt ->  obs.note as n,  n.text = txt "bpText";
      };
    } "arrivalBloodPressure";
  } "onArrivalGroup";
  // ── How the patient got here ────────────────────────────────────────────
  qr.item as g where ((linkId = 'admission') and item.answer.value.exists()) ->  bundle.entry as entry,  entry.resource = create('Observation') as obs then {
    qr ->  obs,  entry then BuildObsBase(qr, obs, entry) "base";
    qr -> obs then SetSurveyCategory(qr, obs) "category";
    g -> obs.code as cc then {
      g -> cc.coding as cg then {
        g -> cg.system = 'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem' "sys";
        g -> cg.code = 'admission-route-how-the-patient-arrived' "cd";
        g -> cg.display = 'How the patient arrived' "dsp";
      } "coding";
    } "code";
    g.item as it where ((linkId = 'admission/admitted-from') and answer.value.exists()) -> obs.component as comp then {
      it -> comp.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem' "sys";
          it -> cg.code = 'admission-route-admitted-from' "cd";
          it -> cg.display = 'Admitted from' "dsp";
        } "coding";
      } "code";
      it -> comp then SetComponentCodedValue(it, comp) "value";
    } "comp_admission_route_admitted_from";
    g.item as it where ((linkId = 'admission/arrival-mode') and answer.value.exists()) -> obs.component as comp then {
      it -> comp.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem' "sys";
          it -> cg.code = 'admission-route-mode-of-arrival' "cd";
          it -> cg.display = 'Mode of arrival' "dsp";
        } "coding";
      } "code";
      it -> comp then SetComponentCodedValue(it, comp) "value";
    } "comp_admission_route_mode_of_arrival";
    g.item as it where ((linkId = 'admission/accompanied-by') and answer.value.exists()) -> obs.component as comp then {
      it -> comp.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem' "sys";
          it -> cg.code = 'admission-route-accompanied-by' "cd";
          it -> cg.display = 'Accompanied by' "dsp";
        } "coding";
      } "code";
      it -> comp then SetComponentValue(it, comp) "value";
    } "comp_admission_route_accompanied_by";
    g.item as it where ((linkId = 'admission/unit') and answer.value.exists()) -> obs.component as comp then {
      it -> comp.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem' "sys";
          it -> cg.code = 'admission-route-unit-admitted-to' "cd";
          it -> cg.display = 'Unit admitted to' "dsp";
        } "coding";
      } "code";
      it -> comp then SetComponentValue(it, comp) "value";
    } "comp_admission_route_unit_admitted_to";
    g.item as it where ((linkId = 'admission/room') and answer.value.exists()) -> obs.component as comp then {
      it -> comp.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem' "sys";
          it -> cg.code = 'admission-route-room' "cd";
          it -> cg.display = 'Room' "dsp";
        } "coding";
      } "code";
      it -> comp then SetComponentValue(it, comp) "value";
    } "comp_admission_route_room";
    g.item as it where ((linkId = 'admission/bed') and answer.value.exists()) -> obs.component as comp then {
      it -> comp.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem' "sys";
          it -> cg.code = 'admission-route-bed' "cd";
          it -> cg.display = 'Bed' "dsp";
        } "coding";
      } "code";
      it -> comp then SetComponentValue(it, comp) "value";
    } "comp_admission_route_bed";
  } "admissionRoute";
  qr.item as it where ((linkId = 'presenting-symptoms') and answer.value.exists()) ->  bundle.entry as entry,  entry.resource = create('Observation') as obs then {
    qr ->  obs,  entry then BuildObsBase(qr, obs, entry) "base";
    qr -> obs then SetSurveyCategory(qr, obs) "category";
    it -> obs.code as cc then {
      it -> cc.coding as cg then {
        it -> cg.system = 'http://loinc.org' "sys";
        it -> cg.code = '10154-3' "cd";
        it -> cg.display = 'Chief complaint Narrative - Reported' "dsp";
      } "coding";
      it -> cc.text = 'Presenting symptoms on admission' "codeText";
    } "code";
    it.answer first as a then {
      a.value as txt ->  obs.note as n,  n.text = txt "text";
    };
  } "narr_10154_3";
  // ── The head-to-toe nursing assessment ──────────────────────────────────
  // One panel. The sheet gives one ruled line per system and it is read whole:
  // "cool, clammy extremities" beside "confused" is the finding, and neither
  // line alone says it.
  qr.item as g where ((linkId = 'assessment') and item.answer.value.exists()) ->  bundle.entry as entry,  entry.resource = create('Observation') as obs then {
    qr ->  obs,  entry then BuildObsBase(qr, obs, entry) "base";
    qr -> obs then SetExamCategory(qr, obs) "category";
    g -> obs.code as cc then {
      g -> cc.coding as cg then {
        g -> cg.system = 'http://loinc.org' "sys";
        g -> cg.code = '29545-1' "cd";
        g -> cg.display = 'Physical findings note' "dsp";
      } "coding";
    } "code";
    g.item as it where ((linkId = 'assessment/orientation') and answer.value.exists()) -> obs.component as comp then {
      it -> comp.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem' "sys";
          it -> cg.code = 'nursing-assessment-orientation-on-admission' "cd";
          it -> cg.display = 'Orientation on admission' "dsp";
        } "coding";
      } "code";
      it -> comp then SetComponentValue(it, comp) "value";
    } "comp_nursing_assessment_orientation_on_admission";
    g.item as it where ((linkId = 'assessment/pain-and-location') and answer.value.exists()) -> obs.component as comp then {
      it -> comp.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem' "sys";
          it -> cg.code = 'nursing-assessment-pain-and-its-location' "cd";
          it -> cg.display = 'Pain and its location' "dsp";
        } "coding";
      } "code";
      it -> comp then SetComponentValue(it, comp) "value";
    } "comp_nursing_assessment_pain_and_its_location";
    g.item as it where ((linkId = 'assessment/skin') and answer.value.exists()) -> obs.component as comp then {
      it -> comp.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem' "sys";
          it -> cg.code = 'nursing-assessment-skin-on-admission' "cd";
          it -> cg.display = 'Skin on admission' "dsp";
        } "coding";
      } "code";
      it -> comp then SetComponentValue(it, comp) "value";
    } "comp_nursing_assessment_skin_on_admission";
    g.item as it where ((linkId = 'assessment/extremities') and answer.value.exists()) -> obs.component as comp then {
      it -> comp.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem' "sys";
          it -> cg.code = 'nursing-assessment-extremities-on-admission' "cd";
          it -> cg.display = 'Extremities on admission' "dsp";
        } "coding";
      } "code";
      it -> comp then SetComponentValue(it, comp) "value";
    } "comp_nursing_assessment_extremities_on_admission";
    g.item as it where ((linkId = 'assessment/eyes') and answer.value.exists()) -> obs.component as comp then {
      it -> comp.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem' "sys";
          it -> cg.code = 'nursing-assessment-eyes-on-admission' "cd";
          it -> cg.display = 'Eyes on admission' "dsp";
        } "coding";
      } "code";
      it -> comp then SetComponentValue(it, comp) "value";
    } "comp_nursing_assessment_eyes_on_admission";
    g.item as it where ((linkId = 'assessment/hearing') and answer.value.exists()) -> obs.component as comp then {
      it -> comp.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem' "sys";
          it -> cg.code = 'nursing-assessment-hearing-on-admission' "cd";
          it -> cg.display = 'Hearing on admission' "dsp";
        } "coding";
      } "code";
      it -> comp then SetComponentValue(it, comp) "value";
    } "comp_nursing_assessment_hearing_on_admission";
    g.item as it where ((linkId = 'assessment/mouth-and-teeth') and answer.value.exists()) -> obs.component as comp then {
      it -> comp.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem' "sys";
          it -> cg.code = 'nursing-assessment-mouth-and-teeth-on-admission' "cd";
          it -> cg.display = 'Mouth and teeth on admission' "dsp";
        } "coding";
      } "code";
      it -> comp then SetComponentValue(it, comp) "value";
    } "comp_nursing_assessment_mouth_and_teeth_on_admission";
    g.item as it where ((linkId = 'assessment/lungs') and answer.value.exists()) -> obs.component as comp then {
      it -> comp.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem' "sys";
          it -> cg.code = 'nursing-assessment-lungs-on-admission' "cd";
          it -> cg.display = 'Lungs on admission' "dsp";
        } "coding";
      } "code";
      it -> comp then SetComponentValue(it, comp) "value";
    } "comp_nursing_assessment_lungs_on_admission";
    g.item as it where ((linkId = 'assessment/cardiovascular') and answer.value.exists()) -> obs.component as comp then {
      it -> comp.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem' "sys";
          it -> cg.code = 'nursing-assessment-cardiovascular-examination-on-admission' "cd";
          it -> cg.display = 'Cardiovascular examination on admission' "dsp";
        } "coding";
      } "code";
      it -> comp then SetComponentValue(it, comp) "value";
    } "comp_nursing_assessment_cardiovascular_examination_on_admission";
    g.item as it where ((linkId = 'assessment/gastrointestinal') and answer.value.exists()) -> obs.component as comp then {
      it -> comp.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem' "sys";
          it -> cg.code = 'nursing-assessment-gastrointestinal-examination-on-admission' "cd";
          it -> cg.display = 'Gastrointestinal examination on admission' "dsp";
        } "coding";
      } "code";
      it -> comp then SetComponentValue(it, comp) "value";
    } "comp_nursing_assessment_gastrointestinal_examination_on_admission";
    g.item as it where ((linkId = 'assessment/genitourinary') and answer.value.exists()) -> obs.component as comp then {
      it -> comp.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem' "sys";
          it -> cg.code = 'nursing-assessment-genitourinary-examination-on-admission' "cd";
          it -> cg.display = 'Genitourinary examination on admission' "dsp";
        } "coding";
      } "code";
      it -> comp then SetComponentValue(it, comp) "value";
    } "comp_nursing_assessment_genitourinary_examination_on_admission";
    g.item as it where (linkId = 'assessment/nurse-summary') then {
      it.answer first as a then {
        a.value as txt ->  obs.note as n,  n.text = txt "text";
      };
    } "note";
  } "nursingAssessment";
  qr.item as it where ((linkId = 'assessment/patient-description') and answer.value.exists()) ->  bundle.entry as entry,  entry.resource = create('Observation') as obs then {
    qr ->  obs,  entry then BuildObsBase(qr, obs, entry) "base";
    qr -> obs then SetSurveyCategory(qr, obs) "category";
    it -> obs.code as cc then {
      it -> cc.coding as cg then {
        it -> cg.system = 'http://loinc.org' "sys";
        it -> cg.code = '10164-2' "cd";
        it -> cg.display = 'History of Present illness Narrative' "dsp";
      } "coding";
      it -> cc.text = 'The patient\'s own description of the illness' "codeText";
    } "code";
    it.answer first as a then {
      a.value as txt ->  obs.note as n,  n.text = txt "text";
    };
  } "narr_10164_2";
  qr.item as it where ((linkId = 'assessment/present-medications') and answer.value.exists()) ->  bundle.entry as entry,  entry.resource = create('Observation') as obs then {
    qr ->  obs,  entry then BuildObsBase(qr, obs, entry) "base";
    qr -> obs then SetSurveyCategory(qr, obs) "category";
    it -> obs.code as cc then {
      it -> cc.coding as cg then {
        it -> cg.system = 'http://loinc.org' "sys";
        it -> cg.code = '10160-0' "cd";
        it -> cg.display = 'History of Medication use Narrative' "dsp";
      } "coding";
      it -> cc.text = 'Medications the patient was taking on admission' "codeText";
    } "code";
    it.answer first as a then {
      a.value as txt ->  obs.note as n,  n.text = txt "text";
    };
  } "narr_10160_0";
  // ── Chronic illness history ─────────────────────────────────────────────
  // One Condition per ticked illness, verificationStatus 'unconfirmed'. A tick
  // against a printed word on a nursing admission sheet is a reported history.
  // Recording it as confirmed would put twelve unverified diagnoses on the
  // problem list of every patient admitted.
  qr.item as pg where (linkId = 'past-history') then {
    pg.item as row where ((linkId = 'past-history/chronic') and item.where(linkId = 'past-history/chronic/illness').answer.value.exists()) ->  bundle.entry as entry,  entry.resource = create('Condition') as cond then {
      qr ->  cond,  entry then BuildConditionBase(qr, cond, entry) "base";
      row -> cond.clinicalStatus as cs then {
        row -> cs.coding as cg then {
          row -> cg.system = 'http://terminology.hl7.org/CodeSystem/condition-clinical' "sys";
          row -> cg.code = 'active' "cd";
          row -> cg.display = 'Active' "dsp";
        } "coding";
      } "clinicalStatus";
      row -> cond.verificationStatus as vs then {
        row -> vs.coding as cg then {
          row -> cg.system = 'http://terminology.hl7.org/CodeSystem/condition-ver-status' "sys";
          row -> cg.code = 'unconfirmed' "cd";
          row -> cg.display = 'Unconfirmed' "dsp";
        } "coding";
      } "verificationStatus";
      row -> cond.category as cat then {
        row -> cat.coding as cg then {
          row -> cg.system = 'http://terminology.hl7.org/CodeSystem/condition-category' "sys";
          row -> cg.code = 'problem-list-item' "cd";
          row -> cg.display = 'Problem List Item' "dsp";
        } "coding";
      } "category";
      row.item as it where (linkId = 'past-history/chronic/illness') then {
        it.answer first as a then {
          a.value as cv -> cond.code as cc then {
            cv -> cc.coding = cv "coding";
            cv.display as d -> cc.text = d "text";
          } "codeFromAnswer";
        };
      } "illnessRule";
      row.item as it where ((linkId = 'past-history/chronic/since') and answer.value.exists()) then {
        it.answer first as a then {
          a.value as txt -> cond.onset = txt "onsetString";
        };
      } "sinceRule";
      qr.authored as t -> cond.recordedDate = t "recordedDate";
      qr.encounter as e -> cond.encounter = e;
    } "chronicIllness";
  } "pastHistoryGroup";
  qr.item as g where (linkId = 'past-history') then {
    g.item as it where ((linkId = 'past-history/surgeries') and answer.value.exists()) ->  bundle.entry as entry,  entry.resource = create('Observation') as obs then {
      qr ->  obs,  entry then BuildObsBase(qr, obs, entry) "base";
      qr -> obs then SetSurveyCategory(qr, obs) "category";
      it -> obs.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'http://loinc.org' "sys";
          it -> cg.code = '11348-0' "cd";
          it -> cg.display = 'History of Past illness note' "dsp";
        } "coding";
        it -> cc.text = 'Surgeries and their dates, as reported on admission' "codeText";
      } "code";
      it.answer first as a then {
        a.value as txt ->  obs.note as n,  n.text = txt "text";
      };
    } "narr_11348_0";
    g.item as it where ((linkId = 'past-history/previous-admissions') and answer.value.exists()) ->  bundle.entry as entry,  entry.resource = create('Observation') as obs then {
      qr ->  obs,  entry then BuildObsBase(qr, obs, entry) "base";
      qr -> obs then SetSurveyCategory(qr, obs) "category";
      it -> obs.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem' "sys";
          it -> cg.code = 'nursing-assessment-previous-admissions-reported-on-admission' "cd";
          it -> cg.display = 'Previous admissions reported on admission' "dsp";
        } "coding";
      } "code";
      it.answer first as a then {
        a.value as txt ->  obs.note as n,  n.text = txt "text";
      };
    } "narr_nursing_assessment_previous_admissions_reported_on_admission";
  } "pastHistoryNarratives";
  // ── Family and social history ───────────────────────────────────────────
  qr.item as g where ((linkId = 'family-and-social') and item.answer.value.exists()) ->  bundle.entry as entry,  entry.resource = create('Observation') as obs then {
    qr ->  obs,  entry then BuildObsBase(qr, obs, entry) "base";
    qr -> obs then SetSurveyCategory(qr, obs) "category";
    g -> obs.code as cc then {
      g -> cc.coding as cg then {
        g -> cg.system = 'http://loinc.org' "sys";
        g -> cg.code = '10157-6' "cd";
        g -> cg.display = 'History of family member diseases note' "dsp";
      } "coding";
    } "code";
    g.item as it where ((linkId = 'family-and-social/alcohol') and answer.value.exists()) -> obs.component as comp then {
      it -> comp.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem' "sys";
          it -> cg.code = 'nursing-assessment-takes-alcohol' "cd";
          it -> cg.display = 'Takes alcohol' "dsp";
        } "coding";
      } "code";
      it -> comp then SetComponentCodedValue(it, comp) "value";
    } "comp_nursing_assessment_takes_alcohol";
    g.item as it where ((linkId = 'family-and-social/cigarettes') and answer.value.exists()) -> obs.component as comp then {
      it -> comp.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem' "sys";
          it -> cg.code = 'nursing-assessment-smokes-cigarettes' "cd";
          it -> cg.display = 'Smokes cigarettes' "dsp";
        } "coding";
      } "code";
      it -> comp then SetComponentCodedValue(it, comp) "value";
    } "comp_nursing_assessment_smokes_cigarettes";
    g.item as it where ((linkId = 'family-and-social/family') and answer.value.exists()) -> obs.component as comp then {
      it -> comp.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem' "sys";
          it -> cg.code = 'nursing-assessment-family-circumstances' "cd";
          it -> cg.display = 'Family circumstances' "dsp";
        } "coding";
      } "code";
      it -> comp then SetComponentValue(it, comp) "value";
    } "comp_nursing_assessment_family_circumstances";
    g.item as it where ((linkId = 'family-and-social/education') and answer.value.exists()) -> obs.component as comp then {
      it -> comp.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem' "sys";
          it -> cg.code = 'nursing-assessment-education' "cd";
          it -> cg.display = 'Education' "dsp";
        } "coding";
      } "code";
      it -> comp then SetComponentValue(it, comp) "value";
    } "comp_nursing_assessment_education";
    g.item as it where ((linkId = 'family-and-social/occupation') and answer.value.exists()) -> obs.component as comp then {
      it -> comp.code as cc then {
        it -> cc.coding as cg then {
          it -> cg.system = 'https://fhir.slade360.co.ke/fhir/CodeSystem/concept-codesystem' "sys";
          it -> cg.code = 'nursing-assessment-occupation' "cd";
          it -> cg.display = 'Occupation' "dsp";
        } "coding";
      } "code";
      it -> comp then SetComponentValue(it, comp) "value";
    } "comp_nursing_assessment_occupation";
    g.item as it where (linkId = 'family-and-social/family-medical-history') then {
      it.answer first as a then {
        a.value as txt ->  obs.note as n,  n.text = txt "text";
      };
    } "note2";
  } "familyAndSocial";
  // ── Ordered on arrival ──────────────────────────────────────────────────
  qr.item as row where ((linkId = 'investigation') and item.where(linkId = 'investigation/name').answer.value.exists()) ->  bundle.entry as entry,  entry.resource = create('ServiceRequest') as sr then {
    qr ->  sr,  entry then BuildServiceRequestBase(qr, sr, entry) "base";
    row -> sr.status = 'active' "status";
    row -> sr.intent = 'order' "intent";
    qr.encounter as e -> sr.encounter = e;
    qr.authored as t -> sr.authoredOn = t "authoredOn";
    row -> sr.category as cat then {
      row -> cat.coding as cg then {
        row -> cg.system = 'http://snomed.info/sct' "sys";
        row -> cg.code = '108252007' "cd";
        row -> cg.display = 'Laboratory procedure' "dsp";
      } "coding";
    } "category";
    row.item as it where (linkId = 'investigation/name') then {
      it.answer first as a then {
        a.value as txt ->  sr.code as cr,  cr.concept as cc,  cc.text = txt "codeText";
      };
    } "nameRule";
    qr -> sr then SetAdmissionOrderer(qr, sr) "orderer";
  } "investigationOrdered";
  qr.item as row where ((linkId = 'treatment') and item.where(linkId = 'treatment/name').answer.value.exists()) ->  bundle.entry as entry,  entry.resource = create('ServiceRequest') as sr then {
    qr ->  sr,  entry then BuildServiceRequestBase(qr, sr, entry) "base";
    row -> sr.status = 'active' "status";
    row -> sr.intent = 'order' "intent";
    qr.encounter as e -> sr.encounter = e;
    qr.authored as t -> sr.authoredOn = t "authoredOn";
    row.item as it where (linkId = 'treatment/name') then {
      it.answer first as a then {
        a.value as txt ->  sr.code as cr,  cr.concept as cc,  cc.text = txt "codeText";
      };
    } "nameRule";
    qr -> sr then SetAdmissionOrderer(qr, sr) "orderer";
  } "treatmentOrdered";
}

group SetAdmissionOrderer(source qr : QR, target sr : ServiceRequest) {
  qr.item as g where (linkId = 'sign-off') then {
    g.item as it where ((linkId = 'sign-off/physician') and answer.value.exists()) then {
      it.answer first as a then {
        a.value as ref -> sr.requester = ref "requester";
      };
    } "physicianRule";
  } "signOffGroup";
}

// ─────────────────────────────────────────────────────────────────────────────
// Shared scaffolding. Repeated in each map rather than imported: the transform
// engine resolves `imports` against whatever StructureMaps happen to be on the
// server, and a form that stops extracting because a shared map was not
// uploaded is a worse failure than a duplicated group.
// Two notes on what the publisher's StructureMap validator says about this file.
// Source parameters below are left untyped on purpose. Writing `: Element` makes
// the validator compare QuestionnaireResponse.item against the literal 'Element'
// and report them as incompatible with each other, which is noise, not a finding.
// `evaluate(context, expression)` is flagged as "takes 1 parameter but 2 were
// found". Leave it. The FML parser in the same jar refuses the one-argument form
// outright -- it wants a parameter, a comma, then the FHIRPath -- so the
// publisher is objecting to what its own compiler emits. Two arguments is the
// only form that compiles, and it is what the R5 transform engine executes.
// ─────────────────────────────────────────────────────────────────────────────
group BuildObsBase(source qr : QR, target obs : Observation, target entry) {
  qr -> obs then SetResponseProvenance(qr, obs) "responseProvenance";
  qr -> obs.id = uuid() then SetObservationFullUrl(obs, entry) "idAndFullUrl";
  qr -> entry.request as request then {
    qr -> request.method = 'POST' "requestMethod";
    qr -> request.url = 'Observation' "requestUrl";
  } "entryRequest";
  qr -> obs.status = 'final' "status";
  qr.subject as s -> obs.subject = s;
  qr.encounter as e -> obs.encounter = e;
  qr.authored as t -> obs.effective = t "effective";
  qr.source as src -> obs.performer = src "performerFromSource";
  qr where (source.exists().not()) then {
    qr.author as a -> obs.performer = a "performerFromAuthor";
  } "performerFallback";
  // A stored response's id already reads "QuestionnaireResponse/<id>", so it is
  // a relative reference as it stands. The hasValue() guard is what stops an
  // unsaved response -- one posted straight to $extract rather than saved first
  // -- from writing "QuestionnaireResponse/null" into the record.
  qr.id as qid where ($this.hasValue()) ->  obs.derivedFrom = create('Reference') as ref,  ref.reference = qid "linkQuestionnaireResponse";
}

group SetObservationFullUrl(source obs : Observation, target entry) {
  obs.id as id -> entry.fullUrl = append('https://fhir.slade360.co.ke/fhir/Observation/', id) "assignFullUrl";
}

// The two answer shapes a component can carry. Both read the answer through the
// untyped `.value` accessor: reading `valueCoding` or `valueQuantity` by its
// typed property name fails at transform time on HAPI R5 with "Attempt to read
// invalid property ... on QuestionnaireResponse.item.answer".
group SetComponentCodedValue(source it, target comp) {
  it.answer first as a then {
    a.value as cv -> comp.value = create('CodeableConcept') as cc then {
      cv -> cc.coding = cv "valueCoding";
      cv.display as d -> cc.text = d "valueText";
    } "codedValue";
  } "componentAnswer";
}

group SetComponentValue(source it, target comp) {
  it.answer first as a then {
    a.value as v -> comp.value = v "plainValue";
  } "componentAnswer";
}

group SetSurveyCategory(source qr : QR, target obs : Observation) {
  qr -> obs.category as cat then {
    qr -> cat.coding as coding then {
      qr -> coding.system = 'http://terminology.hl7.org/CodeSystem/observation-category' "categorySystem";
      qr -> coding.code = 'survey' "categoryCode";
      qr -> coding.display = 'Survey' "categoryDisplay";
    } "categoryCoding";
  } "category";
}

group SetVitalSignsCategory(source qr : QR, target obs : Observation) {
  qr -> obs.category as cat then {
    qr -> cat.coding as coding then {
      qr -> coding.system = 'http://terminology.hl7.org/CodeSystem/observation-category' "categorySystem";
      qr -> coding.code = 'vital-signs' "categoryCode";
      qr -> coding.display = 'Vital Signs' "categoryDisplay";
    } "categoryCoding";
  } "category";
}

group SetExamCategory(source qr : QR, target obs : Observation) {
  qr -> obs.category as cat then {
    qr -> cat.coding as coding then {
      qr -> coding.system = 'http://terminology.hl7.org/CodeSystem/observation-category' "categorySystem";
      qr -> coding.code = 'exam' "categoryCode";
      qr -> coding.display = 'Exam' "categoryDisplay";
    } "categoryCoding";
  } "category";
}

group BuildConditionBase(source qr : QR, target r : Condition, target entry) {
  qr -> r then SetResponseProvenance(qr, r) "responseProvenance";
  qr -> r.id = uuid() then SetConditionFullUrl(r, entry) "idAndFullUrl";
  qr -> entry.request as request then {
    qr -> request.method = 'POST' "requestMethod";
    qr -> request.url = 'Condition' "requestUrl";
  } "entryRequest";
  qr.subject as s -> r.subject = s;
}

group SetConditionFullUrl(source r : Condition, target entry) {
  r.id as id -> entry.fullUrl = append('https://fhir.slade360.co.ke/fhir/Condition/', id) "assignFullUrl";
}

group BuildServiceRequestBase(source qr : QR, target r : ServiceRequest, target entry) {
  qr -> r then SetResponseProvenance(qr, r) "responseProvenance";
  qr -> r.id = uuid() then SetServiceRequestFullUrl(r, entry) "idAndFullUrl";
  qr -> entry.request as request then {
    qr -> request.method = 'POST' "requestMethod";
    qr -> request.url = 'ServiceRequest' "requestUrl";
  } "entryRequest";
  qr.subject as s -> r.subject = s;
}

group SetServiceRequestFullUrl(source r : ServiceRequest, target entry) {
  r.id as id -> entry.fullUrl = append('https://fhir.slade360.co.ke/fhir/ServiceRequest/', id) "assignFullUrl";
}

// ── Provenance and tenancy ───────────────────────────────────────────────────
// Two tags on everything this map creates. The first says which questionnaire
// produced it, so a form that reopens mid-admission recalls its own earlier
// entries and not another form's. The second copies the tenant tags off the
// response, because nothing else on the path puts them there.
// The code is the form's document type, not the questionnaire's id: every
// environment mints its own ids, and there is only one set of maps, so an id
// here would tie the tag to whichever environment stamped it.
// ── Provenance and tenancy ───────────────────────────────────────────────────
group SetResponseProvenance(source qr : QR, target r) {
  qr -> r.meta as m then {
    qr -> m.tag as t then {
      qr -> t.system = 'http://slade360edi.com/questionnaire-provenance' "provenanceSystem";
      qr -> t.code = 'admission-form' "provenanceCode";
      qr -> t.display = 'Admission form' "provenanceDisplay";
    } "provenanceTag";
  } "provenanceMeta";
  // Every tag the response carries, onto the resource. `meta` is 0..1, so this
  // lands on the same meta the rule above created rather than a second one.
  qr.meta as qm then {
    qm.tag as qt ->  r.meta as m,  m.tag = qt "copyTag";
  } "tenantTags";
}