chore: 🤖 improve code

This commit is contained in:
Jin 2025-04-03 22:07:11 +02:00
parent 79a7513325
commit eb514ad4e3

View File

@ -11,62 +11,50 @@ const getMonthInvalidError = (month: string) => new eu.EtapiError(400, "MONTH_IN
const getYearInvalidError = (year: string) => new eu.EtapiError(400, "YEAR_INVALID", `Year "${year}" is not valid.`);
function isValidDate(date: string) {
if (!/[0-9]{4}-[0-9]{2}-[0-9]{2}/.test(date)) {
return false;
}
return !!Date.parse(date);
}
function isValidWeek(week: string) {
if (!/[0-9]{4}-W[0-9]{2}/.test(week)) {
return false;
}
return true;
return /[0-9]{4}-[0-9]{2}-[0-9]{2}/.test(date) && !!Date.parse(date);
}
function register(router: Router) {
eu.route(router, "get", "/etapi/inbox/:date", (req, res, next) => {
eu.route(router, "get", "/etapi/inbox/:date", async (req, res, next) => {
const { date } = req.params;
if (!isValidDate(date)) {
throw getDateInvalidError(date);
}
const note = await specialNotesService.getInboxNote(date);
res.json(mappers.mapNoteToPojo(note));
});
eu.route(router, "get", "/etapi/calendar/days/:date", async (req, res, next) => {
const { date } = req.params;
if (!isValidDate(date)) {
throw getDateInvalidError(date);
}
const note = specialNotesService.getInboxNote(date);
const note = await dateNotesService.getDayNote(date);
res.json(mappers.mapNoteToPojo(note));
});
eu.route(router, "get", "/etapi/calendar/days/:date", (req, res, next) => {
eu.route(router, "get", "/etapi/calendar/week-first-day/:date", async (req, res, next) => {
const { date } = req.params;
if (!isValidDate(date)) {
throw getDateInvalidError(date);
}
const note = dateNotesService.getDayNote(date);
const note = await dateNotesService.getWeekFirstDayNote(date);
res.json(mappers.mapNoteToPojo(note));
});
eu.route(router, "get", "/etapi/calendar/week-first-day/:date", (req, res, next) => {
const { date } = req.params;
if (!isValidDate(date)) {
throw getDateInvalidError(date);
}
const note = dateNotesService.getWeekFirstDayNote(date);
res.json(mappers.mapNoteToPojo(note));
});
eu.route(router, "get", "/etapi/calendar/weeks/:week", (req, res, next) => {
eu.route(router, "get", "/etapi/calendar/weeks/:week", async (req, res, next) => {
const { week } = req.params;
if (!isValidWeek(week)) {
if (!/[0-9]{4}-W[0-9]{2}/.test(week)) {
throw getWeekInvalidError(week);
}
const note = dateNotesService.getWeekNote(week);
const note = await dateNotesService.getWeekNote(week);
if (!note) {
throw getWeekNotFoundError(week);
@ -75,14 +63,14 @@ function register(router: Router) {
res.json(mappers.mapNoteToPojo(note));
});
eu.route(router, "get", "/etapi/calendar/months/:month", (req, res, next) => {
eu.route(router, "get", "/etapi/calendar/months/:month", async (req, res, next) => {
const { month } = req.params;
if (!/[0-9]{4}-[0-9]{2}/.test(month)) {
throw getMonthInvalidError(month);
}
const note = dateNotesService.getMonthNote(month);
const note = await dateNotesService.getMonthNote(month);
res.json(mappers.mapNoteToPojo(note));
});