From 9e96272eb39534902138f2fc7484a84a08be8dc1 Mon Sep 17 00:00:00 2001 From: azivner Date: Fri, 10 Aug 2018 14:31:57 +0200 Subject: [PATCH] fixed runOnAttributeChange event --- src/entities/entity_constructor.js | 22 +++++++++++-------- src/public/javascripts/services/script_api.js | 2 +- src/routes/api/script.js | 2 +- src/services/attributes.js | 3 ++- src/services/handlers.js | 2 +- src/services/note_cache.js | 2 -- src/services/repository.js | 4 ++-- src/services/scheduler.js | 2 +- 8 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/entities/entity_constructor.js b/src/entities/entity_constructor.js index 8adf645a0..da6bb09de 100644 --- a/src/entities/entity_constructor.js +++ b/src/entities/entity_constructor.js @@ -10,18 +10,22 @@ const Option = require('../entities/option'); const repository = require('../services/repository'); const TABLE_NAME_TO_ENTITY = { - "attributes": Attribute.constructor, - "images": Image.constructor, - "note_images": NoteImage.constructor, - "branches": Branch.constructor, - "notes": Note.constructor, - "note_revisions": NoteRevision.constructor, - "recent_notes": RecentNote.constructor, - "options": Option.constructor, - "api_tokens": ApiToken.constructor, + "attributes": Attribute, + "images": Image, + "note_images": NoteImage, + "branches": Branch, + "notes": Note, + "note_revisions": NoteRevision, + "recent_notes": RecentNote, + "options": Option, + "api_tokens": ApiToken }; function getEntityFromTableName(tableName) { + if (!(tableName in TABLE_NAME_TO_ENTITY)) { + throw new Error(`Entity for table ${tableName} not found!`); + } + return TABLE_NAME_TO_ENTITY[tableName]; } diff --git a/src/public/javascripts/services/script_api.js b/src/public/javascripts/services/script_api.js index 0e59b645e..dba80a42c 100644 --- a/src/public/javascripts/services/script_api.js +++ b/src/public/javascripts/services/script_api.js @@ -44,7 +44,7 @@ function ScriptApi(startNote, currentNote, originEntity = null) { params: prepareParams(params), startNoteId: startNote.noteId, currentNoteId: currentNote.noteId, - originEntityName: originEntity ? originEntity.constructor.tableName() : null, + originEntityName: originEntity ? originEntity.constructor.tableName : null, originEntityId: originEntity ? originEntity.noteId : null }); diff --git a/src/routes/api/script.js b/src/routes/api/script.js index da3d5a554..6e998444a 100644 --- a/src/routes/api/script.js +++ b/src/routes/api/script.js @@ -14,7 +14,7 @@ async function exec(req) { async function run(req) { const note = await repository.getNote(req.params.noteId); - const result = await scriptService.executeNote(req, note); + const result = await scriptService.executeNote(note, note); return { executionResult: result }; } diff --git a/src/services/attributes.js b/src/services/attributes.js index ebebc02ee..6cfea1c86 100644 --- a/src/services/attributes.js +++ b/src/services/attributes.js @@ -19,7 +19,8 @@ const BUILTIN_ATTRIBUTES = [ // relation names { type: 'relation', name: 'runOnNoteView' }, - { type: 'relation', name: 'runOnNoteTitleChange' } + { type: 'relation', name: 'runOnNoteTitleChange' }, + { type: 'relation', name: 'runOnAttributeChange' } ]; async function getNotesWithLabel(name, value) { diff --git a/src/services/handlers.js b/src/services/handlers.js index 627658c3d..0426a3554 100644 --- a/src/services/handlers.js +++ b/src/services/handlers.js @@ -11,7 +11,7 @@ async function runAttachedRelations(note, relationName, originEntity) { for (const relation of runRelations) { const scriptNote = await relation.getTargetNote(); - await scriptService.executeNote(scriptNote, scriptNote, originEntity); + await scriptService.executeNote(scriptNote, originEntity); } } diff --git a/src/services/note_cache.js b/src/services/note_cache.js index ecd8f6e75..43d5bd147 100644 --- a/src/services/note_cache.js +++ b/src/services/note_cache.js @@ -273,8 +273,6 @@ eventService.subscribe(eventService.ENTITY_CHANGED, async ({entityName, entityId const hideLabel = await repository.getEntity(`SELECT * FROM attributes WHERE isDeleted = 0 AND type = 'label' AND name = 'archived' AND noteId = ?`, [attribute.noteId]); - console.log(hideLabel); - if (hideLabel) { archived[attribute.noteId] = hideLabel.isInheritable ? 1 : 0; } diff --git a/src/services/repository.js b/src/services/repository.js index b6418940c..8fc95a1e8 100644 --- a/src/services/repository.js +++ b/src/services/repository.js @@ -10,9 +10,9 @@ async function setEntityConstructor(constructor) { } async function getEntityFromName(entityName, entityId) { - const entityConstructor = entityConstructor.getEntityFromTableName(entityName); + const constructor = entityConstructor.getEntityFromTableName(entityName); - return await getEntity(`SELECT * FROM ${entityConstructor.tableName()} WHERE ${entityConstructor.primaryKeyName()} = ?`, [entityId]); + return await getEntity(`SELECT * FROM ${constructor.tableName} WHERE ${constructor.primaryKeyName} = ?`, [entityId]); } async function getEntities(query, params = []) { diff --git a/src/services/scheduler.js b/src/services/scheduler.js index c671278af..78e02cd9e 100644 --- a/src/services/scheduler.js +++ b/src/services/scheduler.js @@ -17,7 +17,7 @@ async function runNotesWithLabel(runAttrValue) { AND notes.isDeleted = 0`, [runAttrValue]); for (const note of notes) { - scriptService.executeNote(note); + scriptService.executeNote(note, note); } }