36 lines
1.4 KiB
JavaScript
36 lines
1.4 KiB
JavaScript
// JavaScript Document
|
|
exports.onesignal_transfer_subscription_alias = async function (options) {
|
|
// get appid parsed as options.appid, throw error is missing
|
|
const appid = this.parseOptional(options.appid, '*', process.env.ONESIGNAL_APP_ID)
|
|
// log before and after vlaues
|
|
console.log('App id: ' + options.appid + " => " + appid)
|
|
|
|
var subscription_id = this.parseRequired(options.subscription_id, '*', 'Subscription ID not specified')
|
|
console.log('Subscription_id: ' + options.subscription_id + " => " + subscription_id)
|
|
|
|
var alias_label = this.parseOptional(options.alias_label, '*', null)
|
|
console.log('alias_label: ' + options.alias_label + " => " + alias_label)
|
|
|
|
|
|
const fetch = require('node-fetch');
|
|
|
|
const url = `https://api.onesignal.com/apps/${appid}/subscriptions/${subscription_id}/owner`;
|
|
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: { external_id: alias_label } })
|
|
};
|
|
|
|
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));
|
|
}
|
|
|