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 utils = require('./services/utils');
require('./services/handlers.js');
require('./services/handlers');
require('./becca/becca_loader');
const app = express();

View File

@ -668,7 +668,7 @@ class BNote extends AbstractBeccaEntity<BNote> {
* @param name - relation name to filter
* @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);
}

View File

@ -1,13 +1,18 @@
const eventService = require('./events');
const scriptService = require('./script');
const treeService = require('./tree');
const noteService = require('./notes');
const becca = require('../becca/becca');
const BAttribute = require('../becca/entities/battribute');
const hiddenSubtreeService = require('./hidden_subtree');
const oneTimeTimer = require('./one_time_timer');
import eventService = require('./events');
import scriptService = require('./script');
import treeService = require('./tree');
import noteService = require('./notes');
import becca = require('../becca/becca');
import BAttribute = require('../becca/entities/battribute');
import hiddenSubtreeService = require('./hidden_subtree');
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) {
return;
}
@ -16,7 +21,7 @@ function runAttachedRelations(note, relationName, originEntity) {
const notesToRun = new Set(
note.getRelations(relationName)
.map(relation => relation.getTargetNote())
.filter(note => !!note)
.filter(note => !!note) as BNote[]
);
for (const noteToRun of notesToRun) {
@ -84,6 +89,9 @@ eventService.subscribe(eventService.ENTITY_CREATED, ({ entityName, entity }) =>
if (entity.type === 'relation' && entity.name === 'template') {
const note = becca.getNote(entity.noteId);
if (!note) {
return;
}
const templateNote = becca.getNote(entity.value);
@ -94,6 +102,7 @@ eventService.subscribe(eventService.ENTITY_CREATED, ({ entityName, entity }) =>
const content = note.getContent();
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
&& (!content || content.trim().length === 0)
&& templateNote.hasStringContent()) {
@ -138,7 +147,7 @@ eventService.subscribe(eventService.CHILD_NOTE_CREATED, ({ parentNote, childNote
runAttachedRelations(parentNote, 'runOnChildNoteCreation', childNote);
});
function processInverseRelations(entityName, entity, handler) {
function processInverseRelations(entityName: string, entity: BAttribute, handler: Handler) {
if (entityName === 'attributes' && entity.type === 'relation') {
const note = entity.getNote();
const relDefinitions = note.getLabels(`relation:${entity.name}`);
@ -149,13 +158,15 @@ function processInverseRelations(entityName, entity, handler) {
if (definition.inverseRelation && definition.inverseRelation.trim()) {
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);
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
const note = becca.notes[entity.noteId];
@ -203,7 +214,7 @@ eventService.subscribe(eventService.ENTITY_CHANGED, ({ entityName, entity }) =>
new BAttribute({
noteId: targetNote.noteId,
type: 'relation',
name: definition.inverseRelation,
name: definition.inverseRelation || "",
value: note.noteId,
isInheritable: entity.isInheritable
}).save();
@ -215,7 +226,7 @@ eventService.subscribe(eventService.ENTITY_CHANGED, ({ 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
const relations = targetNote.getOwnedRelations(definition.inverseRelation);
@ -238,6 +249,6 @@ eventService.subscribe(eventService.ENTITY_DELETED, ({ entityName, entity }) =>
}
});
module.exports = {
export = {
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 {
isPromoted?: boolean;
labelType?: string;
multiplicity?: string;
numberPrecision?: number;
promotedAlias?: string;
inverseRelation?: string;
}
import { DefinitionObject } from "./promoted_attribute_definition_interface";
function parse(value: string): DefinitionObject {
const tokens = value.split(',').map(t => t.trim());