Notes/src/etapi/special_notes.ts

79 lines
2.4 KiB
TypeScript
Raw Normal View History

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";
2024-04-07 18:21:18 +03:00
import { 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.`);
const getMonthInvalidError = (month: string)=> new eu.EtapiError(400, "MONTH_INVALID", `Month "${month}" is not valid.`);
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) {
2022-01-07 19:33:59 +01:00
if (!/[0-9]{4}-[0-9]{2}-[0-9]{2}/.test(date)) {
return false;
}
return !!Date.parse(date);
}
2024-04-07 18:21:18 +03:00
function register(router: Router) {
2022-01-10 17:09:20 +01:00
eu.route(router, 'get', '/etapi/inbox/:date', (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
}
const note = specialNotesService.getInboxNote(date);
res.json(mappers.mapNoteToPojo(note));
});
2022-01-10 17:09:20 +01:00
eu.route(router, 'get', '/etapi/calendar/days/:date', (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
}
2022-01-10 17:09:20 +01:00
const note = dateNotesService.getDayNote(date);
2022-01-07 19:33:59 +01:00
res.json(mappers.mapNoteToPojo(note));
});
2022-01-10 17:09:20 +01:00
eu.route(router, 'get', '/etapi/calendar/weeks/:date', (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
}
const note = dateNotesService.getWeekNote(date);
res.json(mappers.mapNoteToPojo(note));
});
2022-01-10 17:09:20 +01:00
eu.route(router, 'get', '/etapi/calendar/months/:month', (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
}
const note = dateNotesService.getMonthNote(month);
res.json(mappers.mapNoteToPojo(note));
});
2022-01-10 17:09:20 +01: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));
});
}
export default {
2022-01-07 19:33:59 +01:00
register
};