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