2024-07-18 21:35:17 +03:00
|
|
|
import specialNotesService from "../services/special_notes.js";
|
|
|
|
import dateNotesService from "../services/date_notes.js";
|
|
|
|
import eu from "./etapi_utils.js";
|
|
|
|
import mappers from "./mappers.js";
|
2025-01-13 23:18:10 +02:00
|
|
|
import type { Router } from "express";
|
2022-01-07 19:33:59 +01:00
|
|
|
|
2024-04-07 18:21:18 +03:00
|
|
|
const getDateInvalidError = (date: string) => new eu.EtapiError(400, "DATE_INVALID", `Date "${date}" is not valid.`);
|
2025-04-01 17:10:35 +02:00
|
|
|
const getWeekInvalidError = (week: string) => new eu.EtapiError(400, "WEEK_INVALID", `Week "${week}" is not valid.`);
|
|
|
|
const getWeekNotFoundError = (week: string) => new eu.EtapiError(404, "WEEK_NOT_FOUND", `Week "${week}" not found. Check if week note is enabled.`);
|
2025-01-09 18:07:02 +02:00
|
|
|
const getMonthInvalidError = (month: string) => new eu.EtapiError(400, "MONTH_INVALID", `Month "${month}" is not valid.`);
|
2024-04-07 18:21:18 +03:00
|
|
|
const getYearInvalidError = (year: string) => new eu.EtapiError(400, "YEAR_INVALID", `Year "${year}" is not valid.`);
|
2022-01-07 19:33:59 +01:00
|
|
|
|
2024-04-07 18:21:18 +03:00
|
|
|
function isValidDate(date: string) {
|
2025-04-03 22:07:11 +02:00
|
|
|
return /[0-9]{4}-[0-9]{2}-[0-9]{2}/.test(date) && !!Date.parse(date);
|
2025-04-01 17:10:35 +02:00
|
|
|
}
|
|
|
|
|
2024-04-07 18:21:18 +03:00
|
|
|
function register(router: Router) {
|
2025-04-03 22:07:11 +02:00
|
|
|
eu.route(router, "get", "/etapi/inbox/:date", async (req, res, next) => {
|
2024-04-03 23:05:06 +03:00
|
|
|
const { date } = req.params;
|
2022-01-07 19:33:59 +01:00
|
|
|
|
|
|
|
if (!isValidDate(date)) {
|
2023-04-08 19:51:39 +08:00
|
|
|
throw getDateInvalidError(date);
|
2022-01-07 19:33:59 +01:00
|
|
|
}
|
2025-04-03 22:07:11 +02:00
|
|
|
const note = await specialNotesService.getInboxNote(date);
|
2022-01-07 19:33:59 +01:00
|
|
|
res.json(mappers.mapNoteToPojo(note));
|
|
|
|
});
|
|
|
|
|
2025-04-03 22:07:11 +02:00
|
|
|
eu.route(router, "get", "/etapi/calendar/days/:date", async (req, res, next) => {
|
2024-04-03 23:05:06 +03:00
|
|
|
const { date } = req.params;
|
2022-01-07 19:33:59 +01:00
|
|
|
|
|
|
|
if (!isValidDate(date)) {
|
2023-04-08 19:51:39 +08:00
|
|
|
throw getDateInvalidError(date);
|
2022-01-07 19:33:59 +01:00
|
|
|
}
|
|
|
|
|
2025-04-03 22:07:11 +02:00
|
|
|
const note = await dateNotesService.getDayNote(date);
|
2022-01-07 19:33:59 +01:00
|
|
|
res.json(mappers.mapNoteToPojo(note));
|
|
|
|
});
|
|
|
|
|
2025-04-03 22:07:11 +02:00
|
|
|
eu.route(router, "get", "/etapi/calendar/week-first-day/:date", async (req, res, next) => {
|
2024-04-03 23:05:06 +03:00
|
|
|
const { date } = req.params;
|
2022-01-07 19:33:59 +01:00
|
|
|
|
|
|
|
if (!isValidDate(date)) {
|
2023-04-08 19:51:39 +08:00
|
|
|
throw getDateInvalidError(date);
|
2022-01-07 19:33:59 +01:00
|
|
|
}
|
|
|
|
|
2025-04-03 22:07:11 +02:00
|
|
|
const note = await dateNotesService.getWeekFirstDayNote(date);
|
2022-01-07 19:33:59 +01:00
|
|
|
res.json(mappers.mapNoteToPojo(note));
|
|
|
|
});
|
|
|
|
|
2025-04-03 22:07:11 +02:00
|
|
|
eu.route(router, "get", "/etapi/calendar/weeks/:week", async (req, res, next) => {
|
2025-04-01 17:10:35 +02:00
|
|
|
const { week } = req.params;
|
|
|
|
|
2025-04-03 22:07:11 +02:00
|
|
|
if (!/[0-9]{4}-W[0-9]{2}/.test(week)) {
|
2025-04-01 17:10:35 +02:00
|
|
|
throw getWeekInvalidError(week);
|
|
|
|
}
|
|
|
|
|
2025-04-03 22:07:11 +02:00
|
|
|
const note = await dateNotesService.getWeekNote(week);
|
2025-04-01 17:10:35 +02:00
|
|
|
|
|
|
|
if (!note) {
|
|
|
|
throw getWeekNotFoundError(week);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json(mappers.mapNoteToPojo(note));
|
|
|
|
});
|
|
|
|
|
2025-04-03 22:07:11 +02:00
|
|
|
eu.route(router, "get", "/etapi/calendar/months/:month", async (req, res, next) => {
|
2024-04-03 23:05:06 +03:00
|
|
|
const { month } = req.params;
|
2022-01-07 19:33:59 +01:00
|
|
|
|
|
|
|
if (!/[0-9]{4}-[0-9]{2}/.test(month)) {
|
2023-04-08 19:51:39 +08:00
|
|
|
throw getMonthInvalidError(month);
|
2022-01-07 19:33:59 +01:00
|
|
|
}
|
|
|
|
|
2025-04-03 22:07:11 +02:00
|
|
|
const note = await dateNotesService.getMonthNote(month);
|
2022-01-07 19:33:59 +01:00
|
|
|
res.json(mappers.mapNoteToPojo(note));
|
|
|
|
});
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
eu.route(router, "get", "/etapi/calendar/years/:year", (req, res, next) => {
|
2024-04-03 23:05:06 +03:00
|
|
|
const { year } = req.params;
|
2022-01-07 19:33:59 +01:00
|
|
|
|
|
|
|
if (!/[0-9]{4}/.test(year)) {
|
2023-04-08 19:51:39 +08:00
|
|
|
throw getYearInvalidError(year);
|
2022-01-07 19:33:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const note = dateNotesService.getYearNote(year);
|
|
|
|
res.json(mappers.mapNoteToPojo(note));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-07-18 21:42:44 +03:00
|
|
|
export default {
|
2022-01-07 19:33:59 +01:00
|
|
|
register
|
2023-11-22 19:34:48 +01:00
|
|
|
};
|