<StructureDefinition xmlns="http://hl7.org/fhir">
  <id value="ie-core-bundle-eprescription" />
  <url value="https://hl7-ie.github.io/ie-core/fhir/ie/core/StructureDefinition/ie-core-bundle-eprescription" />
  <name value="IECoreBundleEPrescription" />
  <title value="IE Core Bundle (ePrescription)" />
  <status value="draft" />
  <description value="An Irish electronic prescription as exchanged: one patient (IECorePatientEPrescription), one or more prescription items (IECoreMedicationRequestEPrescription) sharing a group identifier, the prescriber, the prescriber's facility, the medicinal products, and the allergy statement (IECoreListAllergiesAtPrescribing). Prescription-level legal and safety rules (HIQA EP 1.4.2, 1.6.1, 2.10.1, 3.1) are enforced here because they span resources." />
  <fhirVersion value="4.0.1" />
  <kind value="resource" />
  <abstract value="false" />
  <type value="Bundle" />
  <baseDefinition value="http://hl7.org/fhir/StructureDefinition/Bundle" />
  <derivation value="constraint" />
  <differential>
    <element id="Bundle">
      <path value="Bundle" />
      <constraint>
        <key value="ie-bnd-rx-1" />
        <severity value="error" />
        <human value="A multi-item prescription SHALL share one group identifier (system and value) across all its items (HIQA EP 3.1)" />
        <expression value="entry.resource.ofType(MedicationRequest).count() &lt;= 1 or (entry.resource.ofType(MedicationRequest).all(groupIdentifier.exists()) and entry.resource.ofType(MedicationRequest).select(groupIdentifier.system + '|' + groupIdentifier.value).distinct().count() = 1)" />
        <source value="https://hl7-ie.github.io/ie-core/fhir/ie/core/StructureDefinition/ie-core-bundle-eprescription" />
      </constraint>
      <constraint>
        <key value="ie-bnd-rx-2" />
        <severity value="error" />
        <human value="Every prescription item SHALL reference the allergy statement (a List coded LOINC 48765-2) in supportingInformation (HIQA EP 1.6.1 / 1.6.2)" />
        <expression value="entry.resource.ofType(MedicationRequest).all(supportingInformation.where(resolve() is List and resolve().code.coding.where(system = 'http://loinc.org' and code = '48765-2').exists()).exists())" />
        <source value="https://hl7-ie.github.io/ie-core/fhir/ie/core/StructureDefinition/ie-core-bundle-eprescription" />
      </constraint>
      <constraint>
        <key value="ie-bnd-rx-3" />
        <severity value="error" />
        <human value="If the patient is under 12 years old at the date of prescribing, or the date of birth is not a full date, every prescription item SHALL record the patient's age (HIQA EP 1.4.2; a legal requirement)" />
        <expression value="entry.resource.ofType(MedicationRequest).all(extension('https://hl7-ie.github.io/ie-core/fhir/ie/core/StructureDefinition/ie-core-patient-age-at-prescribing').exists() or ((%resource.entry.resource.ofType(Patient).first().birthDate.toString().length() = 10) and ((%resource.entry.resource.ofType(Patient).first().birthDate + 12 years).toString() &lt;= authoredOn.toString().substring(0,10))))" />
        <source value="https://hl7-ie.github.io/ie-core/fhir/ie/core/StructureDefinition/ie-core-bundle-eprescription" />
      </constraint>
      <constraint>
        <key value="ie-bnd-rx-4" />
        <severity value="error" />
        <human value="The prescriber (the requester, its practitioner, or its organisation) SHALL have a telephone number (HIQA EP 2.10 / 2.10.1, Mandatory)" />
        <expression value="entry.resource.ofType(MedicationRequest).all(requester.resolve().telecom.where(system = 'phone').exists() or requester.resolve().ofType(PractitionerRole).practitioner.resolve().telecom.where(system = 'phone').exists() or requester.resolve().ofType(PractitionerRole).organization.resolve().telecom.where(system = 'phone').exists())" />
        <source value="https://hl7-ie.github.io/ie-core/fhir/ie/core/StructureDefinition/ie-core-bundle-eprescription" />
      </constraint>
      <constraint>
        <key value="ie-bnd-rx-5" />
        <severity value="error" />
        <human value="Every prescription item, the allergy statement and every allergy SHALL be about the one Patient in the Bundle (patient safety: the allergy statement must be the patient's own)" />
        <expression value="(entry.resource.ofType(MedicationRequest).subject.reference | entry.resource.ofType(List).subject.reference | entry.resource.ofType(AllergyIntolerance).patient.reference).all(($this = %resource.entry.where(resource is Patient).fullUrl) or ($this = ('Patient/' + %resource.entry.resource.ofType(Patient).id)))" />
        <source value="https://hl7-ie.github.io/ie-core/fhir/ie/core/StructureDefinition/ie-core-bundle-eprescription" />
      </constraint>
      <constraint>
        <key value="ie-bnd-rx-6" />
        <severity value="error" />
        <human value="Every allergy listed in the allergy statement SHALL be included in the Bundle (HIQA EP 1.6.2)" />
        <expression value="entry.resource.ofType(List).entry.item.reference.all(($this in %resource.entry.fullUrl) or ($this.replace('AllergyIntolerance/', '') in %resource.entry.resource.ofType(AllergyIntolerance).id))" />
        <source value="https://hl7-ie.github.io/ie-core/fhir/ie/core/StructureDefinition/ie-core-bundle-eprescription" />
      </constraint>
    </element>
    <element id="Bundle.identifier">
      <path value="Bundle.identifier" />
      <comment value="Bundle identifier; typically the NePS electronic prescription (group) identifier (HIQA EP 3.1)." />
      <min value="1" />
      <mustSupport value="true" />
    </element>
    <element id="Bundle.type">
      <path value="Bundle.type" />
      <patternCode value="collection" />
    </element>
    <element id="Bundle.timestamp">
      <path value="Bundle.timestamp" />
      <min value="1" />
      <mustSupport value="true" />
    </element>
    <element id="Bundle.entry">
      <path value="Bundle.entry" />
      <slicing>
        <discriminator>
          <type value="type" />
          <path value="resource" />
        </discriminator>
        <rules value="open" />
      </slicing>
      <min value="3" />
      <mustSupport value="true" />
    </element>
    <element id="Bundle.entry.fullUrl">
      <path value="Bundle.entry.fullUrl" />
      <min value="1" />
    </element>
    <element id="Bundle.entry.resource">
      <path value="Bundle.entry.resource" />
      <min value="1" />
    </element>
    <element id="Bundle.entry:patient">
      <path value="Bundle.entry" />
      <sliceName value="patient" />
      <min value="1" />
      <max value="1" />
      <mustSupport value="true" />
    </element>
    <element id="Bundle.entry:patient.resource">
      <path value="Bundle.entry.resource" />
      <type>
        <code value="Patient" />
        <profile value="https://hl7-ie.github.io/ie-core/fhir/ie/core/StructureDefinition/ie-core-patient-eprescription" />
      </type>
    </element>
    <element id="Bundle.entry:prescriptionItem">
      <path value="Bundle.entry" />
      <sliceName value="prescriptionItem" />
      <min value="1" />
      <max value="*" />
      <mustSupport value="true" />
    </element>
    <element id="Bundle.entry:prescriptionItem.resource">
      <path value="Bundle.entry.resource" />
      <type>
        <code value="MedicationRequest" />
        <profile value="https://hl7-ie.github.io/ie-core/fhir/ie/core/StructureDefinition/ie-core-medicationrequest-eprescription" />
      </type>
    </element>
    <element id="Bundle.entry:allergyStatement">
      <path value="Bundle.entry" />
      <sliceName value="allergyStatement" />
      <comment value="HIQA EP 1.6.1 / 1.6.2: exactly one allergy statement per prescription, either listing the allergies or giving the reason none are recorded." />
      <min value="1" />
      <max value="1" />
      <mustSupport value="true" />
    </element>
    <element id="Bundle.entry:allergyStatement.resource">
      <path value="Bundle.entry.resource" />
      <type>
        <code value="List" />
        <profile value="https://hl7-ie.github.io/ie-core/fhir/ie/core/StructureDefinition/ie-core-list-allergies-at-prescribing" />
      </type>
    </element>
    <element id="Bundle.entry:allergy">
      <path value="Bundle.entry" />
      <sliceName value="allergy" />
      <min value="0" />
      <max value="*" />
      <mustSupport value="true" />
    </element>
    <element id="Bundle.entry:allergy.resource">
      <path value="Bundle.entry.resource" />
      <type>
        <code value="AllergyIntolerance" />
        <profile value="https://hl7-ie.github.io/ie-core/fhir/ie/core/StructureDefinition/ie-core-allergyintolerance" />
      </type>
    </element>
    <element id="Bundle.entry:practitioner">
      <path value="Bundle.entry" />
      <sliceName value="practitioner" />
      <min value="0" />
      <max value="*" />
      <mustSupport value="true" />
    </element>
    <element id="Bundle.entry:practitioner.resource">
      <path value="Bundle.entry.resource" />
      <type>
        <code value="Practitioner" />
        <profile value="https://hl7-ie.github.io/ie-core/fhir/ie/core/StructureDefinition/ie-core-practitioner" />
      </type>
    </element>
    <element id="Bundle.entry:practitionerRole">
      <path value="Bundle.entry" />
      <sliceName value="practitionerRole" />
      <min value="0" />
      <max value="*" />
      <mustSupport value="true" />
    </element>
    <element id="Bundle.entry:practitionerRole.resource">
      <path value="Bundle.entry.resource" />
      <type>
        <code value="PractitionerRole" />
        <profile value="https://hl7-ie.github.io/ie-core/fhir/ie/core/StructureDefinition/ie-core-practitionerrole" />
      </type>
    </element>
    <element id="Bundle.entry:organization">
      <path value="Bundle.entry" />
      <sliceName value="organization" />
      <min value="0" />
      <max value="*" />
      <mustSupport value="true" />
    </element>
    <element id="Bundle.entry:organization.resource">
      <path value="Bundle.entry.resource" />
      <type>
        <code value="Organization" />
        <profile value="https://hl7-ie.github.io/ie-core/fhir/ie/core/StructureDefinition/ie-core-organization" />
      </type>
    </element>
    <element id="Bundle.entry:medication">
      <path value="Bundle.entry" />
      <sliceName value="medication" />
      <min value="0" />
      <max value="*" />
      <mustSupport value="true" />
    </element>
    <element id="Bundle.entry:medication.resource">
      <path value="Bundle.entry.resource" />
      <type>
        <code value="Medication" />
        <profile value="https://hl7-ie.github.io/ie-core/fhir/ie/core/StructureDefinition/ie-core-medication-eprescription" />
      </type>
    </element>
    <element id="Bundle.entry:signature">
      <path value="Bundle.entry" />
      <sliceName value="signature" />
      <min value="0" />
      <max value="*" />
      <mustSupport value="true" />
    </element>
    <element id="Bundle.entry:signature.resource">
      <path value="Bundle.entry.resource" />
      <type>
        <code value="Provenance" />
        <profile value="https://hl7-ie.github.io/ie-core/fhir/ie/core/StructureDefinition/ie-core-provenance-eprescription-signature" />
      </type>
    </element>
  </differential>
</StructureDefinition>