exports.onesignal_create_alias_by_subscription = async function (options) {

    const appid = this.parseOptional(options.appid, '*', process.env.ONESIGNAL_APP_ID)
    console.log(options.appid + " => " + appid)

    const subscription_id = this.parseRequired(options.subscription_id, '*', 'No Subscription ID specified')
    console.log('Subscription ID: ' + options.subscription_id + " => " + subscription_id)

    const alias_label = this.parseRequired(options.alias_label.trim(), '*', 'No Alias Label specified')
    console.log(options.alias_label + " => " + alias_label)

    const alias_id = this.parseOptional(options.alias_id, '*', 'No Alias ID specified')
    console.log(options.alias_id + " => " + alias_id)



    const fetch = require('node-fetch');

    const url = `https://api.onesignal.com/apps/${appid}/subscriptions/${subscription_id}/user/identity`;
    console.log('URL: ' + url)
    const os_options = {
        method: 'PATCH',
        headers: { accept: 'application/json', 'content-type': 'application/json', Authorization: `Basic ${process.env.ONESIGNAL_REST_API_KEY}` },
        body: JSON.stringify({
            identity: {
                alias_label: alias_label,
                alias_id: alias_id
            }
        })
    };

    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));

}