@openinc/parse-server-opendash
Version:
Parse Server Cloud Code for open.INC Stack.
89 lines (88 loc) • 3.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.scheduleToEvent = scheduleToEvent;
async function scheduleToEvent(schedule) {
console.log("Converting Schedule to Event...");
try {
await schedule.fetchWithInclude("template");
}
catch (error) {
console.error("Error fetching template:", error);
}
const template = schedule.get("template");
const templateCron = template?.get("cron");
const scheduleCron = schedule.get("cron");
const start = scheduleCron?.timestamp?.startDate ?? templateCron?.timestamp?.startDate;
const end = scheduleCron?.timestamp?.endDate ?? templateCron?.timestamp?.endDate;
const interval = scheduleCron?.timestamp?.number ?? templateCron?.timestamp?.number;
const unit = scheduleCron?.timestamp?.unit ?? templateCron?.timestamp?.unit;
if (!start || !end || !interval || !unit) {
console.log("Insufficient data to create event from schedule.");
return;
}
const event = {
subject: schedule.get("title"),
start: {
// for single-day/all-day events we use date-only; keep full ISO otherwise
dateTime: start.toISOString().slice(0, 19),
timeZone: "UTC",
},
end: {
dateTime: end.toISOString().slice(0, 19),
timeZone: "UTC",
},
isAllDay: true,
};
// Helper to format date as YYYY-MM-DD for recurrence range
const toDateOnly = (d) => d.toISOString().slice(0, 10);
// determine pattern type and additional fields
let pattern = { interval };
if (unit === "days") {
pattern.type = "daily";
}
else if (unit === "weeks") {
pattern.type = "weekly";
// include the weekday of the start date so weekly recurrences occur on that weekday
const dayNames = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
pattern.daysOfWeek = [dayNames[start.getUTCDay()]];
pattern.firstDayOfWeek = "Monday";
}
else if (unit === "months") {
pattern.type = "absoluteMonthly";
// Graph uses dayOfMonth for absoluteMonthly
pattern.dayOfMonth = start.getUTCDate();
}
else {
// unsupported unit -> skip recurrence
pattern = null;
}
if (pattern) {
event.recurrence = {
pattern,
range: {
type: "endDate",
startDate: toDateOnly(start),
endDate: toDateOnly(end),
},
};
// if event is all-day, set start/end to date-only values (Graph expects dates)
if (event.isAllDay) {
event.start.dateTime = toDateOnly(start);
// Graph's endDate in event object is exclusive for all-day events; keep as provided for recurrence range
// Set end to next day to represent an all-day event that begins and ends the same day
const endForEvent = new Date(start);
endForEvent.setUTCDate(endForEvent.getUTCDate() + 1);
event.end.dateTime = toDateOnly(endForEvent);
}
}
console.log("Schedule converted to Event:", event);
return event;
}