<StructureDefinition xmlns="http://hl7.org/fhir">
  <id value="BundleABeReC" />
  <url value="https://www.alberta.ca/fhir/aberec/StructureDefinition/BundleABeReC" />
  <version value="1.1.0" />
  <name value="BundleABeReC" />
  <title value="Bundle (AB:eReC)" />
  <status value="draft" />
  <description value="This profile defines a set of constraints to the FHIR Bundle resource for use in Alberta eReferral and eConsult (AB:eReC). It re-profiles the CA-eReC Bundle profile and it includes constraints applied to the Bundle resource by the AB:eReC project to include hard constraints (invariants) than enforce what sections and resource elements are allowed to be present in an Alberta eReferral and/or eConsult. These have been applied at the Bundle level as an alternative to forbidding elements through 0..0 cardinality - to balance the need for mechanical validation on eReferral and/or eConsult against the re-use of clinical FHIR profiles under different contexts." />
  <fhirVersion value="4.0.1" />
  <kind value="resource" />
  <abstract value="false" />
  <type value="Bundle" />
  <baseDefinition value="http://fhir.infoway-inforoute.ca/io/CA-eReC/StructureDefinition/CA-eReC-Bundle" />
  <derivation value="constraint" />
  <differential>
    <element id="Bundle">
      <path value="Bundle" />
      <constraint>
        <key value="aberec-bdl-1" />
        <severity value="error" />
        <human value="If Bundle.meta.extension.extension.valueString = 'AB:eReC', then the following details SHALL NOT be present in all resources within the bundle instance except the bundle itself: contained" />
        <expression value="Bundle.meta.extension.extension.where(valueString = 'AB:eReC').exists() implies &#xA;Bundle.entry.resource.where(contained.exists()).exists().not()" />
        <source value="https://www.alberta.ca/fhir/aberec/StructureDefinition/BundleABeReC" />
      </constraint>
      <constraint>
        <key value="aberec-bdl-app-1" />
        <severity value="error" />
        <human value="If Bundle.meta.extension.extension.valueString = 'AB:eReC', then the following Appointment details SHALL NOT be present: reasonReference, supportingInformation, slot" />
        <expression value="Bundle.meta.extension.extension.where(valueString = 'AB:eReC').exists() implies &#xA;Bundle.entry.where(resource is Appointment).resource.where(reasonReference.exists()).exists().not() and &#xA;Bundle.entry.where(resource is Appointment).resource.where(supportingInformation.exists()).exists().not() and &#xA;Bundle.entry.where(resource is Appointment).resource.where(slot.exists()).exists().not()" />
        <source value="https://www.alberta.ca/fhir/aberec/StructureDefinition/BundleABeReC" />
      </constraint>
      <constraint>
        <key value="aberec-bdl-com-1" />
        <severity value="error" />
        <human value="If Bundle.meta.extension.extension.valueString = 'AB:eReC', then the following Communication details SHALL NOT be present: partOf, about, encounter, reasonReference." />
        <expression value="Bundle.meta.extension.extension.where(valueString = 'AB:eReC').exists() implies &#xA;Bundle.entry.where(resource is Communication).resource.where(partOf.exists()).exists().not() and&#xA;Bundle.entry.where(resource is Communication).resource.where(about.exists()).exists().not() and&#xA;Bundle.entry.where(resource is Communication).resource.where(encounter.exists()).exists().not() and &#xA;Bundle.entry.where(resource is Communication).resource.where(reasonReference.exists()).exists().not()" />
        <source value="https://www.alberta.ca/fhir/aberec/StructureDefinition/BundleABeReC" />
      </constraint>
      <constraint>
        <key value="aberec-bdl-dcr-1" />
        <severity value="error" />
        <human value="If Bundle.meta.extension.extension.valueString = 'AB:eReC', then the following DocumentReference details SHALL NOT be present: authenticator, custodian, relatesTo. context.encounter" />
        <expression value="Bundle.meta.extension.extension.where(valueString = 'AB:eReC').exists() implies &#xA;Bundle.entry.where(resource is DocumentReference).resource.where(authenticator.exists()).exists().not() and &#xA;Bundle.entry.where(resource is DocumentReference).resource.where(custodian.exists()).exists().not() and &#xA;Bundle.entry.where(resource is DocumentReference).resource.where(relatesTo.exists()).exists().not() and &#xA;Bundle.entry.where(resource is DocumentReference).resource.where(context.encounter.exists()).exists().not()" />
        <source value="https://www.alberta.ca/fhir/aberec/StructureDefinition/BundleABeReC" />
      </constraint>
      <constraint>
        <key value="aberec-bdl-hcs-1" />
        <severity value="error" />
        <human value="If Bundle.meta.extension.extension.valueString = 'AB:eReC', then the following HealthcareService details SHALL NOT be present: photo" />
        <expression value="Bundle.meta.extension.extension.where(valueString = 'AB:eReC').exists() implies &#xA;Bundle.entry.where(resource is HealthcareService).resource.where(photo.exists()).exists().not()" />
        <source value="https://www.alberta.ca/fhir/aberec/StructureDefinition/BundleABeReC" />
      </constraint>
      <constraint>
        <key value="aberec-bdl-loc-1" />
        <severity value="error" />
        <human value="If Bundle.meta.extension.extension.valueString = 'AB:eReC', then the following Location details SHALL NOT be present: telecom.use, and address.use, managingOrganization" />
        <expression value="Bundle.meta.extension.extension.where(valueString = 'AB:eReC').exists() implies &#xA;Bundle.entry.where(resource is Location).resource.where(telecom.use.exists()).exists().not() and &#xA;Bundle.entry.where(resource is Location).resource.where(address.use.exists()).exists().not() and &#xA;Bundle.entry.where(resource is Location).resource.where(managingOrganization.exists()).exists().not()" />
        <source value="https://www.alberta.ca/fhir/aberec/StructureDefinition/BundleABeReC" />
      </constraint>
      <constraint>
        <key value="aberec-bdl-pat-1" />
        <severity value="error" />
        <human value="If Bundle.meta.extension.extension.valueString = 'AB:eReC', then the following Patient details SHALL NOT be present: text, active, address.text, maritalStatus, multipleBirth, photo,  contact.gender, contact.organization, contact.period, generalPractitioner, managingOrganization" />
        <expression value="Bundle.meta.extension.extension.where(valueString = 'AB:eReC').exists() implies &#xA;Bundle.entry.where(resource is Patient).resource.where(text.exists()).exists().not() and &#xA;Bundle.entry.where(resource is Patient).resource.where(active.exists()).exists().not() and &#xA;Bundle.entry.where(resource is Patient).resource.where(address.text.exists()).exists().not() and &#xA;Bundle.entry.where(resource is Patient).resource.where(maritalStatus.exists()).exists().not() and &#xA;Bundle.entry.where(resource is Patient).resource.where(multipleBirth.exists()).exists().not() and &#xA;Bundle.entry.where(resource is Patient).resource.where(photo.exists()).exists().not() and &#xA;Bundle.entry.where(resource is Patient).resource.where(contact.gender.exists()).exists().not() and &#xA;Bundle.entry.where(resource is Patient).resource.where(contact.organization.exists()).exists().not() and &#xA;Bundle.entry.where(resource is Patient).resource.where(contact.period.exists()).exists().not() and &#xA;Bundle.entry.where(resource is Patient).resource.where(generalPractitioner.exists()).exists().not() and &#xA;Bundle.entry.where(resource is Patient).resource.where(managingOrganization.exists()).exists().not()" />
        <source value="https://www.alberta.ca/fhir/aberec/StructureDefinition/BundleABeReC" />
      </constraint>
      <constraint>
        <key value="aberec-bdl-pat-2" />
        <severity value="error" />
        <human value="If Bundle.meta.extension.extension.valueString = 'AB:eReC', then the following Patient details SHALL be present: telecom:Phone" />
        <expression value="Bundle.meta.extension.extension.where(valueString = 'AB:eReC').exists() implies &#xA;Bundle.entry.resource.ofType(Patient).all(telecom.where(system = 'phone').exists())" />
        <source value="https://www.alberta.ca/fhir/aberec/StructureDefinition/BundleABeReC" />
      </constraint>
      <constraint>
        <key value="aberec-bdl-prc-1" />
        <severity value="error" />
        <human value="If Bundle.meta.extension.extension.valueString = 'AB:eReC', then the following Practitioner details SHALL NOT be present: photo, qualification, birthDate" />
        <expression value="Bundle.meta.extension.extension.where(valueString = 'AB:eReC').exists() implies &#xA;Bundle.entry.where(resource is Practitioner).resource.where(photo.exists()).exists().not() and &#xA;Bundle.entry.where(resource is Practitioner).resource.where(qualification.exists()).exists().not() and &#xA;Bundle.entry.where(resource is Practitioner).resource.where(birthDate.exists()).exists().not()" />
        <source value="https://www.alberta.ca/fhir/aberec/StructureDefinition/BundleABeReC" />
      </constraint>
      <constraint>
        <key value="aberec-bdl-prl-1" />
        <severity value="error" />
        <human value="If Bundle.meta.extension.extension.valueString = 'AB:eReC', then the following PractitionerRole details SHALL NOT be present: healtcareService" />
        <expression value="Bundle.meta.extension.extension.where(valueString = 'AB:eReC').exists() implies &#xA;Bundle.entry.where(resource is PractitionerRole).resource.where(healthcareService.exists()).exists().not()" />
        <source value="https://www.alberta.ca/fhir/aberec/StructureDefinition/BundleABeReC" />
      </constraint>
      <constraint>
        <key value="aberec-bdl-prl-2" />
        <severity value="error" />
        <human value="If Bundle.meta.extension.extension.valueString = 'AB:eReC', then the following PractitionerRole details SHALL be present: specialty.coding.code" />
        <expression value="Bundle.meta.extension.extension.where(valueString = 'AB:eReC').exists() implies &#xA;Bundle.entry.where(resource is PractitionerRole).resource.where(specialty.coding.code.exists()).exists()" />
        <source value="https://www.alberta.ca/fhir/aberec/StructureDefinition/BundleABeReC" />
      </constraint>
      <constraint>
        <key value="aberec-bdl-srq-1" />
        <severity value="error" />
        <human value="If Bundle.meta.extension.extension.valueString = 'AB:eReC', then the following ServiceRequest details SHALL NOT be present: replaces, encounter, reasonReference, insurance, specimen, relevantHistory" />
        <expression value="Bundle.meta.extension.extension.where(valueString = 'AB:eReC').exists() implies &#xA;Bundle.entry.where(resource is ServiceRequest).resource.where(encounter.exists()).exists().not() and &#xA;Bundle.entry.where(resource is ServiceRequest).resource.where(reasonReference.exists()).exists().not() and &#xA;Bundle.entry.where(resource is ServiceRequest).resource.where(insurance.exists()).exists().not() and &#xA;Bundle.entry.where(resource is ServiceRequest).resource.where(specimen.exists()).exists().not() and &#xA;Bundle.entry.where(resource is ServiceRequest).resource.where(relevantHistory.exists()).exists().not()" />
        <source value="https://www.alberta.ca/fhir/aberec/StructureDefinition/BundleABeReC" />
      </constraint>
      <constraint>
        <key value="aberec-bdl-tsk-1" />
        <severity value="error" />
        <human value="If Bundle.meta.extension.extension.valueString = 'AB:eReC', then the following Task details SHALL NOT be present: for, encounter, insurance, relevantHistory, restriction.recipient, input" />
        <expression value="Bundle.meta.extension.extension.where(valueString = 'AB:eReC').exists() implies &#xA;Bundle.entry.where(resource is Task).resource.where(for.exists()).exists().not() and &#xA;Bundle.entry.where(resource is Task).resource.where(encounter.exists()).exists().not() and &#xA;Bundle.entry.where(resource is Task).resource.where(insurance.exists()).exists().not() and &#xA;Bundle.entry.where(resource is Task).resource.where(relevantHistory.exists()).exists().not() and &#xA;Bundle.entry.where(resource is Task).resource.where(restriction.recipient.exists()).exists().not() and &#xA;Bundle.entry.where(resource is Task).resource.where(input.exists()).exists().not()" />
        <source value="https://www.alberta.ca/fhir/aberec/StructureDefinition/BundleABeReC" />
      </constraint>
    </element>
    <element id="Bundle.meta.extension">
      <path value="Bundle.meta.extension" />
      <min value="1" />
    </element>
    <element id="Bundle.meta.extension:submissionMetadata">
      <path value="Bundle.meta.extension" />
      <sliceName value="submissionMetadata" />
      <min value="1" />
      <max value="*" />
      <type>
        <code value="Extension" />
        <profile value="https://www.alberta.ca/fhir/psab/StructureDefinition/ext-EntityIdentifierAndName" />
      </type>
      <mustSupport value="true" />
    </element>
    <element id="Bundle.meta.security">
      <path value="Bundle.meta.security" />
      <mustSupport value="true" />
      <binding>
        <strength value="extensible" />
        <valueSet value="https://www.alberta.ca/fhir/ValueSet/security-label" />
      </binding>
    </element>
    <element id="Bundle.identifier">
      <path value="Bundle.identifier" />
      <min value="1" />
    </element>
    <element id="Bundle.entry:Message">
      <path value="Bundle.entry" />
      <sliceName value="Message" />
    </element>
    <element id="Bundle.entry:Message.resource">
      <path value="Bundle.entry.resource" />
      <type>
        <code value="Resource" />
        <profile value="https://www.alberta.ca/fhir/aberec/StructureDefinition/MessageHeaderABeReC" />
      </type>
    </element>
    <element id="Bundle.entry:Focus">
      <path value="Bundle.entry" />
      <sliceName value="Focus" />
    </element>
    <element id="Bundle.entry:Focus.resource">
      <path value="Bundle.entry.resource" />
      <type>
        <code value="Resource" />
        <profile value="https://www.alberta.ca/fhir/aberec/StructureDefinition/AppointmentABeReC" />
        <profile value="https://www.alberta.ca/fhir/aberec/StructureDefinition/CommunicationABeReC" />
        <profile value="https://www.alberta.ca/fhir/aberec/StructureDefinition/ServiceRequestABeReC" />
        <profile value="https://www.alberta.ca/fhir/aberec/StructureDefinition/TaskABeReC" />
      </type>
    </element>
  </differential>
</StructureDefinition>