mirror of
https://github.com/hcengineering/platform.git
synced 2025-04-23 16:56:07 +00:00
triggers act on user behalf
Signed-off-by: Andrey Platov <andrey@hardcoreeng.com>
This commit is contained in:
parent
1e6f81a9f3
commit
40534dde46
@ -14,7 +14,7 @@
|
|||||||
// limitations under the License.
|
// 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 core, { Hierarchy, TxFactory, ModelDb, DOMAIN_MODEL } from '@anticrm/core'
|
||||||
import type { Resource, Plugin } from '@anticrm/platform'
|
import type { Resource, Plugin } from '@anticrm/platform'
|
||||||
import { getResource, plugin } from '@anticrm/platform'
|
import { getResource, plugin } from '@anticrm/platform'
|
||||||
@ -37,10 +37,6 @@ export interface Trigger extends Doc {
|
|||||||
export class Triggers {
|
export class Triggers {
|
||||||
private readonly triggers: TriggerFunc[] = []
|
private readonly triggers: TriggerFunc[] = []
|
||||||
|
|
||||||
constructor (private readonly txFactory: TxFactory) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
async tx (tx: Tx): Promise<void> {
|
async tx (tx: Tx): Promise<void> {
|
||||||
if (tx._class === core.class.TxCreateDoc) {
|
if (tx._class === core.class.TxCreateDoc) {
|
||||||
const createTx = tx as TxCreateDoc<Doc>
|
const createTx = tx as TxCreateDoc<Doc>
|
||||||
@ -52,8 +48,8 @@ export class Triggers {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async apply (tx: Tx): Promise<Tx[]> {
|
async apply (account: Ref<Account>, tx: Tx): Promise<Tx[]> {
|
||||||
const derived = this.triggers.map(trigger => trigger(tx, this.txFactory))
|
const derived = this.triggers.map(trigger => trigger(tx, new TxFactory(account)))
|
||||||
const result = await Promise.all(derived)
|
const result = await Promise.all(derived)
|
||||||
return result.flatMap(x => x)
|
return result.flatMap(x => x)
|
||||||
}
|
}
|
||||||
@ -99,7 +95,7 @@ class TServerStorage implements ServerStorage {
|
|||||||
return [] // we do not apply triggers on model changes?
|
return [] // we do not apply triggers on model changes?
|
||||||
} else {
|
} else {
|
||||||
await this.dbAdapter.tx(tx)
|
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) {
|
for (const tx of derived) {
|
||||||
await this.dbAdapter.tx(tx) // triggers does not generate changes to model objects?
|
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<ServerStorage> {
|
export async function createServerStorage (factory: DbAdapterFactory, url: string, db: string): Promise<ServerStorage> {
|
||||||
const hierarchy = new Hierarchy()
|
const hierarchy = new Hierarchy()
|
||||||
const model = new ModelDb(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)
|
const [dbAdapter, txes] = await factory(hierarchy, url, db)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user