mirror of
https://github.com/hcengineering/platform.git
synced 2026-09-11 04:07:43 +02:00
ATS-9: update states once template updates (#3496)
Signed-off-by: Vyacheslav Tumanov <me@slavatumanov.me>
This commit is contained in:
@@ -13,7 +13,95 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import { Class, Doc, Ref, Space, Tx, TxCreateDoc, TxProcessor, TxUpdateDoc } from '@hcengineering/core'
|
||||
import { TriggerControl } from '@hcengineering/server-core'
|
||||
import core from '@hcengineering/core/lib/component'
|
||||
import task, { KanbanTemplateSpace, KanbanTemplate, StateTemplate, State, DoneState } from '@hcengineering/task'
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export async function OnTemplateStateUpdate (tx: Tx, control: TriggerControl): Promise<Tx[]> {
|
||||
const actualTx = tx as TxUpdateDoc<StateTemplate>
|
||||
const txes = await control.findAll(core.class.TxCollectionCUD, {
|
||||
'tx.objectId': actualTx.objectId
|
||||
})
|
||||
const createTxes = await control.findAll(core.class.TxCreateDoc, { objectId: actualTx.objectId })
|
||||
const updateTxes = await control.findAll(core.class.TxUpdateDoc, { objectId: actualTx.objectId })
|
||||
const prevDoc = TxProcessor.buildDoc2Doc(
|
||||
[...createTxes, ...txes, ...updateTxes].filter((t) => t._id !== tx._id)
|
||||
) as StateTemplate
|
||||
if (prevDoc === undefined) return []
|
||||
const templateSpace = (
|
||||
await control.findAll(task.class.KanbanTemplateSpace, {
|
||||
_id: actualTx.objectSpace as Ref<KanbanTemplateSpace>
|
||||
})
|
||||
)[0] as KanbanTemplateSpace
|
||||
if (templateSpace === undefined) return []
|
||||
const template = (await control.findAll(task.class.KanbanTemplate, { _id: prevDoc.attachedTo }))[0] as KanbanTemplate
|
||||
const classToChange = getClassToChangeOrCreate(actualTx.objectClass)
|
||||
const objectWithStatesToChange = await control.findAll(templateSpace.attachedToClass, { templateId: template._id })
|
||||
const ids = Array.from(objectWithStatesToChange.map((x) => x._id)) as Array<Ref<Space>>
|
||||
const newDoc = TxProcessor.buildDoc2Doc([...createTxes, ...txes, ...updateTxes]) as StateTemplate
|
||||
const statesToChange = Array.from(
|
||||
await control.findAll(classToChange, { space: { $in: ids }, name: prevDoc.name })
|
||||
) as Array<State | DoneState>
|
||||
return statesToChange.map((it) => {
|
||||
const newAttributes = it._class === task.class.State ? { color: newDoc.color, rank: newDoc.rank } : {}
|
||||
return control.txFactory.createTxUpdateDoc(it._class, it.space, it._id, {
|
||||
name: newDoc.name,
|
||||
...newAttributes
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export async function OnTemplateStateCreate (tx: Tx, control: TriggerControl): Promise<Tx[]> {
|
||||
const actualTx = tx as TxCreateDoc<StateTemplate>
|
||||
const templateSpace = (
|
||||
await control.findAll(task.class.KanbanTemplateSpace, {
|
||||
_id: actualTx.objectSpace as Ref<KanbanTemplateSpace>
|
||||
})
|
||||
)[0] as KanbanTemplateSpace
|
||||
if (templateSpace === undefined) return []
|
||||
const template = (
|
||||
await control.findAll(task.class.KanbanTemplate, { _id: actualTx.attributes.attachedTo })
|
||||
)[0] as KanbanTemplate
|
||||
const classToChange = getClassToChangeOrCreate(actualTx.objectClass)
|
||||
const objectWithStatesToChange = await control.findAll(templateSpace.attachedToClass, { templateId: template._id })
|
||||
const ids = Array.from(objectWithStatesToChange.map((x) => x._id)) as Array<Ref<Space>>
|
||||
const doc = TxProcessor.createDoc2Doc(actualTx)
|
||||
const ofAttribute = classToChange === task.class.State ? task.attribute.State : task.attribute.DoneState
|
||||
return ids.map((it) => {
|
||||
const newAttributes = classToChange === task.class.State ? { color: doc.color, rank: doc.rank } : {}
|
||||
return control.txFactory.createTxCreateDoc(classToChange, it, {
|
||||
ofAttribute,
|
||||
name: doc.name,
|
||||
...newAttributes
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function getClassToChangeOrCreate (check: Ref<Class<Doc>>): Ref<Class<Doc>> {
|
||||
let classToChange = task.class.State
|
||||
switch (check) {
|
||||
case task.class.WonStateTemplate:
|
||||
classToChange = task.class.WonState
|
||||
break
|
||||
case task.class.LostStateTemplate:
|
||||
classToChange = task.class.LostState
|
||||
break
|
||||
}
|
||||
return classToChange
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
||||
export default async () => ({
|
||||
function: {}
|
||||
function: {},
|
||||
trigger: {
|
||||
OnTemplateStateUpdate,
|
||||
OnTemplateStateCreate
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user