diff --git a/apps/client/src/widgets/view_widgets/table_view/bulk_actions.ts b/apps/client/src/widgets/view_widgets/table_view/bulk_actions.ts index 48262b582..e4f24f4ed 100644 --- a/apps/client/src/widgets/view_widgets/table_view/bulk_actions.ts +++ b/apps/client/src/widgets/view_widgets/table_view/bulk_actions.ts @@ -1,6 +1,4 @@ import { t } from "i18next"; -import attributes from "../../../services/attributes"; -import froca from "../../../services/froca"; import server from "../../../services/server"; import toast from "../../../services/toast"; import ws from "../../../services/ws"; @@ -36,16 +34,10 @@ export async function deleteColumn(parentNoteId: string, type: "label" | "relati } async function executeBulkAction(parentNoteId: string, action: {}) { - const bulkActionNote = await froca.getNote("_bulkAction"); - if (!bulkActionNote) { - console.warn("Bulk action note not found"); - return; - } - - attributes.setLabel("_bulkAction", "action", JSON.stringify(action)); await server.post("bulk-action/execute", { noteIds: [ parentNoteId ], - includeDescendants: true + includeDescendants: true, + actions: [ action ] }); await ws.waitForMaxKnownEntityChangeId(); diff --git a/apps/server/src/routes/api/bulk_action.ts b/apps/server/src/routes/api/bulk_action.ts index 6353322ea..d76ec43ea 100644 --- a/apps/server/src/routes/api/bulk_action.ts +++ b/apps/server/src/routes/api/bulk_action.ts @@ -3,7 +3,7 @@ import becca from "../../becca/becca.js"; import bulkActionService from "../../services/bulk_actions.js"; function execute(req: Request) { - const { noteIds, includeDescendants } = req.body; + const { noteIds, includeDescendants, actions } = req.body; if (!Array.isArray(noteIds)) { throw new Error("noteIds must be an array"); } @@ -12,7 +12,16 @@ function execute(req: Request) { const bulkActionNote = becca.getNoteOrThrow("_bulkAction"); - bulkActionService.executeActionsFromNote(bulkActionNote, affectedNoteIds); + if (actions && actions.length > 0) { + for (const action of actions) { + if (!action.name) { + throw new Error("Action must have a name"); + } + } + bulkActionService.executeActions(actions, affectedNoteIds); + } else { + bulkActionService.executeActionsFromNote(bulkActionNote, affectedNoteIds); + } } function getAffectedNoteCount(req: Request) { diff --git a/apps/server/src/services/bulk_actions.ts b/apps/server/src/services/bulk_actions.ts index a1447c858..71333992c 100644 --- a/apps/server/src/services/bulk_actions.ts +++ b/apps/server/src/services/bulk_actions.ts @@ -50,13 +50,15 @@ type ActionHandlers = { } }; +type BulkActionData = ActionHandlers[T] & { name: T }; + type ActionHandler = (action: T, note: BNote) => void; type ActionHandlerMap = { - [K in keyof ActionHandlers]: ActionHandler; + [K in keyof ActionHandlers]: ActionHandler>; }; -export type BulkAction = { name: keyof ActionHandlers } & ActionHandlers[keyof ActionHandlers]; +export type BulkAction = BulkActionData; const ACTION_HANDLERS: ActionHandlerMap = { addLabel: (action, note) => { @@ -207,7 +209,9 @@ function executeActions(actions: BulkAction[], noteIds: string[] | Set) try { log.info(`Applying action handler to note ${resultNote.noteId}: ${JSON.stringify(action)}`); - ACTION_HANDLERS[action.name](action, resultNote); + const handler = ACTION_HANDLERS[action.name]; + //@ts-ignore + handler(action as BulkAction, resultNote); } catch (e: any) { log.error(`ExecuteScript search action failed with ${e.message}`); }