<![CDATA[
/*
    $Id$

    File:       delete_webhooks.wsf

    Calling:     cscript ..\..\..\utils\exchange\delete_webhooks.wsf
    Parameters:
    Context:     Test only vanuit cust/Exchange folder
    Description: Verwijdert de lopende webhook subscriptions
    Note:
*/
]]>

<job id="exchange_webhooks">
  <script language="JScript" src="../wsf_shared.js"/>
  <script language="JScript" src="../json2.js"/>
  <script language="JScript" src="ms_graph.js"/>

  <script language="JScript">
/* global */ var custabspath = fso.GetAbsolutePathName("../");

    var config = loadconfig("exchange.config");
    var token;
    var Oracle = new ActiveXObject("ADODB.Connection");
    Oracle.Open('File Name=../oracle.udl'); //../Oracle.udl
    token = requestToken(config);

    var response = getSubscriptions();
    if (response && response.value) {
      var subscriptionList = response.value;
      while (response.skiptoken) {
        response = getSubscriptions(response.skiptoken);
        for (var i = 0; i < response.value.length; i++) {
          subscriptionList.push(response.value[i]);
        }
      }
      var deleted = 0;
      for (var i = 0; i < subscriptionList.length; i++) {
        if (config.hookurl && subscriptionList[i].notificationUrl.toUpperCase().indexOf(config.hookurl.toUpperCase()) != -1 // Alleen de subscriptions van deze omgeving verwijderen
            || subscriptionList[i].notificationUrl.indexOf("ngrok") != -1) { // en gewoon alle lokale
          var request = {
            "method": "DELETE",
            "url": "https://graph.microsoft.com/v1.0/subscriptions/" + subscriptionList[i].id,
            "headers": { "Accept": "application/json", "Authorization": "Bearer " + token }
          }
          if (doHTTP(request)) {
            deleted++;
          }
        }
      }
      WScript.Echo(deleted + "/" + subscriptionList.length + " subscription(s) deleted");
    }
  </script>
</job>