<?xml version="1.0" encoding="UTF-8"?>
<actions>

<meta>
  <description>Template für die standardisierte PAIA-API</description>
  <variable name="baseurl"><description>Serverurl inkl. Protokol</description></variable>
  <variable name="server"><description>Serveraddresse (veraltet)</description></variable>
</meta>

<action id="connect">
  <s>baseurl := if (get("baseurl", ())) then get("baseurl", ()) else x"https://{get('server')}"</s>
  <try>
    <json url="{$baseurl}/auth/login">
      <header>Accept: application/json,*/*</header>
      <method>POST</method>
      <post>username={uri-encode($username)}&amp;password={uri-encode($password)}&amp;grant_type=password</post>
    </json>
    <catch errors="http:403"><s>vl:raise-login("HTTP Code 403. Vermutlich ungültige Kontonummer oder Passwort.")</s></catch></try>
    <s>
      xquery version "3.0-videlibri";
      access_token := encode-for-uri($json.access_token),
      patron_identifier := encode-for-uri(if ($json.patron) then $json.patron else $username),
      scope := if ($json.scope) then $json.scope else "read_items write_items",
      allow_renew := contains(x" {$scope} ", " write_items "),
      if (not(contains(x" {$scope} ", " read_items "))) then ( error(QName("VL:1"), "No access" ) ) else ()
    </s>
</action>

<action id="update-all">
  <json url="{$baseurl}/core/{$patron_identifier}/items?access_token={$access_token}">
    <header>Accept: application/json,*/*</header>
  </json>
  <s>
   if ($json.error) then videlibri:raise(x"PAIA error on items: {$json.code}: {$json.error}: {$json.error_description}") else (
     videlibri:delete-current-books(),
     for $doc in jn:members($json.doc)[(.).status &lt; 5] return
       book := {
         "id": $doc.label,
         "title": $doc.about,
         "duedate": if ($doc.duedate) then parse-date($doc.duedate, if (substring($doc.duedate, 3, 1) = "-") then "dd-mm-yyyy" else "yyyy-mm-dd") else (),
         "status": if ($doc.renewals) then x"bisher {$doc.renewals}-mal verlängert" else (),
         "statusId": if ($doc.status = (1,2)) then "ordered"
                     else if ($doc.status = 4) then "provided"
                     else if ($doc.canrenew) then "curious" 
                     else "critical",
         "cancelable": $doc.cancancel,
         
         "item": $doc.item, 
         "edition": $doc.edition,
         "barcode": $doc.barcode 
       }
   )
    </s>
</action>

<action id="renew-list">
  <!--<variable name="book-list">encode-for-uri(serialize-json([ 
        for $book in $renew-books return {
          "item": $book.item,
          "edition": $book.edition
        } ]))</variable>-->
  <json test="$allow_renew" url="{$baseurl}/core/{$patron_identifier}/renew?access_token={$access_token}" >
    <header>Content-Type: application/json; charset=utf-8</header>
    <header>Accept: application/json,*/*</header>
    <method>POST</method>
    <post>
    { serialize-json({ "doc":  
      for $book in $renew-books return {
        "item": $book.item,
        "edition": $book.edition
      }
    }) }
    </post> 
  </json>
  <call action="update-all"/>
</action>


</actions>
