2018-01-23 21:59:30 -05:00
|
|
|
"use strict";
|
|
|
|
|
2018-04-01 21:27:46 -04:00
|
|
|
const scriptService = require('../../services/script');
|
2018-08-07 12:48:11 +02:00
|
|
|
const attributeService = require('../../services/attributes');
|
2018-03-31 09:07:58 -04:00
|
|
|
const repository = require('../../services/repository');
|
2019-10-20 17:49:58 +02:00
|
|
|
const syncService = require('../../services/sync');
|
2018-01-23 21:59:30 -05:00
|
|
|
|
2018-03-30 17:29:13 -04:00
|
|
|
async function exec(req) {
|
2018-08-17 11:31:42 +02:00
|
|
|
try {
|
|
|
|
const result = await scriptService.executeScript(req.body.script, req.body.params, req.body.startNoteId,
|
|
|
|
req.body.currentNoteId, req.body.originEntityName, req.body.originEntityId);
|
|
|
|
|
2019-10-20 17:49:58 +02:00
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
executionResult: result,
|
|
|
|
maxSyncId: await syncService.getMaxSyncId()
|
|
|
|
};
|
2018-08-17 11:31:42 +02:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
return { success: false, error: e.message };
|
|
|
|
}
|
2018-03-30 17:29:13 -04:00
|
|
|
}
|
2018-03-05 23:19:46 -05:00
|
|
|
|
2018-03-30 17:29:13 -04:00
|
|
|
async function run(req) {
|
2018-03-05 23:19:46 -05:00
|
|
|
const note = await repository.getNote(req.params.noteId);
|
|
|
|
|
2019-01-27 12:28:20 +01:00
|
|
|
const result = await scriptService.executeNote(note, { originEntity: note });
|
2018-03-05 23:19:46 -05:00
|
|
|
|
2018-04-01 12:03:21 -04:00
|
|
|
return { executionResult: result };
|
2018-03-30 17:29:13 -04:00
|
|
|
}
|
2018-01-23 21:59:30 -05:00
|
|
|
|
2020-03-16 21:16:09 +01:00
|
|
|
async function getBundlesWithLabel(label, value) {
|
|
|
|
const notes = await attributeService.getNotesWithLabel(label, value);
|
2018-01-25 23:49:03 -05:00
|
|
|
|
2018-03-30 17:29:13 -04:00
|
|
|
const bundles = [];
|
2018-01-25 23:49:03 -05:00
|
|
|
|
2018-03-04 14:21:11 -05:00
|
|
|
for (const note of notes) {
|
2019-01-13 12:16:05 +01:00
|
|
|
const bundle = await scriptService.getScriptBundleForFrontend(note);
|
2018-03-03 09:11:41 -05:00
|
|
|
|
2018-03-08 23:36:08 -05:00
|
|
|
if (bundle) {
|
2018-03-30 17:29:13 -04:00
|
|
|
bundles.push(bundle);
|
2018-03-08 23:36:08 -05:00
|
|
|
}
|
2018-01-25 23:49:03 -05:00
|
|
|
}
|
|
|
|
|
2018-03-30 17:29:13 -04:00
|
|
|
return bundles;
|
|
|
|
}
|
2018-01-25 23:49:03 -05:00
|
|
|
|
2020-03-16 21:16:09 +01:00
|
|
|
async function getStartupBundles() {
|
|
|
|
return await getBundlesWithLabel("run", "frontendStartup");
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getWidgetBundles() {
|
|
|
|
return await getBundlesWithLabel("widget");
|
|
|
|
}
|
|
|
|
|
2018-07-29 18:39:10 +02:00
|
|
|
async function getRelationBundles(req) {
|
|
|
|
const noteId = req.params.noteId;
|
2018-08-07 13:33:10 +02:00
|
|
|
const note = await repository.getNote(noteId);
|
2018-07-29 18:39:10 +02:00
|
|
|
const relationName = req.params.relationName;
|
|
|
|
|
2018-08-07 13:33:10 +02:00
|
|
|
const attributes = await note.getAttributes();
|
2018-08-07 12:48:11 +02:00
|
|
|
const filtered = attributes.filter(attr => attr.type === 'relation' && attr.name === relationName);
|
|
|
|
const targetNoteIds = filtered.map(relation => relation.value);
|
2018-07-29 18:39:10 +02:00
|
|
|
const uniqueNoteIds = Array.from(new Set(targetNoteIds));
|
|
|
|
|
|
|
|
const bundles = [];
|
|
|
|
|
|
|
|
for (const noteId of uniqueNoteIds) {
|
2019-01-13 11:56:50 +01:00
|
|
|
const note = await repository.getNote(noteId);
|
2019-01-16 22:52:32 +01:00
|
|
|
|
|
|
|
if (!note.isJavaScript() || note.getScriptEnv() !== 'frontend') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-01-13 12:16:05 +01:00
|
|
|
const bundle = await scriptService.getScriptBundleForFrontend(note);
|
2019-01-13 11:56:50 +01:00
|
|
|
|
|
|
|
if (bundle) {
|
|
|
|
bundles.push(bundle);
|
|
|
|
}
|
2018-07-29 18:39:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return bundles;
|
|
|
|
}
|
|
|
|
|
2018-03-30 17:29:13 -04:00
|
|
|
async function getBundle(req) {
|
2019-01-13 11:56:50 +01:00
|
|
|
const note = await repository.getNote(req.params.noteId);
|
|
|
|
|
2019-01-13 12:16:05 +01:00
|
|
|
return await scriptService.getScriptBundleForFrontend(note);
|
2018-03-30 17:29:13 -04:00
|
|
|
}
|
2018-01-23 22:53:27 -05:00
|
|
|
|
2018-03-30 17:29:13 -04:00
|
|
|
module.exports = {
|
|
|
|
exec,
|
|
|
|
run,
|
|
|
|
getStartupBundles,
|
2020-03-16 21:16:09 +01:00
|
|
|
getWidgetBundles,
|
2018-07-29 18:39:10 +02:00
|
|
|
getRelationBundles,
|
2018-03-30 17:29:13 -04:00
|
|
|
getBundle
|
|
|
|
};
|