diff --git a/src/becca/becca-interface.ts b/src/becca/becca-interface.ts index 48a096eda..7fa3ca60f 100644 --- a/src/becca/becca-interface.ts +++ b/src/becca/becca-interface.ts @@ -129,7 +129,7 @@ export default class Becca { return this.branches[branchId]; } - getBranchOrThrow(branchId: string): BBranch | null { + getBranchOrThrow(branchId: string): BBranch { const branch = this.getBranch(branchId); if (!branch) { throw new NotFoundError(`Branch '${branchId}' was not found in becca.`); diff --git a/src/becca/entities/bnote.ts b/src/becca/entities/bnote.ts index 54bb0c427..8542070c2 100644 --- a/src/becca/entities/bnote.ts +++ b/src/becca/entities/bnote.ts @@ -203,9 +203,9 @@ class BNote extends AbstractBeccaEntity { return this.children && this.children.length > 0; } - getChildBranches(): (BBranch | null)[] { + getChildBranches(): BBranch[] { return this.children - .map(childNote => this.becca.getBranchFromChildAndParent(childNote.noteId, this.noteId)); + .map(childNote => this.becca.getBranchFromChildAndParent(childNote.noteId, this.noteId)) as BBranch[]; } /* diff --git a/src/routes/api/branches.js b/src/routes/api/branches.ts similarity index 83% rename from src/routes/api/branches.js rename to src/routes/api/branches.ts index 4638fb63e..8c7562128 100644 --- a/src/routes/api/branches.js +++ b/src/routes/api/branches.ts @@ -1,23 +1,24 @@ "use strict"; -const sql = require('../../services/sql'); -const utils = require('../../services/utils'); -const entityChangesService = require('../../services/entity_changes'); -const treeService = require('../../services/tree'); -const eraseService = require('../../services/erase'); -const becca = require('../../becca/becca'); -const TaskContext = require('../../services/task_context'); -const branchService = require('../../services/branches'); -const log = require('../../services/log'); -const ValidationError = require('../../errors/validation_error'); -const eventService = require("../../services/events"); +import sql = require('../../services/sql'); +import utils = require('../../services/utils'); +import entityChangesService = require('../../services/entity_changes'); +import treeService = require('../../services/tree'); +import eraseService = require('../../services/erase'); +import becca = require('../../becca/becca'); +import TaskContext = require('../../services/task_context'); +import branchService = require('../../services/branches'); +import log = require('../../services/log'); +import ValidationError = require('../../errors/validation_error'); +import eventService = require("../../services/events"); +import { Request } from 'express'; /** * Code in this file deals with moving and cloning branches. The relationship between note and parent note is unique * for not deleted branches. There may be multiple deleted note-parent note relationships. */ -function moveBranchToParent(req) { +function moveBranchToParent(req: Request) { const {branchId, parentBranchId} = req.params; const branchToMove = becca.getBranch(branchId); @@ -30,7 +31,7 @@ function moveBranchToParent(req) { return branchService.moveBranchToBranch(branchToMove, targetParentBranch, branchId); } -function moveBranchBeforeNote(req) { +function moveBranchBeforeNote(req: Request) { const {branchId, beforeBranchId} = req.params; const branchToMove = becca.getBranchOrThrow(branchId); @@ -51,7 +52,7 @@ function moveBranchBeforeNote(req) { [beforeBranch.parentNoteId, originalBeforeNotePosition]); // also need to update becca positions - const parentNote = becca.getNote(beforeBranch.parentNoteId); + const parentNote = becca.getNoteOrThrow(beforeBranch.parentNoteId); for (const childBranch of parentNote.getChildBranches()) { if (childBranch.notePosition >= originalBeforeNotePosition) { @@ -80,11 +81,11 @@ function moveBranchBeforeNote(req) { return { success: true }; } -function moveBranchAfterNote(req) { +function moveBranchAfterNote(req: Request) { const {branchId, afterBranchId} = req.params; - const branchToMove = becca.getBranch(branchId); - const afterNote = becca.getBranch(afterBranchId); + const branchToMove = becca.getBranchOrThrow(branchId); + const afterNote = becca.getBranchOrThrow(afterBranchId); const validationResult = treeService.validateParentChild(afterNote.parentNoteId, branchToMove.noteId, branchId); @@ -100,7 +101,7 @@ function moveBranchAfterNote(req) { [afterNote.parentNoteId, originalAfterNotePosition]); // also need to update becca positions - const parentNote = becca.getNote(afterNote.parentNoteId); + const parentNote = becca.getNoteOrThrow(afterNote.parentNoteId); for (const childBranch of parentNote.getChildBranches()) { if (childBranch.notePosition > originalAfterNotePosition) { @@ -131,7 +132,7 @@ function moveBranchAfterNote(req) { return { success: true }; } -function setExpanded(req) { +function setExpanded(req: Request) { const {branchId} = req.params; const expanded = parseInt(req.params.expanded); @@ -153,11 +154,11 @@ function setExpanded(req) { } } -function setExpandedForSubtree(req) { +function setExpandedForSubtree(req: Request) { const {branchId} = req.params; const expanded = parseInt(req.params.expanded); - let branchIds = sql.getColumn(` + let branchIds = sql.getColumn(` WITH RECURSIVE tree(branchId, noteId) AS ( SELECT branchId, noteId FROM branches WHERE branchId = ? @@ -186,12 +187,12 @@ function setExpandedForSubtree(req) { }; } -function deleteBranch(req) { +function deleteBranch(req: Request) { const last = req.query.last === 'true'; const eraseNotes = req.query.eraseNotes === 'true'; const branch = becca.getBranchOrThrow(req.params.branchId); - const taskContext = TaskContext.getInstance(req.query.taskId, 'deleteNotes'); + const taskContext = TaskContext.getInstance(req.query.taskId as string, 'deleteNotes'); const deleteId = utils.randomString(10); let noteDeleted; @@ -214,16 +215,16 @@ function deleteBranch(req) { }; } -function setPrefix(req) { +function setPrefix(req: Request) { const branchId = req.params.branchId; const prefix = utils.isEmptyOrWhitespace(req.body.prefix) ? null : req.body.prefix; - const branch = becca.getBranch(branchId); + const branch = becca.getBranchOrThrow(branchId); branch.prefix = prefix; branch.save(); } -module.exports = { +export = { moveBranchToParent, moveBranchBeforeNote, moveBranchAfterNote, diff --git a/src/services/branches.ts b/src/services/branches.ts index 7ee32c949..8e6971560 100644 --- a/src/services/branches.ts +++ b/src/services/branches.ts @@ -27,7 +27,8 @@ function moveBranchToNote(branchToMove: BBranch, targetParentNoteId: string) { }; } -function moveBranchToBranch(branchToMove: BBranch, targetParentBranch: BBranch) { +function moveBranchToBranch(branchToMove: BBranch, targetParentBranch: BBranch, branchId: string) { + // TODO: Unused branch ID argument. const res = moveBranchToNote(branchToMove, targetParentBranch.noteId); if (!("success" in res) || !res.success) {