Notes/src/etapi/branches.js

81 lines
2.5 KiB
JavaScript
Raw Normal View History

2022-01-10 17:09:20 +01:00
const becca = require("../becca/becca");
const eu = require("./etapi_utils");
2022-01-07 19:33:59 +01:00
const mappers = require("./mappers");
const Branch = require("../becca/entities/branch");
const noteService = require("../services/notes");
const TaskContext = require("../services/task_context");
const entityChangesService = require("../services/entity_changes");
2022-01-10 17:09:20 +01:00
const validators = require("./validators");
2022-01-07 19:33:59 +01:00
function register(router) {
2022-01-10 17:09:20 +01:00
eu.route(router, 'get', '/etapi/branches/:branchId', (req, res, next) => {
const branch = eu.getAndCheckBranch(req.params.branchId);
2022-01-07 19:33:59 +01:00
res.json(mappers.mapBranchToPojo(branch));
});
2022-01-10 17:09:20 +01:00
eu.route(router, 'post' ,'/etapi/branches', (req, res, next) => {
2022-01-07 19:33:59 +01:00
const params = req.body;
2022-01-10 17:09:20 +01:00
eu.getAndCheckNote(params.noteId);
eu.getAndCheckNote(params.parentNoteId);
2022-01-07 19:33:59 +01:00
const existing = becca.getBranchFromChildAndParent(params.noteId, params.parentNoteId);
if (existing) {
existing.notePosition = params.notePosition;
existing.prefix = params.prefix;
existing.save();
return res.json(mappers.mapBranchToPojo(existing));
}
try {
const branch = new Branch(params).save();
res.json(mappers.mapBranchToPojo(branch));
}
catch (e) {
2022-01-10 17:09:20 +01:00
throw new eu.EtapiError(400, eu.GENERIC_CODE, e.message);
2022-01-07 19:33:59 +01:00
}
});
const ALLOWED_PROPERTIES_FOR_PATCH = {
2022-01-07 23:06:04 +01:00
'notePosition': validators.isInteger,
'prefix': validators.isStringOrNull,
2022-01-07 19:33:59 +01:00
'isExpanded': validators.isBoolean
};
2022-01-10 17:09:20 +01:00
eu.route(router, 'patch' ,'/etapi/branches/:branchId', (req, res, next) => {
const branch = eu.getAndCheckBranch(req.params.branchId);
2022-01-07 19:33:59 +01:00
2022-01-10 17:09:20 +01:00
eu.validateAndPatch(branch, req.body, ALLOWED_PROPERTIES_FOR_PATCH);
2022-01-07 19:33:59 +01:00
res.json(mappers.mapBranchToPojo(branch));
});
2022-01-10 17:09:20 +01:00
eu.route(router, 'delete' ,'/etapi/branches/:branchId', (req, res, next) => {
2022-01-07 19:33:59 +01:00
const branch = becca.getBranch(req.params.branchId);
2022-01-08 12:01:54 +01:00
if (!branch || branch.isDeleted) {
2022-01-07 19:33:59 +01:00
return res.sendStatus(204);
}
noteService.deleteBranch(branch, null, new TaskContext('no-progress-reporting'));
res.sendStatus(204);
});
2022-01-10 17:09:20 +01:00
eu.route(router, 'post' ,'/etapi/refresh-note-ordering/:parentNoteId', (req, res, next) => {
eu.getAndCheckNote(req.params.parentNoteId);
2022-01-07 19:33:59 +01:00
entityChangesService.addNoteReorderingEntityChange(req.params.parentNoteId, "etapi");
2022-01-07 23:06:04 +01:00
res.sendStatus(204);
2022-01-07 19:33:59 +01:00
});
}
module.exports = {
register
2022-01-07 23:06:04 +01:00
};