1
1
"use strict" ;
2
2
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 {
11
18
if ( ! ( noteId in becca . notes ) || ! ( parentNoteId in becca . notes ) ) {
12
19
return { success : false , message : 'Note cannot be cloned because either the cloned note or the intended parent is deleted.' } ;
13
20
}
14
21
15
22
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
+ }
16
26
17
27
if ( parentNote . type === 'search' ) {
18
28
return {
@@ -31,7 +41,7 @@ function cloneNoteToParentNote(noteId: string, parentNoteId: string, prefix: str
31
41
noteId : noteId ,
32
42
parentNoteId : parentNoteId ,
33
43
prefix : prefix ,
34
- isExpanded : 0
44
+ isExpanded : false
35
45
} ) . save ( ) ;
36
46
37
47
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
67
77
68
78
const parentNote = becca . getNote ( parentNoteId ) ;
69
79
80
+ if ( ! parentNote ) {
81
+ return { branch : null , success : false , message : "Can't find parent note." } ;
82
+ }
70
83
if ( parentNote . type === 'search' ) {
71
84
return { branch : null , success : false , message : "Can't clone into a search note" } ;
72
85
}
@@ -81,7 +94,7 @@ function ensureNoteIsPresentInParent(noteId: string, parentNoteId: string, prefi
81
94
noteId : noteId ,
82
95
parentNoteId : parentNoteId ,
83
96
prefix : prefix ,
84
- isExpanded : 0
97
+ isExpanded : false
85
98
} ) . save ( ) ;
86
99
87
100
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
90
103
}
91
104
92
105
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 ] ) ;
94
107
const branch = becca . getBranch ( branchId ) ;
95
108
96
109
if ( branch ) {
@@ -137,13 +150,13 @@ function cloneNoteAfter(noteId: string, afterBranchId: string) {
137
150
138
151
if ( ! ( noteId in becca . notes ) ) {
139
152
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.` } ;
142
155
}
143
156
144
157
const parentNote = becca . getNote ( afterNote . parentNoteId ) ;
145
158
146
- if ( parentNote . type === 'search' ) {
159
+ if ( ! parentNote || parentNote . type === 'search' ) {
147
160
return {
148
161
success : false ,
149
162
message : "Can't clone into a search note"
@@ -167,7 +180,7 @@ function cloneNoteAfter(noteId: string, afterBranchId: string) {
167
180
noteId : noteId ,
168
181
parentNoteId : afterNote . parentNoteId ,
169
182
notePosition : afterNote . notePosition + 10 ,
170
- isExpanded : 0
183
+ isExpanded : false
171
184
} ) . save ( ) ;
172
185
173
186
log . info ( `Cloned note '${ noteId } ' into parent note '${ afterNote . parentNoteId } ' after note '${ afterNote . noteId } ', branch '${ afterBranchId } '` ) ;
0 commit comments