<StructureDefinition xmlns="http://hl7.org/fhir">
  <id value="pcm-consent" />
  <url value="http://pcm.fhir.health.gov.il/StructureDefinition/pcm-consent" />
  <version value="0.1.6" />
  <name value="PCMConsent" />
  <title value="PCM Consent" />
  <status value="active" />
  <description value="PCM consent resource. Created by a Service Provider Organization with status 'proposed'. Status is later controlled by the patient (active/rejected) and by PCM on expiration (inactive). Service providers may set status to 'inactive' only for consents they created; deletion is not allowed. Search/get is limited to PCM administrators and the requesting provider." />
  <fhirVersion value="4.0.1" />
  <kind value="resource" />
  <abstract value="false" />
  <type value="Consent" />
  <baseDefinition value="http://fhir.health.gov.il/StructureDefinition/il-core-consent" />
  <derivation value="constraint" />
  <differential>
    <element id="Consent.extension">
      <path value="Consent.extension" />
      <slicing>
        <discriminator>
          <type value="value" />
          <path value="url" />
        </discriminator>
        <ordered value="false" />
        <rules value="open" />
      </slicing>
      <min value="1" />
    </element>
    <element id="Consent.extension:pcmService">
      <path value="Consent.extension" />
      <sliceName value="pcmService" />
      <comment value="Set on creation. Reference to the HealthcareService instance that the provider is requesting consent for." />
      <min value="1" />
      <max value="1" />
      <type>
        <code value="Extension" />
        <profile value="http://pcm.fhir.health.gov.il/StructureDefinition/ext-pcm-service" />
      </type>
    </element>
    <element id="Consent.identifier">
      <path value="Consent.identifier" />
      <comment value="Stable business identifier for the consent (do not rely on resource.id for business workflows)." />
    </element>
    <element id="Consent.identifier.use">
      <path value="Consent.identifier.use" />
      <min value="1" />
      <fixedCode value="official" />
    </element>
    <element id="Consent.identifier.system">
      <path value="Consent.identifier.system" />
      <min value="1" />
      <fixedUri value="http://pcm.fhir.health.gov.il/identifier/pcm-consent-id" />
    </element>
    <element id="Consent.identifier.value">
      <path value="Consent.identifier.value" />
      <min value="1" />
    </element>
    <element id="Consent.status">
      <path value="Consent.status" />
      <comment value="Created by provider as 'proposed'. Patient can change to 'active' or 'rejected'. PCM sets to 'inactive' after expiration. Service providers may set to 'inactive' only for consents they created; deletion is not allowed." />
      <binding>
        <strength value="required" />
        <valueSet value="http://pcm.fhir.health.gov.il/ValueSet/pcm-consent-status" />
      </binding>
    </element>
    <element id="Consent.scope">
      <path value="Consent.scope" />
      <comment value="PCM consent is a privacy consent for information access." />
    </element>
    <element id="Consent.scope.coding">
      <path value="Consent.scope.coding" />
      <min value="1" />
      <max value="1" />
    </element>
    <element id="Consent.scope.coding.system">
      <path value="Consent.scope.coding.system" />
      <min value="1" />
      <fixedUri value="http://terminology.hl7.org/CodeSystem/consentscope" />
    </element>
    <element id="Consent.scope.coding.code">
      <path value="Consent.scope.coding.code" />
      <min value="1" />
      <fixedCode value="patient-privacy" />
    </element>
    <element id="Consent.category">
      <path value="Consent.category" />
      <slicing>
        <discriminator>
          <type value="value" />
          <path value="coding.system" />
        </discriminator>
        <rules value="open" />
      </slicing>
      <binding>
        <strength value="extensible" />
        <valueSet value="http://pcm.fhir.health.gov.il/ValueSet/pcm-consent-category" />
      </binding>
    </element>
    <element id="Consent.category:actCode">
      <path value="Consent.category" />
      <sliceName value="actCode" />
      <short value="ActCode category (optional informational category)." />
      <min value="0" />
      <max value="1" />
    </element>
    <element id="Consent.category:actCode.coding.system">
      <path value="Consent.category.coding.system" />
      <min value="1" />
      <fixedUri value="http://terminology.hl7.org/CodeSystem/v3-ActCode" />
    </element>
    <element id="Consent.category:actCode.coding.code">
      <path value="Consent.category.coding.code" />
      <fixedCode value="INFA" />
    </element>
    <element id="Consent.category:accessMode">
      <path value="Consent.category" />
      <sliceName value="accessMode" />
      <short value="Access mode copied from the HealthcareService catalog." />
      <comment value="PCM copies the access mode (#one-time or #continuous) from the selected HealthcareService into this category coding." />
      <min value="0" />
      <max value="1" />
      <binding>
        <strength value="required" />
        <valueSet value="http://pcm.fhir.health.gov.il/ValueSet/pcm-access-mode" />
      </binding>
    </element>
    <element id="Consent.category:accessMode.coding">
      <path value="Consent.category.coding" />
      <min value="1" />
      <max value="1" />
    </element>
    <element id="Consent.category:accessMode.coding.system">
      <path value="Consent.category.coding.system" />
      <min value="1" />
      <fixedUri value="http://fhir.health.gov.il/cs/pcm-consent-access-mode" />
    </element>
    <element id="Consent.category:accessMode.coding.code">
      <path value="Consent.category.coding.code" />
      <min value="1" />
      <binding>
        <strength value="required" />
        <valueSet value="http://pcm.fhir.health.gov.il/ValueSet/pcm-access-mode" />
      </binding>
    </element>
    <element id="Consent.patient">
      <path value="Consent.patient" />
      <comment value="Always a logical reference (identifier only). The identifier.system can be IL national-id or a passport system URI (passport-uri VS)." />
      <min value="1" />
    </element>
    <element id="Consent.patient.reference">
      <path value="Consent.patient.reference" />
      <max value="0" />
    </element>
    <element id="Consent.patient.identifier">
      <path value="Consent.patient.identifier" />
      <comment value="Logical patient identifier used for PCM portal identification flows (ID or Passport)." />
      <min value="1" />
      <constraint>
        <key value="pcm-consent-patient-id-system" />
        <severity value="error" />
        <human value="Consent.patient.identifier.system must be either IL national-id or one of the passport system URIs." />
        <expression value="patient.identifier.system = 'http://fhir.health.gov.il/identifier/il-national-id' or patient.identifier.system.memberOf('http://fhir.health.gov.il/ValueSet/passport-uri')" />
        <source value="http://pcm.fhir.health.gov.il/StructureDefinition/pcm-consent" />
      </constraint>
      <constraint>
        <key value="pcm-consent-patient-id-value-trimmed" />
        <severity value="error" />
        <human value="Consent.patient.identifier.value must be non-empty and must not have leading/trailing whitespace." />
        <expression value="patient.identifier.value.exists() and patient.identifier.value = patient.identifier.value.trim() and patient.identifier.value.trim() != ''" />
        <source value="http://pcm.fhir.health.gov.il/StructureDefinition/pcm-consent" />
      </constraint>
      <constraint>
        <key value="pcm-consent-patient-il-id-chk" />
        <severity value="error" />
        <human value="If Consent.patient.identifier.system is IL national-id, the value must be 9 digits and pass the Israeli ID check-digit algorithm." />
        <expression value="patient.identifier.system = 'http://fhir.health.gov.il/identifier/il-national-id' implies (patient.identifier.value.matches('^[0-9]{9}$') and patient.identifier.value.substring(8,1).toInteger()= (10-((patient.identifier.value.substring(0,1).toInteger() + patient.identifier.value.substring(1,1).toInteger()*2 mod 10 + patient.identifier.value.substring(1,1).toInteger()*2 div 10 + patient.identifier.value.substring(2,1).toInteger() + patient.identifier.value.substring(3,1).toInteger()*2 mod 10 + patient.identifier.value.substring(3,1).toInteger()*2 div 10 + patient.identifier.value.substring(4,1).toInteger() + patient.identifier.value.substring(5,1).toInteger()*2 mod 10 + patient.identifier.value.substring(5,1).toInteger()*2 div 10 + patient.identifier.value.substring(6,1).toInteger() + patient.identifier.value.substring(7,1).toInteger()*2 mod 10 + patient.identifier.value.substring(7,1).toInteger()*2 div 10) mod 10)) mod 10))" />
        <source value="http://pcm.fhir.health.gov.il/StructureDefinition/pcm-consent" />
      </constraint>
    </element>
    <element id="Consent.patient.identifier.system">
      <path value="Consent.patient.identifier.system" />
      <min value="1" />
    </element>
    <element id="Consent.patient.identifier.value">
      <path value="Consent.patient.identifier.value" />
      <min value="1" />
    </element>
    <element id="Consent.dateTime">
      <path value="Consent.dateTime" />
      <comment value="Timestamp when the Consent was created. Set by the PCM." />
    </element>
    <element id="Consent.performer">
      <path value="Consent.performer" />
      <max value="0" />
    </element>
    <element id="Consent.organization">
      <path value="Consent.organization" />
      <max value="0" />
    </element>
    <element id="Consent.source[x]">
      <path value="Consent.source[x]" />
      <max value="0" />
    </element>
    <element id="Consent.source[x]:sourceReference">
      <path value="Consent.source[x]" />
      <sliceName value="sourceReference" />
      <max value="0" />
      <type>
        <code value="Reference" />
        <targetProfile value="http://fhir.health.gov.il/StructureDefinition/il-core-consent" />
        <targetProfile value="http://fhir.health.gov.il/StructureDefinition/il-core-document-reference" />
        <targetProfile value="http://hl7.org/fhir/StructureDefinition/Contract" />
        <targetProfile value="http://fhir.health.gov.il/StructureDefinition/il-core-questionnaire-response" />
      </type>
    </element>
    <element id="Consent.source[x]:sourceAttachment">
      <path value="Consent.source[x]" />
      <sliceName value="sourceAttachment" />
      <min value="0" />
      <max value="0" />
      <type>
        <code value="Attachment" />
      </type>
    </element>
    <element id="Consent.policy">
      <path value="Consent.policy" />
      <max value="0" />
    </element>
    <element id="Consent.policyRule">
      <path value="Consent.policyRule" />
      <max value="0" />
    </element>
    <element id="Consent.verification">
      <path value="Consent.verification" />
      <max value="0" />
    </element>
    <element id="Consent.provision">
      <path value="Consent.provision" />
      <comment value="PCM uses a single provision of type 'permit' to express the access grant." />
    </element>
    <element id="Consent.provision.type">
      <path value="Consent.provision.type" />
      <min value="1" />
      <fixedCode value="permit" />
    </element>
    <element id="Consent.provision.period">
      <path value="Consent.provision.period" />
      <comment value="Derived from the HealthcareService catalog (access duration), resulting in an explicit validity period." />
    </element>
    <element id="Consent.provision.actor">
      <path value="Consent.provision.actor" />
      <slicing>
        <discriminator>
          <type value="value" />
          <path value="role.coding.code" />
        </discriminator>
        <rules value="open" />
      </slicing>
    </element>
    <element id="Consent.provision.actor:requestor">
      <path value="Consent.provision.actor" />
      <sliceName value="requestor" />
      <short value="The requesting Service Provider organization." />
      <comment value="Always populated by the server from the access token/certificate. Role fixed to IRCP." />
      <min value="0" />
      <max value="1" />
    </element>
    <element id="Consent.provision.actor:requestor.role.coding">
      <path value="Consent.provision.actor.role.coding" />
      <min value="1" />
      <max value="1" />
    </element>
    <element id="Consent.provision.actor:requestor.role.coding.system">
      <path value="Consent.provision.actor.role.coding.system" />
      <min value="1" />
      <fixedUri value="http://terminology.hl7.org/CodeSystem/v3-ParticipationType" />
    </element>
    <element id="Consent.provision.actor:requestor.role.coding.code">
      <path value="Consent.provision.actor.role.coding.code" />
      <min value="1" />
      <fixedCode value="IRCP" />
    </element>
    <element id="Consent.provision.actor:requestor.reference">
      <path value="Consent.provision.actor.reference" />
      <type>
        <code value="Reference" />
        <targetProfile value="http://pcm.fhir.health.gov.il/StructureDefinition/pcm-organization-service-provider" />
      </type>
    </element>
    <element id="Consent.provision.actor:data-source">
      <path value="Consent.provision.actor" />
      <sliceName value="data-source" />
      <short value="Allowed data sources (filled by PCM after patient approval)." />
      <comment value="After consent approval, PCM derives relevant Data Source organizations and adds them here (role CST)." />
      <min value="0" />
      <max value="*" />
    </element>
    <element id="Consent.provision.actor:data-source.role.coding">
      <path value="Consent.provision.actor.role.coding" />
      <min value="1" />
      <max value="1" />
    </element>
    <element id="Consent.provision.actor:data-source.role.coding.system">
      <path value="Consent.provision.actor.role.coding.system" />
      <min value="1" />
      <fixedUri value="http://terminology.hl7.org/CodeSystem/v3-ParticipationType" />
    </element>
    <element id="Consent.provision.actor:data-source.role.coding.code">
      <path value="Consent.provision.actor.role.coding.code" />
      <min value="1" />
      <fixedCode value="CST" />
    </element>
    <element id="Consent.provision.actor:data-source.reference">
      <path value="Consent.provision.actor.reference" />
      <type>
        <code value="Reference" />
        <targetProfile value="http://pcm.fhir.health.gov.il/StructureDefinition/pcm-organization-source" />
      </type>
    </element>
    <element id="Consent.provision.action">
      <path value="Consent.provision.action" />
      <max value="0" />
    </element>
    <element id="Consent.provision.securityLabel">
      <path value="Consent.provision.securityLabel" />
      <max value="0" />
    </element>
    <element id="Consent.provision.purpose">
      <path value="Consent.provision.purpose" />
      <comment value="Purpose of use for PCM information sharing (currently treatment)." />
      <min value="1" />
    </element>
    <element id="Consent.provision.purpose.system">
      <path value="Consent.provision.purpose.system" />
      <min value="1" />
      <fixedUri value="http://terminology.hl7.org/CodeSystem/v3-ActReason" />
    </element>
    <element id="Consent.provision.purpose.code">
      <path value="Consent.provision.purpose.code" />
      <min value="1" />
      <fixedCode value="TREAT" />
    </element>
    <element id="Consent.provision.class">
      <path value="Consent.provision.class" />
      <max value="0" />
    </element>
    <element id="Consent.provision.code">
      <path value="Consent.provision.code" />
      <max value="0" />
    </element>
    <element id="Consent.provision.dataPeriod">
      <path value="Consent.provision.dataPeriod" />
      <max value="0" />
    </element>
    <element id="Consent.provision.data">
      <path value="Consent.provision.data" />
      <max value="0" />
    </element>
  </differential>
</StructureDefinition>