DocWithState trigger

Signed-off-by: Andrey Platov <andrey@hardcoreeng.com>
This commit is contained in:
Andrey Platov 2021-10-10 12:46:46 +02:00
parent 3d833008f5
commit 4f147640c5
No known key found for this signature in database
GPG Key ID: C8787EFEB4B64AF0
3 changed files with 40 additions and 17 deletions

View File

@ -1,6 +1,6 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "ES2016", "target": "ES2017",
"module": "commonjs", "module": "commonjs",
"declaration": true, "declaration": true,
"strict": true, "strict": true,

View File

@ -44,26 +44,26 @@
const client = getClient() const client = getClient()
async function createApplication() { async function createApplication() {
const state = await client.findOne(core.class.State, { space: _space }) // const state = await client.findOne(core.class.State, { space: _space })
if (state === undefined) { // if (state === undefined) {
throw new Error('create application: state not found') // throw new Error('create application: state not found')
} // }
const id = await client.createDoc(recruit.class.Applicant, _space, { const id = await client.createDoc(recruit.class.Applicant, _space, {
candidate, candidate,
state: state._id // state: state._id
}) })
const kanban = await client.findOne(view.class.Kanban, { attachedTo: _space }) // const kanban = await client.findOne(view.class.Kanban, { attachedTo: _space })
if (kanban === undefined) { // if (kanban === undefined) {
throw new Error('kanban object not found') // throw new Error('kanban object not found')
} // }
await client.updateDoc(view.class.Kanban, _space, kanban._id, { // await client.updateDoc(view.class.Kanban, _space, kanban._id, {
$push: { // $push: {
order: id // order: id
} // }
}) // })
dispatch('close') dispatch('close')
} }

View File

@ -14,10 +14,11 @@
// limitations under the License. // limitations under the License.
// //
import type { Tx, TxFactory, Doc, TxCreateDoc, DocWithState } from '@anticrm/core' import type { Tx, TxFactory, Doc, TxCreateDoc, DocWithState, State, TxRemoveDoc } from '@anticrm/core'
import type { FindAll } from '@anticrm/server-core' import type { FindAll } from '@anticrm/server-core'
import core, { Hierarchy } from '@anticrm/core' import core, { Hierarchy } from '@anticrm/core'
import view, { Kanban } from '@anticrm/view'
/** /**
* @public * @public
@ -26,7 +27,29 @@ export async function OnDocWithState (tx: Tx, txFactory: TxFactory, findAll: Fin
if (tx._class === core.class.TxCreateDoc) { if (tx._class === core.class.TxCreateDoc) {
const createTx = tx as TxCreateDoc<DocWithState> const createTx = tx as TxCreateDoc<DocWithState>
if (hierarchy.isDerived(createTx.objectClass, core.class.DocWithState)) { if (hierarchy.isDerived(createTx.objectClass, core.class.DocWithState)) {
console.log('OnDocWithState derived here') const state = (await (findAll as FindAll<State>)(core.class.State, { space: createTx.objectSpace }))[0] // TODO: make FindAll generic
if (state === undefined) {
throw new Error('OnDocWithState: state not found')
}
const kanban = (await (findAll as FindAll<Kanban>)(view.class.Kanban, { attachedTo: createTx.objectSpace }))[0]
if (kanban === undefined) {
throw new Error('OnDocWithState: kanban not found')
}
return [
txFactory.createTxUpdateDoc(createTx.objectClass, createTx.objectSpace, createTx.objectId, { state: state._id }),
txFactory.createTxUpdateDoc(view.class.Kanban, createTx.objectSpace, kanban._id, { $push: { order: createTx.objectId } })
]
}
} else if (tx._class === core.class.TxRemoveDoc) {
const removeTx = tx as TxRemoveDoc<DocWithState>
if (hierarchy.isDerived(removeTx.objectClass, core.class.DocWithState)) {
const kanban = (await (findAll as FindAll<Kanban>)(view.class.Kanban, { attachedTo: removeTx.objectSpace }))[0]
if (kanban === undefined) {
throw new Error('OnDocWithState: kanban not found')
}
return [
txFactory.createTxUpdateDoc(view.class.Kanban, removeTx.objectSpace, kanban._id, { $pull: { order: removeTx.objectId } })
]
} }
} }
return [] return []