added date services to ETAPI

This commit is contained in:
zadam
2022-01-05 19:25:17 +01:00
parent e3114e0602
commit 82b2871a08
8 changed files with 293 additions and 60 deletions

View File

@@ -3,12 +3,12 @@ const utils = require("../../services/utils");
const noteService = require("../../services/notes");
const attributeService = require("../../services/attributes");
const Branch = require("../../becca/entities/branch");
const cls = require("../../services/cls.js");
const sql = require("../../services/sql.js");
const log = require("../../services/log.js");
const cls = require("../../services/cls");
const sql = require("../../services/sql");
const log = require("../../services/log");
const specialNotesService = require("../../services/special_notes");
const dateNotesService = require("../../services/date_notes");
const entityChangesService = require("../../services/entity_changes.js");
const sqlInit = require("../../services/sql_init.js");
const passwordService = require("../../services/password.js");
const GENERIC_CODE = "GENERIC";
@@ -23,21 +23,21 @@ function sendError(res, statusCode, code, message) {
}));
}
function sendNoteNotFoundError(res, noteId) {
return sendError(res, 404, "NOTE_NOT_FOUND",`Note ${noteId} not found`);
}
const sendNoteNotFoundError = (res, noteId) => sendError(res, 404, "NOTE_NOT_FOUND", `Note ${noteId} not found`);
const sendBranchNotFoundError = (res, branchId) => sendError(res, 404, "BRANCH_NOT_FOUND", `Branch ${branchId} not found`);
const sendAttributeNotFoundError = (res, attributeId) => sendError(res, 404, "ATTRIBUTE_NOT_FOUND", `Attribute ${attributeId} not found`);
const sendDateInvalidError = (res, date) => sendError(res, 400, "DATE_INVALID", `Date "${date}" is not valid.`);
const sendMonthInvalidError = (res, month) => sendError(res, 400, "MONTH_INVALID", `Month "${month}" is not valid.`);
const sendYearInvalidError = (res, year) => sendError(res, 400, "YEAR_INVALID", `Year "${year}" is not valid.`);
function sendBranchNotFoundError(res, branchId) {
return sendError(res, 404, "BRANCH_NOT_FOUND",`Branch ${branchId} not found`);
function isValidDate(date) {
if (!/[0-9]{4}-[0-9]{2}-[0-9]{2}/.test(date)) {
return false;
}
return !!Date.parse(date);
}
function sendAttributeNotFoundError(res, attributeId) {
return sendError(res, 404, "ATTRIBUTE_NOT_FOUND",`Attribute ${attributeId} not found`);
}
// TODO:
// * add date/month/year functions
function checkEtapiAuth(req, res, next) {
if (false) {
sendError(res, 401, "NOT_AUTHENTICATED", "Not authenticated");
@@ -71,6 +71,72 @@ function register(router) {
});
}
route('get', '/etapi/inbox/:date', (req, res, next) => {
const {date} = req.params;
if (!isValidDate(date)) {
return sendDateInvalidError(res, date);
}
const note = specialNotesService.getInboxNote(date);
res.json(mapNoteToPojo(note));
});
route('get', '/etapi/date/:date', (req, res, next) => {
const {date} = req.params;
if (!isValidDate(date)) {
return sendDateInvalidError(res, date);
}
const note = dateNotesService.getDateNote(date);
res.json(mapNoteToPojo(note));
});
route('get', '/etapi/week/:date', (req, res, next) => {
const {date} = req.params;
if (!isValidDate(date)) {
return sendDateInvalidError(res, date);
}
const note = dateNotesService.getWeekNote(date);
res.json(mapNoteToPojo(note));
});
route('get', '/etapi/month/:month', (req, res, next) => {
const {month} = req.params;
if (!/[0-9]{4}-[0-9]{2}/.test(month)) {
return sendMonthInvalidError(res, month);
}
const note = dateNotesService.getMonthNote(month);
res.json(mapNoteToPojo(note));
});
route('get', '/etapi/year/:year', (req, res, next) => {
const {year} = req.params;
if (!/[0-9]{4}/.test(year)) {
return sendYearInvalidError(res, year);
}
const note = dateNotesService.getYearNote(year);
res.json(mapNoteToPojo(note));
});
route('get', '/etapi/notes/:noteId', (req, res, next) => {
const {noteId} = req.params;
const note = becca.getNote(noteId);
if (!note) {
return sendNoteNotFoundError(res, noteId);
}
res.json(mapNoteToPojo(note));
});
route('get', '/etapi/notes/:noteId', (req, res, next) => {
const {noteId} = req.params;
const note = becca.getNote(noteId);
@@ -197,6 +263,16 @@ function register(router) {
return sendError(res, 400, GENERIC_CODE, e.message);
}
});
route('post' ,'/etapi/refresh-note-ordering/:parentNoteId', (req, res, next) => {
const {parentNoteId} = req.params;
if (!becca.getNote(parentNoteId)) {
return sendNoteNotFoundError(res, parentNoteId);
}
entityChangesService.addNoteReorderingEntityChange(parentNoteId, "etapi");
});
}
function mapNoteToPojo(note) {