<StructureDefinition xmlns="http://hl7.org/fhir" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://hl7.org/fhir ../../input-cache/schemas/R5/fhir-single.xsd">
  <id value="profile-Reference-group-or-patient" />
  <url value="https://bpcs.exchange/fhir/StructureDefinition/profile-Reference-group-or-patient" />
  <name value="ReferenceGroupOrPatient" />
  <title value="Group or Patient Reference" />
  <status value="active" />
  <experimental value="false" />
  <description value="A reference to a Patient that may need to occasionally point to a Group" />
  <fhirVersion value="5.0.0" />
  <mapping>
    <identity value="BPCS" />
    <name value="Internal BPCS API" />
  </mapping>
  <kind value="complex-type" />
  <abstract value="false" />
  <type value="Reference" />
  <baseDefinition value="http://hl7.org/fhir/StructureDefinition/Reference" />
  <derivation value="constraint" />
  <differential>
    <element id="Reference">
      <path value="Reference" />
      <constraint>
        <key value="bpcs-reference-1" />
        <severity value="error" />
        <human value="Must have either a reference or a subject extension, but not both" />
        <expression value="reference.exists() != extension('https://bpcs.exchange/fhir/StructureDefinition/ext-Resource-Subject').exists()" />
      </constraint>
      <constraint>
        <key value="bpcs-reference-2" />
        <severity value="error" />
        <human value="If reference is not present, must have display" />
        <expression value="reference.exists().not() implies display.exists()" />
      </constraint>
    </element>
    <element id="Reference.extension:family">
      <path value="Reference.extension" />
      <sliceName value="family" />
      <type>
        <code value="Extension" />
        <profile value="https://bpcs.exchange/fhir/StructureDefinition/ext-Resource-Subject" />
      </type>
      <condition value="bpcs-reference-1" />
      <mustSupport value="true" />
      <mapping>
        <identity value="BPCS" />
        <map value="BatchRecordDTO.applicationNum (in Group reference), BatchInfoDTO.applicationNum (in Group reference)" />
      </mapping>
    </element>
    <element id="Reference.reference">
      <path value="Reference.reference" />
      <condition value="bpcs-reference-1" />
      <mustSupport value="true" />
      <mapping>
        <identity value="BPCS" />
        <map value="ReceiptDTO.odbNum, BatchRecordDTO.odbNum, BatchInfoDTO.odbNum" />
      </mapping>
    </element>
    <element id="Reference.display">
      <path value="Reference.display" />
      <comment value="If there is no reference (because there's an extension), this conveys the id of the group being referenced to satisfy best-practice" />
      <condition value="bpcs-reference-2" />
      <mustSupport value="true" />
    </element>
  </differential>
</StructureDefinition>