const express = require("express"); const { DateTime } = require("luxon"); const Schedule = require("../models/Schedule"); const router = express.Router(); // Korea Standard Time zone const KST_TIMEZONE = "Asia/Seoul"; const startOfWeek = () => { // Get current time in KST and find the start of the week (Monday) const nowKst = DateTime.now().setZone(KST_TIMEZONE); // Luxon uses 1 = Monday, 7 = Sunday const startOfWeekKst = nowKst.startOf("week"); // Monday 00:00:00.000 return startOfWeekKst.toJSDate(); }; const endOfWeek = () => { // Get current time in KST and find the end of the week (Sunday) const nowKst = DateTime.now().setZone(KST_TIMEZONE); const endOfWeekKst = nowKst.endOf("week"); // Sunday 23:59:59.999 return endOfWeekKst.toJSDate(); }; const startOfMonth = () => { const nowKst = DateTime.now().setZone(KST_TIMEZONE); return nowKst.startOf("month").toJSDate(); }; const endOfMonth = () => { const nowKst = DateTime.now().setZone(KST_TIMEZONE); return nowKst.endOf("month").toJSDate(); }; router.get("/", async (req, res) => { try { const schedules = await Schedule.find().sort({ startDate: 1 }); res.json(schedules); } catch (error) { res.status(500).json({ message: "Failed to fetch schedules" }); } }); router.get("/week", async (req, res) => { try { const start = startOfWeek(); const end = endOfWeek(); const schedules = await Schedule.find({ startDate: { $lte: end }, endDate: { $gte: start }, }).sort({ startDate: 1 }); res.json(schedules); } catch (error) { res.status(500).json({ message: "Failed to fetch weekly schedules" }); } }); router.get("/month", async (req, res) => { try { const start = startOfMonth(); const end = endOfMonth(); const schedules = await Schedule.find({ startDate: { $lte: end }, endDate: { $gte: start }, }).sort({ startDate: 1 }); res.json(schedules); } catch (error) { res.status(500).json({ message: "Failed to fetch monthly schedules" }); } }); router.post("/", async (req, res) => { try { const schedule = await Schedule.create(req.body); res.status(201).json(schedule); } catch (error) { res.status(400).json({ message: "Failed to create schedule" }); } }); router.get("/:id", async (req, res) => { try { const schedule = await Schedule.findById(req.params.id); if (!schedule) { return res.status(404).json({ message: "Schedule not found" }); } res.json(schedule); } catch (error) { res.status(400).json({ message: "Failed to fetch schedule" }); } }); router.put("/:id", async (req, res) => { try { const schedule = await Schedule.findByIdAndUpdate(req.params.id, req.body, { new: true, }); if (!schedule) { return res.status(404).json({ message: "Schedule not found" }); } res.json(schedule); } catch (error) { res.status(400).json({ message: "Failed to update schedule" }); } }); router.delete("/:id", async (req, res) => { try { const schedule = await Schedule.findByIdAndDelete(req.params.id); if (!schedule) { return res.status(404).json({ message: "Schedule not found" }); } res.json({ ok: true }); } catch (error) { res.status(400).json({ message: "Failed to delete schedule" }); } }); module.exports = router;