server-ts: Convert routes/api/tree

This commit is contained in:
Elian Doran 2024-04-06 23:34:47 +03:00
parent 7a98718e64
commit 6e042c20e9
No known key found for this signature in database
2 changed files with 25 additions and 20 deletions

View File

@ -1,16 +1,18 @@
"use strict"; "use strict";
const becca = require('../../becca/becca'); import becca = require('../../becca/becca');
const log = require('../../services/log'); import log = require('../../services/log');
const NotFoundError = require('../../errors/not_found_error'); import NotFoundError = require('../../errors/not_found_error');
import { Request } from 'express';
import BNote = require('../../becca/entities/bnote');
function getNotesAndBranchesAndAttributes(noteIds) { function getNotesAndBranchesAndAttributes(_noteIds: string[] | Set<string>) {
noteIds = new Set(noteIds); const noteIds = new Set(_noteIds);
const collectedNoteIds = new Set(); const collectedNoteIds = new Set<string>();
const collectedAttributeIds = new Set(); const collectedAttributeIds = new Set<string>();
const collectedBranchIds = new Set(); const collectedBranchIds = new Set<string>();
function collectEntityIds(note) { function collectEntityIds(note?: BNote) {
if (!note || collectedNoteIds.has(note.noteId)) { if (!note || collectedNoteIds.has(note.noteId)) {
return; return;
} }
@ -18,15 +20,18 @@ function getNotesAndBranchesAndAttributes(noteIds) {
collectedNoteIds.add(note.noteId); collectedNoteIds.add(note.noteId);
for (const branch of note.getParentBranches()) { for (const branch of note.getParentBranches()) {
collectedBranchIds.add(branch.branchId); if (branch.branchId) {
collectedBranchIds.add(branch.branchId);
}
collectEntityIds(branch.parentNote); collectEntityIds(branch.parentNote);
} }
for (const childNote of note.children) { for (const childNote of note.children) {
const childBranch = becca.getBranchFromChildAndParent(childNote.noteId, note.noteId); const childBranch = becca.getBranchFromChildAndParent(childNote.noteId, note.noteId);
if (childBranch && childBranch.branchId) {
collectedBranchIds.add(childBranch.branchId); collectedBranchIds.add(childBranch.branchId);
}
} }
for (const attr of note.ownedAttributes) { for (const attr of note.ownedAttributes) {
@ -122,11 +127,11 @@ function getNotesAndBranchesAndAttributes(noteIds) {
}; };
} }
function getTree(req) { function getTree(req: Request) {
const subTreeNoteId = req.query.subTreeNoteId || 'root'; const subTreeNoteId = req.query.subTreeNoteId === "string" ? req.query.subTreeNoteId : "" || 'root';
const collectedNoteIds = new Set([subTreeNoteId]); const collectedNoteIds = new Set<string>([subTreeNoteId]);
function collect(parentNote) { function collect(parentNote: BNote) {
if (!parentNote) { if (!parentNote) {
console.trace(parentNote); console.trace(parentNote);
} }
@ -136,7 +141,7 @@ function getTree(req) {
const childBranch = becca.getBranchFromChildAndParent(childNote.noteId, parentNote.noteId); const childBranch = becca.getBranchFromChildAndParent(childNote.noteId, parentNote.noteId);
if (childBranch.isExpanded) { if (childBranch?.isExpanded) {
collect(childBranch.childNote); collect(childBranch.childNote);
} }
} }
@ -151,11 +156,11 @@ function getTree(req) {
return getNotesAndBranchesAndAttributes(collectedNoteIds); return getNotesAndBranchesAndAttributes(collectedNoteIds);
} }
function load(req) { function load(req: Request) {
return getNotesAndBranchesAndAttributes(req.body.noteIds); return getNotesAndBranchesAndAttributes(req.body.noteIds);
} }
module.exports = { export = {
getTree, getTree,
load load
}; };

View File

@ -22,7 +22,7 @@ const loginRoute = require('./login.js');
const indexRoute = require('./index.js'); const indexRoute = require('./index.js');
// API routes // API routes
const treeApiRoute = require('./api/tree.js'); const treeApiRoute = require('./api/tree');
const notesApiRoute = require('./api/notes'); const notesApiRoute = require('./api/notes');
const branchesApiRoute = require('./api/branches'); const branchesApiRoute = require('./api/branches');
const attachmentsApiRoute = require('./api/attachments'); const attachmentsApiRoute = require('./api/attachments');