From 6e042c20e9d7e88eed8a3a84f9e20813f373f092 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sat, 6 Apr 2024 23:34:47 +0300 Subject: [PATCH] server-ts: Convert routes/api/tree --- src/routes/api/{tree.js => tree.ts} | 43 ++++++++++++++++------------- src/routes/routes.js | 2 +- 2 files changed, 25 insertions(+), 20 deletions(-) rename src/routes/api/{tree.js => tree.ts} (75%) diff --git a/src/routes/api/tree.js b/src/routes/api/tree.ts similarity index 75% rename from src/routes/api/tree.js rename to src/routes/api/tree.ts index c8188068b..0dcb42e67 100644 --- a/src/routes/api/tree.js +++ b/src/routes/api/tree.ts @@ -1,16 +1,18 @@ "use strict"; -const becca = require('../../becca/becca'); -const log = require('../../services/log'); -const NotFoundError = require('../../errors/not_found_error'); +import becca = require('../../becca/becca'); +import log = require('../../services/log'); +import NotFoundError = require('../../errors/not_found_error'); +import { Request } from 'express'; +import BNote = require('../../becca/entities/bnote'); -function getNotesAndBranchesAndAttributes(noteIds) { - noteIds = new Set(noteIds); - const collectedNoteIds = new Set(); - const collectedAttributeIds = new Set(); - const collectedBranchIds = new Set(); +function getNotesAndBranchesAndAttributes(_noteIds: string[] | Set) { + const noteIds = new Set(_noteIds); + const collectedNoteIds = new Set(); + const collectedAttributeIds = new Set(); + const collectedBranchIds = new Set(); - function collectEntityIds(note) { + function collectEntityIds(note?: BNote) { if (!note || collectedNoteIds.has(note.noteId)) { return; } @@ -18,15 +20,18 @@ function getNotesAndBranchesAndAttributes(noteIds) { collectedNoteIds.add(note.noteId); for (const branch of note.getParentBranches()) { - collectedBranchIds.add(branch.branchId); + if (branch.branchId) { + collectedBranchIds.add(branch.branchId); + } collectEntityIds(branch.parentNote); } for (const childNote of note.children) { const childBranch = becca.getBranchFromChildAndParent(childNote.noteId, note.noteId); - - collectedBranchIds.add(childBranch.branchId); + if (childBranch && childBranch.branchId) { + collectedBranchIds.add(childBranch.branchId); + } } for (const attr of note.ownedAttributes) { @@ -122,11 +127,11 @@ function getNotesAndBranchesAndAttributes(noteIds) { }; } -function getTree(req) { - const subTreeNoteId = req.query.subTreeNoteId || 'root'; - const collectedNoteIds = new Set([subTreeNoteId]); +function getTree(req: Request) { + const subTreeNoteId = req.query.subTreeNoteId === "string" ? req.query.subTreeNoteId : "" || 'root'; + const collectedNoteIds = new Set([subTreeNoteId]); - function collect(parentNote) { + function collect(parentNote: BNote) { if (!parentNote) { console.trace(parentNote); } @@ -136,7 +141,7 @@ function getTree(req) { const childBranch = becca.getBranchFromChildAndParent(childNote.noteId, parentNote.noteId); - if (childBranch.isExpanded) { + if (childBranch?.isExpanded) { collect(childBranch.childNote); } } @@ -151,11 +156,11 @@ function getTree(req) { return getNotesAndBranchesAndAttributes(collectedNoteIds); } -function load(req) { +function load(req: Request) { return getNotesAndBranchesAndAttributes(req.body.noteIds); } -module.exports = { +export = { getTree, load }; diff --git a/src/routes/routes.js b/src/routes/routes.js index 47bd7fe1a..2e53e4546 100644 --- a/src/routes/routes.js +++ b/src/routes/routes.js @@ -22,7 +22,7 @@ const loginRoute = require('./login.js'); const indexRoute = require('./index.js'); // API routes -const treeApiRoute = require('./api/tree.js'); +const treeApiRoute = require('./api/tree'); const notesApiRoute = require('./api/notes'); const branchesApiRoute = require('./api/branches'); const attachmentsApiRoute = require('./api/attachments');