<StructureDefinition xmlns="http://hl7.org/fhir">
  <id value="pcm-consent" />
  <url value="http://pcm.fhir.health.gov.il/StructureDefinition/pcm-consent" />
  <version value="0.3.2" />
  <name value="PCMConsent" />
  <title value="PCM Consent" />
  <status value="active" />
  <description value="PCM consent resource. Created by a Service Provider Organization with status 'proposed'. Patient decisions and assisted/delegated flows are handled outside the client FHIR API and may narrow the service-requested continuous mode to one-time and/or shorten the service maximum duration. The authoritative Consent carries the requested mode while proposed and the effective approved mode and period after approval. Service providers may set status to 'inactive' only for consents they created; deletion is not allowed. Client search/get is limited to consents created by 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">
      <path value="Consent" />
      <constraint>
        <key value="pcm-consent-business-status-map" />
        <severity value="error" />
        <human value="The PCM Consent business status must agree with Consent.status." />
        <expression value="(status = 'proposed' implies extension.where(url = 'http://pcm.fhir.health.gov.il/StructureDefinition/ext-consent-business-status').value.ofType(Coding).code = 'requested') and (status = 'active' implies extension.where(url = 'http://pcm.fhir.health.gov.il/StructureDefinition/ext-consent-business-status').value.ofType(Coding).code = 'active') and (status = 'rejected' implies extension.where(url = 'http://pcm.fhir.health.gov.il/StructureDefinition/ext-consent-business-status').value.ofType(Coding).code = 'rejected') and (status = 'inactive' implies (extension.where(url = 'http://pcm.fhir.health.gov.il/StructureDefinition/ext-consent-business-status').value.ofType(Coding).code = 'temporarily-inactive' or extension.where(url = 'http://pcm.fhir.health.gov.il/StructureDefinition/ext-consent-business-status').value.ofType(Coding).code = 'permanently-inactive' or extension.where(url = 'http://pcm.fhir.health.gov.il/StructureDefinition/ext-consent-business-status').value.ofType(Coding).code = 'service-provider-cancelled')) and (status = 'entered-in-error' implies extension.where(url = 'http://pcm.fhir.health.gov.il/StructureDefinition/ext-consent-business-status').value.ofType(Coding).code = 'permanently-inactive')" />
        <source value="http://pcm.fhir.health.gov.il/StructureDefinition/pcm-consent" />
      </constraint>
      <constraint>
        <key value="pcm-consent-period-by-status" />
        <severity value="error" />
        <human value="Active Consent resources require an approval-anchored period; proposed and rejected Consent resources must not have one." />
        <expression value="(status = 'active' implies provision.period.exists()) and ((status = 'proposed' or status = 'rejected') implies provision.period.empty())" />
        <source value="http://pcm.fhir.health.gov.il/StructureDefinition/pcm-consent" />
      </constraint>
      <constraint>
        <key value="pcm-consent-period-order" />
        <severity value="error" />
        <human value="When a Consent period is present, it must have both endpoints and end must be later than start." />
        <expression value="provision.period.empty() or (provision.period.start.exists() and provision.period.end.exists() and provision.period.start &lt; provision.period.end)" />
        <source value="http://pcm.fhir.health.gov.il/StructureDefinition/pcm-consent" />
      </constraint>
      <constraint>
        <key value="pcm-consent-sources-by-status" />
        <severity value="error" />
        <human value="Proposed and rejected Consent resources must not identify approved Data Sources." />
        <expression value="(status = 'proposed' or status = 'rejected') implies provision.actor.where(role.coding.where(system = 'http://terminology.hl7.org/CodeSystem/v3-ParticipationType' and code = 'CST').exists()).empty()" />
        <source value="http://pcm.fhir.health.gov.il/StructureDefinition/pcm-consent" />
      </constraint>
      <constraint>
        <key value="pcm-consent-test-security-label" />
        <severity value="error" />
        <human value="The two synthetic test patients require the HTEST security label, and HTEST is not used for another patient identifier." />
        <expression value="(patient.identifier.system = 'http://fhir.health.gov.il/identifier/il-hdp-test-id') = meta.security.where(system = 'http://terminology.hl7.org/CodeSystem/v3-ActReason' and code = 'HTEST').exists()" />
        <source value="http://pcm.fhir.health.gov.il/StructureDefinition/pcm-consent" />
      </constraint>
    </element>
    <element id="Consent.id">
      <path value="Consent.id" />
      <min value="1" />
    </element>
    <element id="Consent.meta">
      <path value="Consent.meta" />
      <min value="1" />
    </element>
    <element id="Consent.meta.lastUpdated">
      <path value="Consent.meta.lastUpdated" />
      <comment value="Server-managed timestamp of the most recent authoritative resource change." />
      <min value="1" />
    </element>
    <element id="Consent.meta.profile">
      <path value="Consent.meta.profile" />
      <slicing>
        <discriminator>
          <type value="value" />
          <path value="$this" />
        </discriminator>
        <rules value="open" />
      </slicing>
      <comment value="PCM includes the applicable server-return profile URL; additional compatible profile URLs are allowed." />
      <min value="1" />
    </element>
    <element id="Consent.meta.profile:pcmProfile">
      <path value="Consent.meta.profile" />
      <sliceName value="pcmProfile" />
      <min value="1" />
      <max value="1" />
      <fixedCanonical value="http://pcm.fhir.health.gov.il/StructureDefinition/pcm-consent" />
    </element>
    <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="2" />
    </element>
    <element id="Consent.extension:businessStatus">
      <path value="Consent.extension" />
      <sliceName value="businessStatus" />
      <comment value="PCM-managed business status. A Service Provider cannot create, select, or modify it directly." />
      <min value="1" />
      <max value="1" />
      <type>
        <code value="Extension" />
        <profile value="http://pcm.fhir.health.gov.il/StructureDefinition/ext-consent-business-status" />
      </type>
    </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="PCM-managed stable business identifier for the Consent. It is distinct from the FHIR logical id." />
      <min value="1" />
      <max value="1" />
    </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 approval/rejection/revocation is handled outside the client FHIR API. PCM may set status after patient decision, expiration, or policy events. Service providers may set status 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="closed" />
      </slicing>
      <min value="2" />
      <max value="2" />
      <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="Fixed information-access category." />
      <min value="1" />
      <max value="1" />
    </element>
    <element id="Consent.category:actCode.coding">
      <path value="Consent.category.coding" />
      <min value="1" />
      <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" />
      <min value="1" />
      <fixedCode value="INFA" />
    </element>
    <element id="Consent.category:accessMode">
      <path value="Consent.category" />
      <sliceName value="accessMode" />
      <short value="Server-managed requested/effective access mode." />
      <comment value="PCM initializes the proposed Consent from the selected HealthcareService access mode. An authorized patient decision, including an assisted or delegated flow, may retain that mode or narrow continuous access to one-time. The authoritative Consent carries the requested mode while proposed and the effective approved mode after a decision; it never widens a one-time service to continuous." />
      <min value="1" />
      <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://pcm.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 an identifier-only logical reference. The identifier uses IL national-id, an allowed passport system, or the exact synthetic test system http://fhir.health.gov.il/identifier/il-hdp-test-id with value 1 or 2." />
      <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. The synthetic test system accepts only values 1 and 2 and requires the HTEST meta.security label on the returned Consent." />
      <min value="1" />
      <constraint>
        <key value="pcm-consent-patient-id-system" />
        <severity value="error" />
        <human value="Consent.patient.identifier must use IL national-id, an allowed passport system, or one of the two exact synthetic test identifiers." />
        <expression value="system = 'http://fhir.health.gov.il/identifier/il-national-id' or system.memberOf('http://fhir.health.gov.il/ValueSet/passport-uri') or (system = 'http://fhir.health.gov.il/identifier/il-hdp-test-id' and (value = '1' or value = '2'))" />
        <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="value.exists() and value.matches('^\\S(.*\\S)?$')" />
        <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="system = 'http://fhir.health.gov.il/identifier/il-national-id' implies (value.matches('^[0-9]{9}$') and value.substring(8,1).toInteger()= (10-((value.substring(0,1).toInteger() + value.substring(1,1).toInteger()*2 mod 10 + value.substring(1,1).toInteger()*2 div 10 + value.substring(2,1).toInteger() + value.substring(3,1).toInteger()*2 mod 10 + value.substring(3,1).toInteger()*2 div 10 + value.substring(4,1).toInteger() + value.substring(5,1).toInteger()*2 mod 10 + value.substring(5,1).toInteger()*2 div 10 + value.substring(6,1).toInteger() + value.substring(7,1).toInteger()*2 mod 10 + 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." />
      <min value="1" />
    </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" />
      <comment value="PCM-managed reference to the versioned legal Consent policy." />
      <min value="1" />
      <max value="1" />
    </element>
    <element id="Consent.policy.authority">
      <path value="Consent.policy.authority" />
      <min value="1" />
      <fixedUri value="https://www.gov.il/" />
    </element>
    <element id="Consent.policy.uri">
      <path value="Consent.policy.uri" />
      <min value="1" />
      <fixedUri value="https://pcm.fhir.health.gov.il/consent-policy/medical-information-mobility-law-2024" />
    </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." />
      <min value="1" />
    </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="PCM sets this period when the patient approves the Consent. The start is the approval timestamp. The end reflects the effective patient-approved duration and cannot exceed the selected service's accessDuration ceiling. An authorized later patient decision, including an assisted or delegated flow, may shorten the effective period. Proposed and rejected Consent resources have no period." />
    </element>
    <element id="Consent.provision.period.start">
      <path value="Consent.provision.period.start" />
      <min value="1" />
    </element>
    <element id="Consent.provision.period.end">
      <path value="Consent.provision.period.end" />
      <min value="1" />
    </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>
      <min value="1" />
    </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="1" />
      <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:requestor.reference.reference">
      <path value="Consent.provision.actor.reference.reference" />
      <min value="1" />
    </element>
    <element id="Consent.provision.actor:requestor.reference.identifier">
      <path value="Consent.provision.actor.reference.identifier" />
      <max value="0" />
    </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.actor:data-source.reference.reference">
      <path value="Consent.provision.actor.reference.reference" />
      <min value="1" />
    </element>
    <element id="Consent.provision.actor:data-source.reference.identifier">
      <path value="Consent.provision.actor.reference.identifier" />
      <max value="0" />
    </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>