- backend/routes/todos.js: 오늘의 할일 조회 시 KST 기준으로 날짜 계산 - backend/routes/schedules.js: 주간/월간 일정 조회 시 KST 기준으로 날짜 범위 계산 - backend/routes/bible.js: 오늘의 성경 구절 조회 시 KST 기준으로 날짜 계산 - flutter_app: Mock 데이터 사용 시 타임존 관련 주석 추가
106 lines
2.7 KiB
JavaScript
106 lines
2.7 KiB
JavaScript
const express = require("express");
|
|
const { DateTime } = require("luxon");
|
|
const Todo = require("../models/Todo");
|
|
|
|
const router = express.Router();
|
|
|
|
// Korea Standard Time zone
|
|
const KST_TIMEZONE = "Asia/Seoul";
|
|
|
|
// Get day range in Korea Standard Time (Asia/Seoul)
|
|
const getDayRange = () => {
|
|
// Get current time in KST
|
|
const nowKst = DateTime.now().setZone(KST_TIMEZONE);
|
|
|
|
// Start of day in KST (00:00:00.000)
|
|
const startOfDayKst = nowKst.startOf("day");
|
|
|
|
// End of day in KST (23:59:59.999)
|
|
const endOfDayKst = nowKst.endOf("day");
|
|
|
|
// Convert to JavaScript Date objects (UTC) for MongoDB query
|
|
const start = startOfDayKst.toJSDate();
|
|
const end = endOfDayKst.toJSDate();
|
|
|
|
return { start, end };
|
|
};
|
|
|
|
router.get("/", async (req, res) => {
|
|
try {
|
|
const todos = await Todo.find().sort({ dueDate: 1, createdAt: -1 });
|
|
res.json(todos);
|
|
} catch (error) {
|
|
res.status(500).json({ message: "Failed to fetch todos" });
|
|
}
|
|
});
|
|
|
|
router.get("/today", async (req, res) => {
|
|
try {
|
|
const { start } = getDayRange();
|
|
// Show incomplete todos that are:
|
|
// 1. Due today or later (not overdue)
|
|
// 2. Have no due date set
|
|
const todos = await Todo.find({
|
|
completed: false,
|
|
$or: [
|
|
{ dueDate: { $gte: start } },
|
|
{ dueDate: { $exists: false } },
|
|
{ dueDate: null }
|
|
]
|
|
}).sort({
|
|
dueDate: 1,
|
|
createdAt: -1,
|
|
});
|
|
res.json(todos);
|
|
} catch (error) {
|
|
res.status(500).json({ message: "Failed to fetch today todos" });
|
|
}
|
|
});
|
|
|
|
router.post("/", async (req, res) => {
|
|
try {
|
|
const todo = await Todo.create(req.body);
|
|
res.status(201).json(todo);
|
|
} catch (error) {
|
|
res.status(400).json({ message: "Failed to create todo" });
|
|
}
|
|
});
|
|
|
|
router.get("/:id", async (req, res) => {
|
|
try {
|
|
const todo = await Todo.findById(req.params.id);
|
|
if (!todo) {
|
|
return res.status(404).json({ message: "Todo not found" });
|
|
}
|
|
res.json(todo);
|
|
} catch (error) {
|
|
res.status(400).json({ message: "Failed to fetch todo" });
|
|
}
|
|
});
|
|
|
|
router.put("/:id", async (req, res) => {
|
|
try {
|
|
const todo = await Todo.findByIdAndUpdate(req.params.id, req.body, { new: true });
|
|
if (!todo) {
|
|
return res.status(404).json({ message: "Todo not found" });
|
|
}
|
|
res.json(todo);
|
|
} catch (error) {
|
|
res.status(400).json({ message: "Failed to update todo" });
|
|
}
|
|
});
|
|
|
|
router.delete("/:id", async (req, res) => {
|
|
try {
|
|
const todo = await Todo.findByIdAndDelete(req.params.id);
|
|
if (!todo) {
|
|
return res.status(404).json({ message: "Todo not found" });
|
|
}
|
|
res.json({ ok: true });
|
|
} catch (error) {
|
|
res.status(400).json({ message: "Failed to delete todo" });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|