<script>
  const https = require("https");
  let id = "";
  let devices;
  let policy;
  export let privat;
  function fetch_json(id) {
    const url = encodeURI(
      `https://${privat.MDMSERVER}/relution/api/v1/autoenrollments?getNonpagedCount=true&limit=1&filter={"type":"containsString","fieldName":"serialNumber","contains":"${id}"}&field=uuid`
    );
    const options = { headers: { "X-User-Access-Token": privat.MDMKEY } };
    https.get(url, options, async (res) => {
      try {
        let body = "";
        res.setEncoding("utf-8");
        for await (const chunk of res) {
          body += chunk;
        }
        devices = JSON.parse(body);
      } catch (e) {
        console.log(e);
      }
    });
  }
  function get_policies() {
    const url = encodeURI(
      `https://${privat.MDMSERVER}/relution/api/v1/policies?getNonpagedCount=true&limit=100&offset=0&sortOrder=-modificationDate&filter={"type":"logOp","operation":"AND","filters":[{"type":"stringEnum","fieldName":"platform","values":["IOS"]},{"type":"boolean","fieldName":"versions.published","value":true},{"type":"string","fieldName":"deletionDate","value":"NULL"}]}`
    );
    const options = { headers: { "X-User-Access-Token": privat.MDMKEY } };
    return new Promise((resolve, reject) => {
      https.get(url, options, async (res) => {
        try {
          let body = "";
          res.setEncoding("utf-8");
          for await (const chunk of res) {
            body += chunk;
          }
          resolve(JSON.parse(body));
        } catch (e) {
          reject(e);
        }
      });
    });
  }
  function set_policy(device) {
    console.log("SET");
    if (devices.total !== 1 && !policy) return;
    device.defaultPolicy = { uuid: policy };
    const data = JSON.stringify(device);
    console.log(data);
    const options = {
      hostname: privat.MDMSERVER,
      port: 443,
      path: `/relution/api/v1/autoenrollments/${device.uuid}`,
      method: "PUT",
      headers: {
        "Content-Type": "application/json",
        "Content-Length": data.length,
        "X-User-Access-Token": privat.MDMKEY,
      },
    };
    const req = https.request(options, (res) => {
      console.log(`statusCode: ${res.statusCode}`);
      if (res.statusCode === 200) {
        id = "";
        devices = undefined;
      }
    });
    req.on("error", (error) => {
      console.error(error);
    });
    req.write(data);
    req.end();
  }
</script>

Verfügbare Policies:
{#await get_policies() then policies}
  {#each policies.results as p}
    <br /><input type="radio" bind:group={policy} value={p.uuid} />{p.name}
  {/each}
  <br />ausgewählt {policies.results.find((p) => p.uuid === policy)?.name ||
    "keine"}
{/await}
<br />
<br /><input
  type="text"
  bind:value={id}
  on:keyup={(e) =>
    e.code === "Enter" ? set_policy(devices.results[0]) : fetch_json(id)}
/>

{#if devices}
  {#if devices.total > 1}
    {devices.total} Ergebnisse
  {:else if devices.total === 0}
    keine Ergebnisse
  {:else if devices.total === 1}
    <br />{devices.results[0].uuid}
    <br />{devices.results[0].serialNumber}
    <br />{devices.results[0].description}
  {:else}Fehler
  {/if}
{/if}
