Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 94 additions & 7 deletions plugins/process-resources/src/exporter.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,122 @@
import { type MasterTag } from '@hcengineering/card'
import core, { type Doc, type Ref, type Class, generateId } from '@hcengineering/core'
import card from '@hcengineering/card'
import core, {
type Doc,
type Ref,
type Class,
generateId,
type EnumOf,
type RefTo,
type ArrOf,
type ModelDb,
type Hierarchy
} from '@hcengineering/core'
import { getClient } from '@hcengineering/presentation'
import process from './plugin'
import { type Process, type Transition } from '@hcengineering/process'
import processPlugin from './plugin'

export function exportProcesses (_id: Ref<MasterTag>): {
docs: Doc[]
required: Array<Ref<Class<Doc>>>
} {
const docs: Doc[] = []
const required: Array<Ref<Class<Doc>>> = []
const client = getClient()
const m = client.getModel()
const processes = m.findAllSync(process.class.Process, { masterTag: _id })
const processes = m.findAllSync(processPlugin.class.Process, { masterTag: _id })
for (const proc of processes) {
const res = exportProcess(proc)
docs.push(...res.docs)
for (const req of res.required) {
if (!required.includes(req)) required.push(req)
}
}
return { docs, required: [] }
return { docs, required }
}

export function exportProcess (proc: Doc): {
docs: Doc[]
required: Array<Ref<Class<Doc>>>
} {
const docs: Doc[] = [proc]
const required: Array<Ref<Class<Doc>>> = []
const client = getClient()
const m = client.getModel()
const h = client.getHierarchy()

docs.push(...m.findAllSync(processPlugin.class.State, { process: proc._id as any }))
const transitions = m.findAllSync(processPlugin.class.Transition, { process: proc._id as any }) as Transition[]
docs.push(...transitions)

const processDoc = proc as Process

for (const tr of transitions) {
processParams(tr.triggerParams, processDoc.masterTag, docs, required, m, h)
for (const action of tr.actions) {
processParams(action.params, processDoc.masterTag, docs, required, m, h)
}
}

docs.push(...m.findAllSync(process.class.State, { process: proc._id as any }))
docs.push(...m.findAllSync(process.class.Transition, { process: proc._id as any }))
return { docs, required }
}

return { docs, required: [] }
function processParams (
params: Record<string, any> | undefined,
masterTag: Ref<MasterTag>,
docs: Doc[],
required: Array<Ref<Class<Doc>>>,
m: ModelDb,
h: Hierarchy
): void {
if (params === undefined) return
for (const key in params) {
if (key.startsWith('$')) {
const val = params[key]
if (Array.isArray(val)) {
for (const item of val) {
if (typeof item === 'object' && item !== null) {
processParams(item, masterTag, docs, required, m, h)
}
}
} else if (typeof val === 'object' && val !== null) {
processParams(val, masterTag, docs, required, m, h)
}
continue
}
const attr = h.findAttribute(masterTag, key)
if (attr !== undefined) {
const type = attr.type
if (type._class === core.class.EnumOf) {
const enumRef = (type as EnumOf).of
const enumDoc = m.findObject(enumRef)
if (enumDoc !== undefined && !docs.some((d) => d._id === enumDoc._id)) {
docs.push(enumDoc)
}
}
if (type._class === core.class.RefTo) {
const to = (type as RefTo<Doc>).to
if (h.isDerived(to, card.class.Card) && !required.includes(to)) {
required.push(to)
}
}
if (type._class === core.class.ArrOf) {
const of = (type as ArrOf<Doc>).of
if (of._class === core.class.EnumOf) {
const enumRef = (of as EnumOf).of
const enumDoc = m.findObject(enumRef)
if (enumDoc !== undefined && !docs.some((d) => d._id === enumDoc._id)) {
docs.push(enumDoc)
}
}
if (of._class === core.class.RefTo) {
const to = (of as RefTo<Doc>).to
if (h.isDerived(to, card.class.Card) && !required.includes(to)) {
required.push(to)
}
}
}
}
}
}

export async function importProcess (masterTag: Ref<MasterTag>, json: string): Promise<void> {
Expand Down
Loading