<OperationDefinition xmlns="http://hl7.org/fhir">
  <url value="http://electronichealth.se/fhir/OperationDefinition/NLLGetLatestDispense" />
  <version value="1.0.0" />
  <name value="NLLGetLatestDispense" />
  <title value="latestdispense" />
  <status value="draft" />
  <kind value="query" />
  <date value="2026-01-12T00:00:00+01:00" />
  <publisher value="The Swedish eHealth Agency" />
  <description value="An operation implemented as a named query that retrieves prescriptions for a patient or for a specified set of prescriptions, returning only the most recent dispense associated with each prescription. The operation also supports the optional inclusion of associated provenance resources." />
  <purpose value="Simplify the consumption of prescription data by identifying and returning only the most recent dispense for each prescription on the server side, with the option to include related events." />
  <affectsState value="false" />
  <code value="latest-dispense" />
  <comment value="The query always returns the prescription (MedicationRequest) together with its most recent dispense (MedicationDispense).&#xA;&#xA;Clients may optionally request the inclusion of Provenance related to both the prescription and the dispense. The server may obtain these related resources using FHIR Search reverse‑include expressions, such as _revinclude=Provenance:target:MedicationRequest to include prescribing Provenance, and _revinclude:iterate=Provenance:target:MedicationDispense to include dispense‑related Provenance. These expressions illustrate one possible retrieval approach and are not formal operation parameters.&#xD;&#xA;&#xD;&#xA;Example:&#xD;&#xA;```&#xD;&#xA;_revinclude=Provenance:target:MedicationRequest&#xA;&amp;_revinclude:iterate=Provenance:target:MedicationDispense&#xA;```&#xD;&#xA;&#xD;&#xA;This named query is invoked as:&#xA;GET [base]/MedicationRequest?_query=latest-dispense" />
  <resource value="MedicationRequest" />
  <system value="false" />
  <type value="false" />
  <instance value="false" />
  <parameter>
    <name value="patient._id" />
    <use value="in" />
    <min value="0" />
    <max value="1" />
    <documentation value="The patient whose prescriptions are to be retrieved. Use of this parameter precludes the use of the _id parameter within the same request." />
    <type value="Reference" />
  </parameter>
  <parameter>
    <name value="_id" />
    <use value="in" />
    <min value="0" />
    <max value="*" />
    <documentation value="A comma‑separated list of one or more prescription identifiers (MedicationRequest IDs)." />
    <type value="string" />
  </parameter>
  <parameter>
    <name value="status" />
    <use value="in" />
    <min value="0" />
    <max value="*" />
    <documentation value="A comma‑separated list of prescription status values that may be used in combination with a patient reference." />
    <type value="ValueSet" />
    <binding>
      <strength value="required" />
      <valueSet value="http://electronichealth.se/fhir/ValueSet/medication-request-status" />
    </binding>
  </parameter>
  <parameter>
    <name value="authoredon" />
    <use value="in" />
    <min value="0" />
    <max value="1" />
    <documentation value="Specifies the authoredOn date used for date‑based filtering of prescriptions. When provided, it defines the earliest date from which prescriptions shall be included. The parameter may be supplied on its own to indicate a start date, or together with an upper bound to express a date interval.&#xA;&#xA;Example: ?authoredon=ge2023-01-01&amp;authoredon=le2024-01-01" />
    <type value="Range" />
  </parameter>
  <parameter>
    <name value="result" />
    <use value="out" />
    <min value="1" />
    <max value="1" />
    <documentation value="A Bundle containing prescriptions (MedicationRequest), the most recent dispense (MedicationDispense) for each prescription, and, when requested, Provenance resources associated specifically with the prescriptions." />
    <type value="Bundle" />
  </parameter>
</OperationDefinition>