diff --git a/plugins/process-resources/src/exporter.ts b/plugins/process-resources/src/exporter.ts index 30d206d5a59..83f22fc8a8e 100644 --- a/plugins/process-resources/src/exporter.ts +++ b/plugins/process-resources/src/exporter.ts @@ -1,21 +1,37 @@ 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): { docs: Doc[] required: Array>> } { const docs: Doc[] = [] + const required: Array>> = [] 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): { @@ -23,13 +39,84 @@ export function exportProcess (proc: Doc): { required: Array>> } { const docs: Doc[] = [proc] + const required: Array>> = [] 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 | undefined, + masterTag: Ref, + docs: Doc[], + required: Array>>, + 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).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).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).to + if (h.isDerived(to, card.class.Card) && !required.includes(to)) { + required.push(to) + } + } + } + } + } } export async function importProcess (masterTag: Ref, json: string): Promise {