From 40534dde46a6aea6cb0f1159b8217fd7cb2433b1 Mon Sep 17 00:00:00 2001 From: Andrey Platov Date: Thu, 26 Aug 2021 10:26:06 +0200 Subject: [PATCH] triggers act on user behalf Signed-off-by: Andrey Platov --- server/core/src/index.ts | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/server/core/src/index.ts b/server/core/src/index.ts index fe85a86462..35091122f2 100644 --- a/server/core/src/index.ts +++ b/server/core/src/index.ts @@ -14,7 +14,7 @@ // limitations under the License. // -import type { Doc, Tx, TxCreateDoc, Ref, Class, ServerStorage, DocumentQuery, FindOptions, FindResult, Storage } from '@anticrm/core' +import type { Doc, Tx, TxCreateDoc, Ref, Class, ServerStorage, DocumentQuery, FindOptions, FindResult, Storage, Account } from '@anticrm/core' import core, { Hierarchy, TxFactory, ModelDb, DOMAIN_MODEL } from '@anticrm/core' import type { Resource, Plugin } from '@anticrm/platform' import { getResource, plugin } from '@anticrm/platform' @@ -37,10 +37,6 @@ export interface Trigger extends Doc { export class Triggers { private readonly triggers: TriggerFunc[] = [] - constructor (private readonly txFactory: TxFactory) { - - } - async tx (tx: Tx): Promise { if (tx._class === core.class.TxCreateDoc) { const createTx = tx as TxCreateDoc @@ -52,8 +48,8 @@ export class Triggers { } } - async apply (tx: Tx): Promise { - const derived = this.triggers.map(trigger => trigger(tx, this.txFactory)) + async apply (account: Ref, tx: Tx): Promise { + const derived = this.triggers.map(trigger => trigger(tx, new TxFactory(account))) const result = await Promise.all(derived) return result.flatMap(x => x) } @@ -99,7 +95,7 @@ class TServerStorage implements ServerStorage { return [] // we do not apply triggers on model changes? } else { await this.dbAdapter.tx(tx) - const derived = await this.triggers.apply(tx) + const derived = await this.triggers.apply(tx.modifiedBy, tx) for (const tx of derived) { await this.dbAdapter.tx(tx) // triggers does not generate changes to model objects? } @@ -114,7 +110,7 @@ class TServerStorage implements ServerStorage { export async function createServerStorage (factory: DbAdapterFactory, url: string, db: string): Promise { const hierarchy = new Hierarchy() const model = new ModelDb(hierarchy) - const triggers = new Triggers(new TxFactory(core.account.System)) + const triggers = new Triggers() const [dbAdapter, txes] = await factory(hierarchy, url, db)