server-esm: Fix type errors related to cloning

This commit is contained in:
Elian Doran 2024-07-18 22:41:58 +03:00
parent 8ac8f6c2df
commit 4ed88d28e9
No known key found for this signature in database
3 changed files with 35 additions and 16 deletions

View File

@ -111,7 +111,7 @@ const ACTION_HANDLERS: Record<string, ActionHandler> = {
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)}`);
}
},

View File

@ -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<string>(`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}'`);

View File

@ -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.` };
}