Skip to content

Build your personal knowledge base with Trilium Notes

server-ts: Port services/bulk_actions

Elian Doran6df09cb1576f
diff --git a/src/becca/entities/battribute.ts b/src/becca/entities/battribute.ts
index b29d3e2370..ebd9e74cfe 100644
--- a/src/becca/entities/battribute.ts
+++ b/src/becca/entities/battribute.ts
@@ -222,7 +222,7 @@ class BAttribute extends AbstractBeccaEntity<BAttribute> {
};
}
- createClone(type: AttributeType, name: string, value: string, isInheritable: boolean) {
+ createClone(type: AttributeType, name: string, value: string, isInheritable?: boolean) {
return new BAttribute({
noteId: this.noteId,
type: type,
diff --git a/src/routes/api/bulk_action.js b/src/routes/api/bulk_action.js
index f04b32d613..27955cd358 100644
--- a/src/routes/api/bulk_action.js
+++ b/src/routes/api/bulk_action.js
@@ -1,5 +1,5 @@
const becca = require('../../becca/becca');
-const bulkActionService = require('../../services/bulk_actions.js');
+const bulkActionService = require('../../services/bulk_actions');
function execute(req) {
const {noteIds, includeDescendants} = req.body;
diff --git a/src/routes/api/search.js b/src/routes/api/search.js
index e4b9dec8ff..96428c6af9 100644
--- a/src/routes/api/search.js
+++ b/src/routes/api/search.js
@@ -3,7 +3,7 @@
const becca = require('../../becca/becca');
const SearchContext = require('../../services/search/search_context');
const searchService = require('../../services/search/services/search');
-const bulkActionService = require('../../services/bulk_actions.js');
+const bulkActionService = require('../../services/bulk_actions');
const cls = require('../../services/cls');
const {formatAttrForSearch} = require('../../services/attribute_formatter');
const ValidationError = require('../../errors/validation_error');
diff --git a/src/services/bulk_actions.js b/src/services/bulk_actions.ts
similarity index 80%
rename from src/services/bulk_actions.js
rename to src/services/bulk_actions.ts
index 427b94075b..c91b1c0c97 100644
--- a/src/services/bulk_actions.js
+++ b/src/services/bulk_actions.ts
@@ -1,12 +1,30 @@
-const log = require('./log');
-const revisionService = require('./revisions');
-const becca = require('../becca/becca');
-const cloningService = require('./cloning');
-const branchService = require('./branches');
-const utils = require('./utils');
-const eraseService = require("./erase");
-
-const ACTION_HANDLERS = {
+import log = require('./log');
+import becca = require('../becca/becca');
+import cloningService = require('./cloning');
+import branchService = require('./branches');
+import utils = require('./utils');
+import eraseService = require("./erase");
+import BNote = require('../becca/entities/bnote');
+
+interface Action {
+ labelName: string;
+ labelValue: string;
+ oldLabelName: string;
+ newLabelName: string;
+
+
+ relationName: string;
+ oldRelationName: string;
+ newRelationName: string;
+
+ targetNoteId: string;
+ targetParentNoteId: string;
+ newTitle: string;
+ script: string;
+}
+type ActionHandler = (action: Action, note: BNote) => void;
+
+const ACTION_HANDLERS: Record<string, ActionHandler> = {
addLabel: (action, note) => {
note.addLabel(action.labelName, action.labelValue);
},
@@ -19,7 +37,10 @@ const ACTION_HANDLERS = {
note.deleteNote(deleteId);
},
deleteRevisions: (action, note) => {
- eraseService.eraseRevisions(note.getRevisions().map(rev => rev.revisionId));
+ const revisionIds = note.getRevisions()
+ .map(rev => rev.revisionId)
+ .filter((rev) => !!rev) as string[];
+ eraseService.eraseRevisions(revisionIds);
},
deleteLabel: (action, note) => {
for (const label of note.getOwnedLabels(action.labelName)) {
@@ -107,7 +128,7 @@ const ACTION_HANDLERS = {
}
};
-function getActions(note) {
+function getActions(note: BNote) {
return note.getLabels('action')
.map(actionLabel => {
let action;
@@ -129,7 +150,7 @@ function getActions(note) {
.filter(a => !!a);
}
-function executeActions(note, searchResultNoteIds) {
+function executeActions(note: BNote, searchResultNoteIds: string[]) {
const actions = getActions(note);
for (const resultNoteId of searchResultNoteIds) {
@@ -144,13 +165,13 @@ function executeActions(note, searchResultNoteIds) {
log.info(`Applying action handler to note ${resultNote.noteId}: ${JSON.stringify(action)}`);
ACTION_HANDLERS[action.name](action, resultNote);
- } catch (e) {
+ } catch (e: any) {
log.error(`ExecuteScript search action failed with ${e.message}`);
}
}
}
}
-module.exports = {
+export = {
executeActions
};