From b50ceaf299dbae15928d8b804ffee191d67ed0e8 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sat, 6 Apr 2024 22:13:47 +0300 Subject: [PATCH] server-ts: Convert routes/api/relation-map --- .../api/{relation-map.js => relation-map.ts} | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) rename src/routes/api/{relation-map.js => relation-map.ts} (73%) diff --git a/src/routes/api/relation-map.js b/src/routes/api/relation-map.ts similarity index 73% rename from src/routes/api/relation-map.js rename to src/routes/api/relation-map.ts index 280ed7f67..c81496349 100644 --- a/src/routes/api/relation-map.js +++ b/src/routes/api/relation-map.ts @@ -1,10 +1,22 @@ -const becca = require('../../becca/becca'); -const sql = require('../../services/sql'); +import { Request } from 'express'; +import becca = require('../../becca/becca'); +import sql = require('../../services/sql'); -function getRelationMap(req) { +interface ResponseData { + noteTitles: Record; + relations: { + attributeId: string, + sourceNoteId: string, + targetNoteId: string, + name: string + }[]; + inverseRelations: Record; +} + +function getRelationMap(req: Request) { const {relationMapNoteId, noteIds} = req.body; - const resp = { + const resp: ResponseData = { // noteId => title noteTitles: {}, relations: [], @@ -14,13 +26,13 @@ function getRelationMap(req) { } }; - if (noteIds.length === 0) { + if (!Array.isArray(noteIds) || noteIds.length === 0) { return resp; } const questionMarks = noteIds.map(noteId => '?').join(','); - const relationMapNote = becca.getNote(relationMapNoteId); + const relationMapNote = becca.getNoteOrThrow(relationMapNoteId); const displayRelationsVal = relationMapNote.getLabelValue('displayRelations'); const displayRelations = !displayRelationsVal ? [] : displayRelationsVal @@ -32,7 +44,7 @@ function getRelationMap(req) { .split(",") .map(token => token.trim()); - const foundNoteIds = sql.getColumn(`SELECT noteId FROM notes WHERE isDeleted = 0 AND noteId IN (${questionMarks})`, noteIds); + const foundNoteIds = sql.getColumn(`SELECT noteId FROM notes WHERE isDeleted = 0 AND noteId IN (${questionMarks})`, noteIds); const notes = becca.getNotes(foundNoteIds); for (const note of notes) { @@ -64,6 +76,6 @@ function getRelationMap(req) { return resp; } -module.exports = { +export = { getRelationMap };