Решено ниже
Я пытаюсь создать чат-бота, который автоматически отменяет назначенную встречу, если она назначена в нерабочее время. Это моя текущая функция:
(updated)
// This function checks for the availability of the time slot, which starts at 'dateTimeStart' and ends at 'dateTimeEnd'.
// 'dateTimeStart' and 'dateTimeEnd' are instances of a Date object.
function checkCalendarAvailablity (dateTimeStart, dateTimeEnd) {
var appsoftOpen = new Date();
var appsoftClose = new Date();
appsoftOpen.setHours(9);
ppsoftClose.setHours(5);
var time = dateTimeStart.getHours();
var appsoftClose1 = appsoftClose.getHours();
var appsoftOpen1 = appsoftOpen.getHours();
time = Number(time);
appsoftClose = Number(appsoftClose1);
appsoftOpen = Number(appsoftOpen1);
return new Promise((resolve, reject) => {
calendar.events.list({
auth: serviceAccountAuth, // List events for time period
calendarId: calendarId,
timeMin: dateTimeStart.toISOString(),
timeMax: dateTimeEnd.toISOString()
}, (err, calendarResponse) => {
// Check if there is an event already on the Calendar
if (err || calendarResponse.data.items.length > 0) {
reject(err || new Error('Requested time conflicts with another appointment'));
} else if (err || time >= appsoftClose1) {
reject(err || new Error('Requested time falls outside business hours'));
} else if (err || time <= appsoftOpen1) {
reject(err || new Error('Requested time falls outside business hours'));
}else {
resolve(calendarResponse);
}
});
});
}
Прямо сейчас, это предотвращает перекрытие встреч, но позволяет назначать встречи в любое время. Предложения? Спасибо!
решаемые
// This function checks for the availability of the time slot, which starts at 'dateTimeStart' and ends at 'dateTimeEnd'.
// 'dateTimeStart' and 'dateTimeEnd' are instances of a Date object.
function checkCalendarAvailablity (dateTimeStart, dateTimeEnd) {
var time = dateTimeStart.getHours();
// adjust for timezone
time = time - 4;
console.log(time);
return new Promise((resolve, reject) => {
calendar.events.list({
auth: serviceAccountAuth, // List events for time period
calendarId: calendarId,
timeMin: dateTimeStart.toISOString(),
timeMax: dateTimeEnd.toISOString()
}, (err, calendarResponse) => {
// Check if there is an event already on the Calendar
if (err || calendarResponse.data.items.length > 0) {
reject(err || new Error('Requested time conflicts with another appointment'));
} else if (err || time >= 17) {
reject(err || new Error('Requested time falls outside business hours'));
} else if (err || time <= 9) {
reject(err || new Error('Requested time falls outside business hours'));
}else {
resolve(calendarResponse);
}
});
});
}