load("integrationservices/applications/sample-terminate/configuration.js");

// The termination code requires 3 of the Integration Agent js libraries
// xmutil.js - holds the method we are going to call
// xmio.js - is used to make the calls to xM-API to find and terminate the events
// log.js - is used by both this file and the two libraries to write logs to the IA Logfile
// If you use the event.js library, you likely get all of them without having to explicitly load
// each individual one.
// In this case, we use event.js in order to benefit from the apia_input method it provides.
load("lib/integrationservices/javascript/event.js");

function apia_event(form) {
    // This handles "injection" of events via the apclient utility
    IALOG.info('sample-terminate: Handling form: ' + JSON.stringify(form, null, 2));

    // This sample expects both the propertyName and propertyValue to
    // be defined, since it uses them to identify the events we wish to
    // terminate
    var propertyName = form.properties.property_name;
    var propertyValue = form.properties.property_value;
    if (!propertyName || !propertyValue) {
        IALOG.error("Missing propertyName and/or propertyValue. Try Again.");
    } else {
        callTerminateEvents(propertyName, propertyValue);
    }
    return null;
}

function apia_callback(msg) {
    // this should never be called since we aren't creating events
    var str = "Received message from xMatters:\n";
    str += "Incident: " + msg.incident_id;
    str += "\nEvent Id: " + msg.eventidentifier;
    str += "\nCallback Type: " + msg.xmatters_callback_type;
    IALOG.info(str);
}

function apia_http(httpRequestProperties, httpResponse) {
    // This handles "injection" of events via http request
    // It expects the payload to be in JSON format.
    var data = JSON.parse(httpRequestProperties.getProperty("REQUEST_BODY"));

    var propertyName = data.propertyName;
    var propertyValue = data.propertyValue;

    if (!propertyName || !propertyValue) {
        IALOG.error("Missing propertyName and/or propertyValue. Try Again.");
    } else {
        callTerminateEvents(propertyName, propertyValue);
    }

    // we are not modifying the httpResponse in this example
}

function callTerminateEvents(propertyName, propertyValue) {
    // XMUtil.terminateEvents expects the filter properties in a Javascript object
    var filterProperties = {};
    filterProperties[propertyName] = propertyValue;

    // You can enter more than one property to filter by
    // filterProperties.shouldTerminate = 'yes';

    // There are two ways to call the XMUtil.terminateEvents code
    // 1. You can specify all parameters (filterProperties, language, xMatters Domain)
    // var terminateResponse = XMUtil.terminateEvents(filterProperties, 'en', 'https://<company>.xmatters.com');

    // 2. You can also use the defaults instead of explicitly setting the parameter
    // filterProperties is required and must always be populated
    // The language suffix is inferred from the DEFAULT_LANGUAGE_SUFFIX constants
    //   (see xmutil.js). For more information,
    //   see https://help.xmatters.com/xmAPI/index.html#get-events
    // The xMatters Domain or Base URL is interpreted based on two global variables. 
    //   XMATTERS_DOMAIN - defined in xmutil.js, it should not contain a trailing '/'
    //   WEB_SERVER_URL - defined in xmio.js, it will usually be defined as an Inbound
    //     Integration URL. The base url of your xMatters company will be inferred based
    //     on the location of '/api'.
    var terminateResponse = XMUtil.terminateEvents(filterProperties);
    
    // the response will be an object with the following shape:
    //   success - true, if the termination was successful
    //   data - if the termination was successful, it will contain the list of terminated
    //      events
    //   reason - if the termination was unsuccessful, it will give a reason for the failure
    //   error - if the termination was unsuccessful and there was an error, you can see more
    //   details
    IALOG.info(JSON.stringify(terminateResponse, null, 2));
    return terminateResponse;
}
