<![CDATA[
/*
    $Id$

    File:           process_webhooks.wsf

    Calling:        cscript ..\..\..\utils\exchange\process_webhooks.wsf
    Context:        Scheduled task
    Description:    Maakt voor alle res_ruimte's een webhook waar we notificaties op gaan krijgen
    Note:           De code veronderstelt (nog) dat de current folder een cust/xxxx/exchange folder is
                    Concreter: ../Oracle.udl wordt gebruikt
*/
]]>

<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" src="exchange_graph.js"/>
    <script language="JScript" src="../../appl/imp/imp_shared.js"/>
    <script language="JScript" src="process_webhook.js"/>
    <script language="JScript">

        /* global */ var custabspath = fso.GetAbsolutePathName("../");

        var config = loadconfig("exchange.config");

        var Oracle = new ActiveXObject("ADODB.Connection");
        Oracle.Open('File Name=../oracle.udl'); //../Oracle.udl

        var token = requestToken(config);

        var sql = "BEGIN DBMS_APPLICATION_INFO.SET_MODULE ('process_webhooks.wsf', NULL); END;";
        var oRs = Oracle.Execute(sql);

        // Determine customerId
        sql = "SELECT fac_version_cust FROM fac_version";
        oRs = Oracle.Execute(sql);
        var customerId = oRs("fac_version_cust").Value;
        oRs.Close();

        // Get the api-(user)key
        sql = "SELECT prs_perslid_key FROM prs_perslid WHERE prs_perslid_oslogin = '_MSGRAPHNOTIFICATION'";
        oRs = Oracle.Execute(sql);
        var user_key = oRs("prs_perslid_key").Value;
        oRs.Close();

        sql = "SELECT f.fac_notificatie_key,"
            + "       f.fac_notificatie_refkey,"
            + "       rr.res_ruimte_extern_id,"
            + "       f.fac_notificatie_attachments"
            + "  FROM fac_notificatie f"
            + "       JOIN res_ruimte rr ON rr.res_ruimte_key = f.fac_notificatie_refkey"
            + " WHERE     f.fac_notificatie_queue = 'EXCHANGE'"
            //+ "       AND f.fac_notificatie_notbefore <= SYSDATE"
            + " ORDER BY f.fac_notificatie_key";

        function deleteNotification(noti_key) {
            var sql2 = "DELETE fac_notificatie"
                    + " WHERE fac_notificatie_key = " + noti_key;
            var oRs2 = Oracle.Execute(sql2);
        }

        while (true) {
            oRs = Oracle.Execute(sql);
            if (oRs.EOF) {
                oRs.Close();
                break;
            }

            while (!oRs.EOF) {
                var logfile_path   = oRs("fac_notificatie_attachments").Value;
                var res_ruimte_key = oRs("fac_notificatie_refkey").Value;
                var zaalemail      = oRs("res_ruimte_extern_id").Value;
                var notidata       = "";
                try {
                    var ts = fso.OpenTextFile(logfile_path, 1);
                    notidata = JSON.parse(ts.ReadAll());
                    ts.Close();

                    fso.DeleteFile(logfile_path);
                } catch (e) {
                    __DoLog("Error reading or deleting notification.data file", "#FF0000");
                    __DoLog(e.description);
                    deleteNotification(oRs("fac_notificatie_key").Value);
                    oRs.MoveNext();
                    continue;
                }

                process_webhook(res_ruimte_key, zaalemail, notidata);
                deleteNotification(oRs("fac_notificatie_key").Value);

                oRs.MoveNext();
            }
            oRs.Close();
        }

        Oracle.Close();
    </script>
</job>