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