From 4ed88d28e922c5326a8247255ece815f63ac407e Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Thu, 18 Jul 2024 22:41:58 +0300 Subject: [PATCH] server-esm: Fix type errors related to cloning --- src/services/bulk_actions.ts | 2 +- src/services/cloning.ts | 41 ++++++++++++++++++++++++------------ src/services/tree.ts | 8 ++++++- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/services/bulk_actions.ts b/src/services/bulk_actions.ts index 2b81c634d..64c180405 100644 --- a/src/services/bulk_actions.ts +++ b/src/services/bulk_actions.ts @@ -111,7 +111,7 @@ const ACTION_HANDLERS: Record = { res = branchService.moveBranchToNote(note.getParentBranches()[0], action.targetParentNoteId); } - if (!res.success) { + if ("success" in res && !res.success) { log.info(`Moving/cloning note ${note.noteId} to ${action.targetParentNoteId} failed with error ${JSON.stringify(res)}`); } }, diff --git a/src/services/cloning.ts b/src/services/cloning.ts index 939b6bf42..4e81b2666 100644 --- a/src/services/cloning.ts +++ b/src/services/cloning.ts @@ -1,18 +1,28 @@ "use strict"; -const sql = require('./sql'); -const eventChangesService = require('./entity_changes'); -const treeService = require('./tree'); -const BBranch = require('../becca/entities/bbranch'); -const becca = require('../becca/becca'); -const log = require('./log'); +import sql from './sql'; +import eventChangesService from './entity_changes'; +import treeService from './tree'; +import BBranch from '../becca/entities/bbranch'; +import becca from '../becca/becca'; +import log from './log'; -function cloneNoteToParentNote(noteId: string, parentNoteId: string, prefix: string | null = null) { +interface CloneResponse { + success: boolean; + message?: string; + branchId?: string; + notePath?: string; +} + +function cloneNoteToParentNote(noteId: string, parentNoteId: string, prefix: string | null = null): CloneResponse { if (!(noteId in becca.notes) || !(parentNoteId in becca.notes)) { return { success: false, message: 'Note cannot be cloned because either the cloned note or the intended parent is deleted.' }; } const parentNote = becca.getNote(parentNoteId); + if (!parentNote) { + return { success: false, message: 'Note cannot be cloned because the parent note could not be found.' }; + } if (parentNote.type === 'search') { return { @@ -31,7 +41,7 @@ function cloneNoteToParentNote(noteId: string, parentNoteId: string, prefix: str noteId: noteId, parentNoteId: parentNoteId, prefix: prefix, - isExpanded: 0 + isExpanded: false }).save(); log.info(`Cloned note '${noteId}' to a new parent note '${parentNoteId}' with prefix '${prefix}'`); @@ -67,6 +77,9 @@ function ensureNoteIsPresentInParent(noteId: string, parentNoteId: string, prefi const parentNote = becca.getNote(parentNoteId); + if (!parentNote) { + return { branch: null, success: false, message: "Can't find parent note." }; + } if (parentNote.type === 'search') { return { branch: null, success: false, message: "Can't clone into a search note" }; } @@ -81,7 +94,7 @@ function ensureNoteIsPresentInParent(noteId: string, parentNoteId: string, prefi noteId: noteId, parentNoteId: parentNoteId, prefix: prefix, - isExpanded: 0 + isExpanded: false }).save(); log.info(`Ensured note '${noteId}' is in parent note '${parentNoteId}' with prefix '${branch.prefix}'`); @@ -90,7 +103,7 @@ function ensureNoteIsPresentInParent(noteId: string, parentNoteId: string, prefi } function ensureNoteIsAbsentFromParent(noteId: string, parentNoteId: string) { - const branchId = sql.getValue(`SELECT branchId FROM branches WHERE noteId = ? AND parentNoteId = ? AND isDeleted = 0`, [noteId, parentNoteId]); + const branchId = sql.getValue(`SELECT branchId FROM branches WHERE noteId = ? AND parentNoteId = ? AND isDeleted = 0`, [noteId, parentNoteId]); const branch = becca.getBranch(branchId); if (branch) { @@ -137,13 +150,13 @@ function cloneNoteAfter(noteId: string, afterBranchId: string) { if (!(noteId in becca.notes)) { return { success: false, message: `Note to be cloned '${noteId}' is deleted or does not exist.` }; - } else if (!(afterNote.parentNoteId in becca.notes)) { - return { success: false, message: `After note '${afterNote.parentNoteId}' is deleted or does not exist.` }; + } else if (!afterNote || !(afterNote.parentNoteId in becca.notes)) { + return { success: false, message: `After note '${afterNote?.parentNoteId}' is deleted or does not exist.` }; } const parentNote = becca.getNote(afterNote.parentNoteId); - if (parentNote.type === 'search') { + if (!parentNote || parentNote.type === 'search') { return { success: false, message: "Can't clone into a search note" @@ -167,7 +180,7 @@ function cloneNoteAfter(noteId: string, afterBranchId: string) { noteId: noteId, parentNoteId: afterNote.parentNoteId, notePosition: afterNote.notePosition + 10, - isExpanded: 0 + isExpanded: false }).save(); log.info(`Cloned note '${noteId}' into parent note '${afterNote.parentNoteId}' after note '${afterNote.noteId}', branch '${afterBranchId}'`); diff --git a/src/services/tree.ts b/src/services/tree.ts index c3efb8ed0..0ab72e7f7 100644 --- a/src/services/tree.ts +++ b/src/services/tree.ts @@ -7,7 +7,13 @@ import entityChangesService from "./entity_changes.js"; import becca from "../becca/becca.js"; import BNote from "../becca/entities/bnote.js"; -function validateParentChild(parentNoteId: string, childNoteId: string, branchId: string | null = null) { +interface ValidationResponse { + branch: BBranch | null; + success: boolean; + message?: string; +} + +function validateParentChild(parentNoteId: string, childNoteId: string, branchId: string | null = null): ValidationResponse { if (['root', '_hidden', '_share', '_lbRoot', '_lbAvailableLaunchers', '_lbVisibleLaunchers'].includes(childNoteId)) { return { branch: null, success: false, message: `Cannot change this note's location.` }; }