140 lines
3.3 KiB
JavaScript
140 lines
3.3 KiB
JavaScript
var express = require("express");
|
|
var router = express.Router();
|
|
const moment = require("moment-timezone");
|
|
|
|
// Helper: Group timezones by region for the timezone selection screen
|
|
function getTimezoneGroups() {
|
|
const regions = {
|
|
"USA/CANADA": [
|
|
"America/Los_Angeles",
|
|
"America/Denver",
|
|
"America/New_York",
|
|
"America/Halifax",
|
|
],
|
|
EUROPE: [
|
|
"Europe/Berlin",
|
|
"Europe/Helsinki",
|
|
"Europe/Dublin",
|
|
"Europe/Samara",
|
|
],
|
|
ASIA: ["Asia/Hong_Kong", "Asia/Jakarta", "Asia/Kabul", "Asia/Kathmandu"],
|
|
"SOUTH AMERICA": [
|
|
"America/Bogota",
|
|
"America/Campo_Grande",
|
|
"America/Caracas",
|
|
"America/Lima",
|
|
],
|
|
};
|
|
const groups = {};
|
|
for (const region in regions) {
|
|
groups[region] = regions[region].map((tz) => ({
|
|
name: tz,
|
|
label: tz.replace(/_/g, " ").replace("America/", ""),
|
|
time_am: moment().tz(tz).format("h:mma"),
|
|
time_24: moment().tz(tz).format("HH:mm"),
|
|
}));
|
|
}
|
|
return groups;
|
|
}
|
|
|
|
// Helper: All timezones for dropdown
|
|
function getAllTimezones() {
|
|
return moment.tz.names().map((tz) => ({
|
|
value: tz,
|
|
label: tz,
|
|
}));
|
|
}
|
|
|
|
// Helper: Generate week days and slots
|
|
function getWeekDaysAndSlots(selectedTz, weekOffset = 0) {
|
|
const weekDays = [];
|
|
const today = moment()
|
|
.tz(selectedTz)
|
|
.add(weekOffset * 7, "days");
|
|
for (let i = 0; i < 7; i++) {
|
|
const day = today.clone().add(i, "days");
|
|
weekDays.push({
|
|
label: day.format("dddd"),
|
|
date: day.format("MMMM D"),
|
|
dateISO: day.format("YYYY-MM-DD"),
|
|
slots: [
|
|
"9:00am",
|
|
"9:15am",
|
|
"9:30am",
|
|
"9:45am",
|
|
"10:00am",
|
|
"10:15am",
|
|
"10:30am",
|
|
"10:45am",
|
|
"11:00am",
|
|
"11:15am",
|
|
"11:30am",
|
|
"11:45am",
|
|
"12:00pm",
|
|
"12:15pm",
|
|
"12:30pm",
|
|
"12:45pm",
|
|
"1:00pm",
|
|
"1:15pm",
|
|
],
|
|
});
|
|
}
|
|
return { weekDays, maxSlots: 17 };
|
|
}
|
|
|
|
// Timezone selection screen
|
|
router.get("/timezone", function (req, res) {
|
|
res.render("timezone", {
|
|
timezones: getAllTimezones(),
|
|
timezoneGroups: getTimezoneGroups(),
|
|
});
|
|
});
|
|
|
|
// Calendar slot selection screen
|
|
router.get("/calendar", function (req, res) {
|
|
const selectedTimezone = req.query.tz || "America/New_York";
|
|
const weekOffset = parseInt(req.query.week) || 0;
|
|
const { weekDays, maxSlots } = getWeekDaysAndSlots(
|
|
selectedTimezone,
|
|
weekOffset
|
|
);
|
|
res.render("calendar", {
|
|
selectedTimezone,
|
|
weekDays,
|
|
maxSlots,
|
|
showPrevWeek: weekOffset > 0,
|
|
prevWeek: weekOffset - 1,
|
|
nextWeek: weekOffset + 1,
|
|
});
|
|
});
|
|
|
|
// Booking form screen
|
|
router.get("/book", function (req, res) {
|
|
const selectedTimezone = req.query.tz || "America/New_York";
|
|
const selectedDate = req.query.date || "";
|
|
const selectedTime = req.query.time || "";
|
|
res.render("booking-form", {
|
|
selectedTimezone,
|
|
selectedDate,
|
|
selectedTime,
|
|
});
|
|
});
|
|
|
|
// Booking POST (simulate success)
|
|
router.post("/book", function (req, res) {
|
|
// Here you would save booking info to DB
|
|
res.redirect("/success");
|
|
});
|
|
|
|
// Success screen
|
|
router.get("/success", function (req, res) {
|
|
res.render("success");
|
|
});
|
|
|
|
// Home page redirect to timezone selection
|
|
router.get("/", function (req, res) {
|
|
res.redirect("/timezone");
|
|
});
|
|
|
|
module.exports = router;
|