server-ts: Convert services/handlers

This commit is contained in:
Elian Doran 2024-04-04 23:04:54 +03:00
parent 59c533cb6c
commit 92ca32bd70
No known key found for this signature in database
5 changed files with 39 additions and 27 deletions

View File

@ -7,7 +7,7 @@ const compression = require('compression');
const sessionParser = require('./routes/session_parser.js'); const sessionParser = require('./routes/session_parser.js');
const utils = require('./services/utils'); const utils = require('./services/utils');
require('./services/handlers.js'); require('./services/handlers');
require('./becca/becca_loader'); require('./becca/becca_loader');
const app = express(); const app = express();

View File

@ -668,7 +668,7 @@ class BNote extends AbstractBeccaEntity<BNote> {
* @param name - relation name to filter * @param name - relation name to filter
* @returns all note's relations (attributes with type relation), excluding inherited ones * @returns all note's relations (attributes with type relation), excluding inherited ones
*/ */
getOwnedRelations(name: string): BAttribute[] { getOwnedRelations(name?: string | null): BAttribute[] {
return this.getOwnedAttributes(RELATION, name); return this.getOwnedAttributes(RELATION, name);
} }

View File

@ -1,13 +1,18 @@
const eventService = require('./events'); import eventService = require('./events');
const scriptService = require('./script'); import scriptService = require('./script');
const treeService = require('./tree'); import treeService = require('./tree');
const noteService = require('./notes'); import noteService = require('./notes');
const becca = require('../becca/becca'); import becca = require('../becca/becca');
const BAttribute = require('../becca/entities/battribute'); import BAttribute = require('../becca/entities/battribute');
const hiddenSubtreeService = require('./hidden_subtree'); import hiddenSubtreeService = require('./hidden_subtree');
const oneTimeTimer = require('./one_time_timer'); import oneTimeTimer = require('./one_time_timer');
import BNote = require('../becca/entities/bnote');
import AbstractBeccaEntity = require('../becca/entities/abstract_becca_entity');
import { DefinitionObject } from './promoted_attribute_definition_interface';
function runAttachedRelations(note, relationName, originEntity) { type Handler = (definition: DefinitionObject, note: BNote, targetNote: BNote) => void;
function runAttachedRelations(note: BNote, relationName: string, originEntity: AbstractBeccaEntity<any>) {
if (!note) { if (!note) {
return; return;
} }
@ -16,7 +21,7 @@ function runAttachedRelations(note, relationName, originEntity) {
const notesToRun = new Set( const notesToRun = new Set(
note.getRelations(relationName) note.getRelations(relationName)
.map(relation => relation.getTargetNote()) .map(relation => relation.getTargetNote())
.filter(note => !!note) .filter(note => !!note) as BNote[]
); );
for (const noteToRun of notesToRun) { for (const noteToRun of notesToRun) {
@ -84,6 +89,9 @@ eventService.subscribe(eventService.ENTITY_CREATED, ({ entityName, entity }) =>
if (entity.type === 'relation' && entity.name === 'template') { if (entity.type === 'relation' && entity.name === 'template') {
const note = becca.getNote(entity.noteId); const note = becca.getNote(entity.noteId);
if (!note) {
return;
}
const templateNote = becca.getNote(entity.value); const templateNote = becca.getNote(entity.value);
@ -94,6 +102,7 @@ eventService.subscribe(eventService.ENTITY_CREATED, ({ entityName, entity }) =>
const content = note.getContent(); const content = note.getContent();
if (["text", "code"].includes(note.type) if (["text", "code"].includes(note.type)
&& typeof content === "string"
// if the note has already content we're not going to overwrite it with template's one // if the note has already content we're not going to overwrite it with template's one
&& (!content || content.trim().length === 0) && (!content || content.trim().length === 0)
&& templateNote.hasStringContent()) { && templateNote.hasStringContent()) {
@ -138,7 +147,7 @@ eventService.subscribe(eventService.CHILD_NOTE_CREATED, ({ parentNote, childNote
runAttachedRelations(parentNote, 'runOnChildNoteCreation', childNote); runAttachedRelations(parentNote, 'runOnChildNoteCreation', childNote);
}); });
function processInverseRelations(entityName, entity, handler) { function processInverseRelations(entityName: string, entity: BAttribute, handler: Handler) {
if (entityName === 'attributes' && entity.type === 'relation') { if (entityName === 'attributes' && entity.type === 'relation') {
const note = entity.getNote(); const note = entity.getNote();
const relDefinitions = note.getLabels(`relation:${entity.name}`); const relDefinitions = note.getLabels(`relation:${entity.name}`);
@ -149,13 +158,15 @@ function processInverseRelations(entityName, entity, handler) {
if (definition.inverseRelation && definition.inverseRelation.trim()) { if (definition.inverseRelation && definition.inverseRelation.trim()) {
const targetNote = entity.getTargetNote(); const targetNote = entity.getTargetNote();
handler(definition, note, targetNote); if (targetNote) {
handler(definition, note, targetNote);
}
} }
} }
} }
} }
function handleSortedAttribute(entity) { function handleSortedAttribute(entity: BAttribute) {
treeService.sortNotesIfNeeded(entity.noteId); treeService.sortNotesIfNeeded(entity.noteId);
if (entity.isInheritable) { if (entity.isInheritable) {
@ -169,7 +180,7 @@ function handleSortedAttribute(entity) {
} }
} }
function handleMaybeSortingLabel(entity) { function handleMaybeSortingLabel(entity: BAttribute) {
// check if this label is used for sorting, if yes force re-sort // check if this label is used for sorting, if yes force re-sort
const note = becca.notes[entity.noteId]; const note = becca.notes[entity.noteId];
@ -203,7 +214,7 @@ eventService.subscribe(eventService.ENTITY_CHANGED, ({ entityName, entity }) =>
new BAttribute({ new BAttribute({
noteId: targetNote.noteId, noteId: targetNote.noteId,
type: 'relation', type: 'relation',
name: definition.inverseRelation, name: definition.inverseRelation || "",
value: note.noteId, value: note.noteId,
isInheritable: entity.isInheritable isInheritable: entity.isInheritable
}).save(); }).save();
@ -215,7 +226,7 @@ eventService.subscribe(eventService.ENTITY_CHANGED, ({ entityName, entity }) =>
}); });
eventService.subscribe(eventService.ENTITY_DELETED, ({ entityName, entity }) => { eventService.subscribe(eventService.ENTITY_DELETED, ({ entityName, entity }) => {
processInverseRelations(entityName, entity, (definition, note, targetNote) => { processInverseRelations(entityName, entity, (definition: DefinitionObject, note: BNote, targetNote: BNote) => {
// if one inverse attribute is deleted, then the other should be deleted as well // if one inverse attribute is deleted, then the other should be deleted as well
const relations = targetNote.getOwnedRelations(definition.inverseRelation); const relations = targetNote.getOwnedRelations(definition.inverseRelation);
@ -238,6 +249,6 @@ eventService.subscribe(eventService.ENTITY_DELETED, ({ entityName, entity }) =>
} }
}); });
module.exports = { export = {
runAttachedRelations runAttachedRelations
}; };

View File

@ -0,0 +1,8 @@
export interface DefinitionObject {
isPromoted?: boolean;
labelType?: string;
multiplicity?: string;
numberPrecision?: number;
promotedAlias?: string;
inverseRelation?: string;
}

View File

@ -1,11 +1,4 @@
interface DefinitionObject { import { DefinitionObject } from "./promoted_attribute_definition_interface";
isPromoted?: boolean;
labelType?: string;
multiplicity?: string;
numberPrecision?: number;
promotedAlias?: string;
inverseRelation?: string;
}
function parse(value: string): DefinitionObject { function parse(value: string): DefinitionObject {
const tokens = value.split(',').map(t => t.trim()); const tokens = value.split(',').map(t => t.trim());