Hi,
I have been following the Working with the Architect APIs tutorial in the developer portal which query architect flows for their schedule and determine if an IVR is currently open or closed. https://developer.genesys.cloud/api/tutorials/architect-schedules/
Looks like recently, Unhandled promise rejections are deprecated & I am getting the below error:
Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code
UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().
Does this mean the tutorial or GIT Hub repository needs to be updated? Can someone help to advice how can we resolve this error?
This is my code:
const rrulestr = require('rrule').rrulestr;
const moment = require('moment');
const platformClient = require('purecloud-platform-client-v2');
const client = platformClient.ApiClient.instance;
client.setEnvironment('usw2.pure.cloud');
let usersApi = new platformClient.UsersApi();
const CLIENT_ID = 'CLIENT_ID';
const CLIENT_SECRET = 'CLIENT_SECRET';
function isCurrentlyInSchedule(schedule) {
var rule = rrulestr('RRULE:' + schedule.rrule);
var nextOccurance = rule.after(moment().startOf('day').toDate(), true);
console.log(`nextOccurance ${nextOccurance}`);
var doesMatchDay = moment(nextOccurance).isSame(moment(), 'day');
if (!doesMatchDay) {
return false;
}
var date = moment().format('YYYY-MM-DD');
var start = moment(date + 'T' + schedule.start.split('T')[1]);
var end = moment(date + 'T' + schedule.end.split('T')[1]);
return moment().isBetween(start, end);
}
function evaluateScheduleGroup(scheduleGroup) {
let architectApi = new platformClient.ArchitectApi();
let openSchedulePromises = [];
let closedSchedulePromises = [];
for (let x = 0; x < scheduleGroup.openSchedules.length; x++) {
openSchedulePromises.push(architectApi.getArchitectSchedule(scheduleGroup.openSchedules[x].id));
}
for (let x = 0; x < scheduleGroup.closedSchedules.length; x++) {
closedSchedulePromises.push(architectApi.getArchitectSchedule(scheduleGroup.closedSchedules[x].id));
}
Promise.all(openSchedulePromises)
.then((openSchedules) => {
let isOpen = false;
for (let x = 0; x < openSchedules.length; x++) {
if (isCurrentlyInSchedule(openSchedules[x])) {
isOpen = true;
}
}
if (isOpen) {
Promise.all(closedSchedulePromises).then((closedSchedules) => {
for (let x = 0; x < closedSchedules.length; x++) {
if (isCurrentlyInSchedule(closedSchedules[x])) {
isOpen = false;
}
}
console.log(`IVR is open? ${isOpen}`);
});
} else {
console.log(`IVR is open? ${isOpen}`);
}
})
.catch(console.log);
}
client
.loginClientCredentialsGrant(CLIENT_ID, CLIENT_SECRET)
.then(() => {
var architectApi = new platformClient.ArchitectApi();
const SCHEDULE_GROUP_NAME = 'ScheduleGroupTest';
architectApi
.getArchitectSchedulegroups({ name: SCHEDULE_GROUP_NAME })
.then((ScheduleGroupObject) => {
console.log(ScheduleGroupObject);
let ScheduleGroupValue = ScheduleGroupObject.entities[0];
let scheduleGroupId = ScheduleGroupValue.id;
architectApi.getArchitectSchedulegroup(scheduleGroupId).then(function(scheduleGroup) {
evaluateScheduleGroup(scheduleGroup);
});
})
.catch(console.log);
})
.catch(console.log);