Unhandled promise rejections - Working with the Architect APIs

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

Hello,

I've tried to reproduce your problem but I didn't get the error you are seeing. The code executed without this error.
Maybe a different nodejs version, or a difference of Genesys Cloud configuration (permission, schedule groups, Call Routing name).
I've used the same module/packages versions and tried also with latest version.

But adding a catch on the second "Promise.all" might help (below).

const rrulestr = require('rrule').rrulestr;
const moment = require('moment');
const platformClient = require('purecloud-platform-client-v2');
const client = platformClient.ApiClient.instance;


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}`);
				})
				.catch(console.log);
			} else {
				console.log(`IVR is open? ${isOpen}`);
			}
		})
		.catch(console.log);
}


// Get client credentials from environment variables
const CLIENT_ID = process.env.GENESYS_CLOUD_CLIENT_ID;
const CLIENT_SECRET = process.env.GENESYS_CLOUD_CLIENT_SECRET;
const ORG_REGION = process.env.GENESYS_CLOUD_REGION; // eg. us_east_1

// Set environment
const environment = platformClient.PureCloudRegionHosts[ORG_REGION];
if(environment) client.setEnvironment(environment);

client
	.loginClientCredentialsGrant(CLIENT_ID, CLIENT_SECRET)
	.then(() => {
		var architectApi = new platformClient.ArchitectApi();

		const IVR_NAME = 'Queue 1';
		architectApi
			.getArchitectIvrs({ name: IVR_NAME })
			.then((ivrs) => {
				console.log(ivrs);
				let ivr = ivrs.entities[0];

				let scheduleGroupId = ivr.scheduleGroup.id;

				architectApi.getArchitectSchedulegroup(scheduleGroupId).then(function(scheduleGroup) {
					evaluateScheduleGroup(scheduleGroup);
				});
			})
			.catch(console.log);
	})
	.catch(console.log);

Regards,

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.