Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit 4ed88d2

Browse files
committed
server-esm: Fix type errors related to cloning
1 parent 8ac8f6c commit 4ed88d2

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

src/services/bulk_actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ const ACTION_HANDLERS: Record<string, ActionHandler> = {
111111
res = branchService.moveBranchToNote(note.getParentBranches()[0], action.targetParentNoteId);
112112
}
113113

114-
if (!res.success) {
114+
if ("success" in res && !res.success) {
115115
log.info(`Moving/cloning note ${note.noteId} to ${action.targetParentNoteId} failed with error ${JSON.stringify(res)}`);
116116
}
117117
},

src/services/cloning.ts

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
"use strict";
22

3-
const sql = require('./sql');
4-
const eventChangesService = require('./entity_changes');
5-
const treeService = require('./tree');
6-
const BBranch = require('../becca/entities/bbranch');
7-
const becca = require('../becca/becca');
8-
const log = require('./log');
9-
10-
function cloneNoteToParentNote(noteId: string, parentNoteId: string, prefix: string | null = null) {
3+
import sql from './sql';
4+
import eventChangesService from './entity_changes';
5+
import treeService from './tree';
6+
import BBranch from '../becca/entities/bbranch';
7+
import becca from '../becca/becca';
8+
import log from './log';
9+
10+
interface CloneResponse {
11+
success: boolean;
12+
message?: string;
13+
branchId?: string;
14+
notePath?: string;
15+
}
16+
17+
function cloneNoteToParentNote(noteId: string, parentNoteId: string, prefix: string | null = null): CloneResponse {
1118
if (!(noteId in becca.notes) || !(parentNoteId in becca.notes)) {
1219
return { success: false, message: 'Note cannot be cloned because either the cloned note or the intended parent is deleted.' };
1320
}
1421

1522
const parentNote = becca.getNote(parentNoteId);
23+
if (!parentNote) {
24+
return { success: false, message: 'Note cannot be cloned because the parent note could not be found.' };
25+
}
1626

1727
if (parentNote.type === 'search') {
1828
return {
@@ -31,7 +41,7 @@ function cloneNoteToParentNote(noteId: string, parentNoteId: string, prefix: str
3141
noteId: noteId,
3242
parentNoteId: parentNoteId,
3343
prefix: prefix,
34-
isExpanded: 0
44+
isExpanded: false
3545
}).save();
3646

3747
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
6777

6878
const parentNote = becca.getNote(parentNoteId);
6979

80+
if (!parentNote) {
81+
return { branch: null, success: false, message: "Can't find parent note." };
82+
}
7083
if (parentNote.type === 'search') {
7184
return { branch: null, success: false, message: "Can't clone into a search note" };
7285
}
@@ -81,7 +94,7 @@ function ensureNoteIsPresentInParent(noteId: string, parentNoteId: string, prefi
8194
noteId: noteId,
8295
parentNoteId: parentNoteId,
8396
prefix: prefix,
84-
isExpanded: 0
97+
isExpanded: false
8598
}).save();
8699

87100
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
90103
}
91104

92105
function ensureNoteIsAbsentFromParent(noteId: string, parentNoteId: string) {
93-
const branchId = sql.getValue(`SELECT branchId FROM branches WHERE noteId = ? AND parentNoteId = ? AND isDeleted = 0`, [noteId, parentNoteId]);
106+
const branchId = sql.getValue<string>(`SELECT branchId FROM branches WHERE noteId = ? AND parentNoteId = ? AND isDeleted = 0`, [noteId, parentNoteId]);
94107
const branch = becca.getBranch(branchId);
95108

96109
if (branch) {
@@ -137,13 +150,13 @@ function cloneNoteAfter(noteId: string, afterBranchId: string) {
137150

138151
if (!(noteId in becca.notes)) {
139152
return { success: false, message: `Note to be cloned '${noteId}' is deleted or does not exist.` };
140-
} else if (!(afterNote.parentNoteId in becca.notes)) {
141-
return { success: false, message: `After note '${afterNote.parentNoteId}' is deleted or does not exist.` };
153+
} else if (!afterNote || !(afterNote.parentNoteId in becca.notes)) {
154+
return { success: false, message: `After note '${afterNote?.parentNoteId}' is deleted or does not exist.` };
142155
}
143156

144157
const parentNote = becca.getNote(afterNote.parentNoteId);
145158

146-
if (parentNote.type === 'search') {
159+
if (!parentNote || parentNote.type === 'search') {
147160
return {
148161
success: false,
149162
message: "Can't clone into a search note"
@@ -167,7 +180,7 @@ function cloneNoteAfter(noteId: string, afterBranchId: string) {
167180
noteId: noteId,
168181
parentNoteId: afterNote.parentNoteId,
169182
notePosition: afterNote.notePosition + 10,
170-
isExpanded: 0
183+
isExpanded: false
171184
}).save();
172185

173186
log.info(`Cloned note '${noteId}' into parent note '${afterNote.parentNoteId}' after note '${afterNote.noteId}', branch '${afterBranchId}'`);

src/services/tree.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ import entityChangesService from "./entity_changes.js";
77
import becca from "../becca/becca.js";
88
import BNote from "../becca/entities/bnote.js";
99

10-
function validateParentChild(parentNoteId: string, childNoteId: string, branchId: string | null = null) {
10+
interface ValidationResponse {
11+
branch: BBranch | null;
12+
success: boolean;
13+
message?: string;
14+
}
15+
16+
function validateParentChild(parentNoteId: string, childNoteId: string, branchId: string | null = null): ValidationResponse {
1117
if (['root', '_hidden', '_share', '_lbRoot', '_lbAvailableLaunchers', '_lbVisibleLaunchers'].includes(childNoteId)) {
1218
return { branch: null, success: false, message: `Cannot change this note's location.` };
1319
}

0 commit comments

Comments
 (0)