// JavaScript Document
//V1.0.0
exports.onesignal_export_audience_activity_csv = async function (options) {
    // get appid
    const appid = this.parseOptional(options.appid, '*', process.env.ONESIGNAL_APP_ID)
    console.log(options.appid + " => " + appid)

    const notification_id = this.parseRequired(options.notification_id, '*', 'No notificastion ID Specified')
    console.log(notification_id + " => " + notification_id)
    const fetch = require('node-fetch');

    const url = `https://api.onesignal.com/notifications/${notification_id}/export_events?app_id=${appid}`;
    const os_options = {
        method: 'POST',
        headers: {
            accept: 'application/json',
            Authorization: `Basic ${process.env.ONESIGNAL_REST_API_KEY}`
        }
    };

    return fetch(url, os_options)
        .then(res => {
            return res.json().then(json => {
                json.status = res.status; // Add status to json response
                return json;
            });
        })
        .catch(err => console.error('Error:', err));
}

