mirror of
https://github.com/hcengineering/platform.git
synced 2026-09-29 04:55:03 +02:00
Support Controlled Documents import (#7541)
* Support Controlled Documents import Signed-off-by: Anna Khismatullina <anna.khismatullina@gmail.com> * Add example table Signed-off-by: Anna Khismatullina <anna.khismatullina@gmail.com> --------- Signed-off-by: Anna Khismatullina <anna.khismatullina@gmail.com>
This commit is contained in:
@@ -16,16 +16,16 @@
|
||||
import { type Employee } from '@hcengineering/contact'
|
||||
import { type AttachedData, type Class, type Ref, type TxOperations, Blob, Mixin } from '@hcengineering/core'
|
||||
import {
|
||||
type Document,
|
||||
type DocumentTemplate,
|
||||
type ControlledDocument,
|
||||
type Document,
|
||||
type DocumentCategory,
|
||||
type DocumentSpace,
|
||||
type DocumentMeta,
|
||||
type DocumentSpace,
|
||||
type DocumentTemplate,
|
||||
type HierarchyDocument,
|
||||
type Project,
|
||||
DocumentState,
|
||||
HierarchyDocument,
|
||||
ProjectDocument
|
||||
type ProjectDocument,
|
||||
DocumentState
|
||||
} from './types'
|
||||
|
||||
import documents from './plugin'
|
||||
@@ -67,18 +67,55 @@ export async function createControlledDocFromTemplate (
|
||||
return { seqNumber: -1, success: false }
|
||||
}
|
||||
|
||||
const { seqNumber, prefix, content, category } = await useDocumentTemplate(client, templateId)
|
||||
const { success, documentMetaId } = await createControlledDocMetadata(
|
||||
client,
|
||||
templateId,
|
||||
documentId,
|
||||
space,
|
||||
project,
|
||||
parent,
|
||||
prefix,
|
||||
seqNumber,
|
||||
spec.code,
|
||||
spec.title
|
||||
)
|
||||
|
||||
if (!success) {
|
||||
return { seqNumber: -1, success: false }
|
||||
}
|
||||
|
||||
await client.addCollection(
|
||||
docClass,
|
||||
space,
|
||||
documentMetaId,
|
||||
documents.class.DocumentMeta,
|
||||
'documents',
|
||||
{
|
||||
...spec,
|
||||
category,
|
||||
template: templateId,
|
||||
seqNumber,
|
||||
prefix,
|
||||
state: DocumentState.Draft,
|
||||
content
|
||||
},
|
||||
documentId
|
||||
)
|
||||
|
||||
return { seqNumber, success: true }
|
||||
}
|
||||
|
||||
export async function useDocumentTemplate (
|
||||
client: TxOperations,
|
||||
templateId: Ref<DocumentTemplate>
|
||||
): Promise<{ seqNumber: number, prefix: string, content: Ref<Blob> | null, category: Ref<DocumentCategory> }> {
|
||||
const template = await client.findOne(documents.mixin.DocumentTemplate, {
|
||||
_id: templateId
|
||||
})
|
||||
|
||||
if (template === undefined) {
|
||||
return { seqNumber: -1, success: false }
|
||||
}
|
||||
|
||||
let path: Array<Ref<DocumentMeta>> = []
|
||||
|
||||
if (parent !== undefined) {
|
||||
path = await getParentPath(client, parent)
|
||||
return { seqNumber: -1, prefix: '', content: null, category: '' as Ref<DocumentCategory> }
|
||||
}
|
||||
|
||||
await client.updateMixin(templateId, documents.class.Document, template.space, documents.mixin.DocumentTemplate, {
|
||||
@@ -89,34 +126,27 @@ export async function createControlledDocFromTemplate (
|
||||
const seqNumber = template.sequence + 1
|
||||
const prefix = template.docPrefix
|
||||
|
||||
return await createControlledDoc(
|
||||
client,
|
||||
templateId,
|
||||
documentId,
|
||||
{ ...spec, category: template.category },
|
||||
space,
|
||||
project,
|
||||
prefix,
|
||||
seqNumber,
|
||||
path,
|
||||
docClass,
|
||||
template.content
|
||||
)
|
||||
return { seqNumber, prefix, content: template.content, category: template.category as Ref<DocumentCategory> }
|
||||
}
|
||||
|
||||
async function createControlledDoc (
|
||||
export async function createControlledDocMetadata (
|
||||
client: TxOperations,
|
||||
templateId: Ref<DocumentTemplate>,
|
||||
documentId: Ref<ControlledDocument>,
|
||||
spec: AttachedData<ControlledDocument>,
|
||||
space: Ref<DocumentSpace>,
|
||||
project: Ref<Project> | undefined,
|
||||
parent: Ref<ProjectDocument> | undefined,
|
||||
prefix: string,
|
||||
seqNumber: number,
|
||||
path: Ref<DocumentMeta>[] = [],
|
||||
docClass: Ref<Class<ControlledDocument>> = documents.class.ControlledDocument,
|
||||
content: Ref<Blob> | null
|
||||
): Promise<{ seqNumber: number, success: boolean }> {
|
||||
specCode: string,
|
||||
specTitle: string,
|
||||
metaId?: Ref<DocumentMeta>
|
||||
): Promise<{
|
||||
success: boolean
|
||||
seqNumber: number
|
||||
documentMetaId: Ref<DocumentMeta>
|
||||
projectDocumentId: Ref<ProjectDocument>
|
||||
}> {
|
||||
const projectId = project ?? documents.ids.NoProject
|
||||
|
||||
const ops = client.apply()
|
||||
@@ -127,23 +157,33 @@ async function createControlledDoc (
|
||||
})
|
||||
|
||||
ops.notMatch(documents.class.Document, {
|
||||
code: spec.code
|
||||
code: specCode
|
||||
})
|
||||
|
||||
const metaId = await ops.createDoc(documents.class.DocumentMeta, space, {
|
||||
documents: 0,
|
||||
title: `${prefix}-${seqNumber} ${spec.title}`
|
||||
})
|
||||
const documentMetaId = await ops.createDoc(
|
||||
documents.class.DocumentMeta,
|
||||
space,
|
||||
{
|
||||
documents: 0,
|
||||
title: `${prefix}-${seqNumber} ${specTitle}`
|
||||
},
|
||||
metaId
|
||||
)
|
||||
|
||||
let path: Array<Ref<DocumentMeta>> = []
|
||||
if (parent !== undefined) {
|
||||
path = await getParentPath(client, parent)
|
||||
}
|
||||
|
||||
const projectMetaId = await ops.createDoc(documents.class.ProjectMeta, space, {
|
||||
project: projectId,
|
||||
meta: metaId,
|
||||
meta: documentMetaId,
|
||||
path,
|
||||
parent: path[0] ?? documents.ids.NoParent,
|
||||
documents: 0
|
||||
})
|
||||
|
||||
await client.addCollection(
|
||||
const projectDocumentId = await client.addCollection(
|
||||
documents.class.ProjectDocument,
|
||||
space,
|
||||
projectMetaId,
|
||||
@@ -156,25 +196,9 @@ async function createControlledDoc (
|
||||
}
|
||||
)
|
||||
|
||||
await ops.addCollection(
|
||||
docClass,
|
||||
space,
|
||||
metaId,
|
||||
documents.class.DocumentMeta,
|
||||
'documents',
|
||||
{
|
||||
...spec,
|
||||
template: templateId,
|
||||
seqNumber,
|
||||
prefix,
|
||||
state: DocumentState.Draft,
|
||||
content
|
||||
},
|
||||
documentId
|
||||
)
|
||||
|
||||
const success = await ops.commit()
|
||||
return { seqNumber, success: success.result }
|
||||
|
||||
return { success: success.result, seqNumber, documentMetaId, projectDocumentId }
|
||||
}
|
||||
|
||||
export async function createDocumentTemplate (
|
||||
@@ -190,6 +214,70 @@ export async function createDocumentTemplate (
|
||||
category: Ref<DocumentCategory>,
|
||||
author?: Ref<Employee>
|
||||
): Promise<{ seqNumber: number, success: boolean }> {
|
||||
const { success, seqNumber, code, documentMetaId } = await createDocumentTemplateMetadata(
|
||||
client,
|
||||
_class,
|
||||
space,
|
||||
_mixin,
|
||||
project,
|
||||
parent,
|
||||
templateId,
|
||||
prefix,
|
||||
spec.code ?? '',
|
||||
spec.title
|
||||
)
|
||||
|
||||
if (!success) {
|
||||
return { seqNumber: -1, success: false }
|
||||
}
|
||||
|
||||
const ops = client.apply()
|
||||
await ops.addCollection<DocumentMeta, HierarchyDocument>(
|
||||
_class,
|
||||
space,
|
||||
documentMetaId,
|
||||
documents.class.DocumentMeta,
|
||||
'documents',
|
||||
{
|
||||
...spec,
|
||||
code,
|
||||
seqNumber,
|
||||
category,
|
||||
prefix: TEMPLATE_PREFIX,
|
||||
author,
|
||||
owner: author,
|
||||
content: spec.content ?? null
|
||||
},
|
||||
templateId
|
||||
)
|
||||
await ops.createMixin(templateId, documents.class.Document, space, _mixin, {
|
||||
sequence: 0,
|
||||
docPrefix: prefix
|
||||
})
|
||||
const commit = await ops.commit()
|
||||
|
||||
return { seqNumber, success: commit.result }
|
||||
}
|
||||
|
||||
export async function createDocumentTemplateMetadata (
|
||||
client: TxOperations,
|
||||
_class: Ref<Class<Document>>,
|
||||
space: Ref<DocumentSpace>,
|
||||
_mixin: Ref<Mixin<DocumentTemplate>>,
|
||||
project: Ref<Project> | undefined,
|
||||
parent: Ref<ProjectDocument> | undefined,
|
||||
templateId: Ref<ControlledDocument>,
|
||||
prefix: string,
|
||||
specCode: string,
|
||||
specTitle: string,
|
||||
metaId?: Ref<DocumentMeta>
|
||||
): Promise<{
|
||||
success: boolean
|
||||
seqNumber: number
|
||||
code: string
|
||||
documentMetaId: Ref<DocumentMeta>
|
||||
projectDocumentId: Ref<ProjectDocument>
|
||||
}> {
|
||||
const projectId = project ?? documents.ids.NoProject
|
||||
|
||||
const incResult = await client.updateDoc(
|
||||
@@ -202,7 +290,7 @@ export async function createDocumentTemplate (
|
||||
true
|
||||
)
|
||||
const seqNumber = (incResult as any).object.sequence as number
|
||||
const code = spec.code === '' ? `${TEMPLATE_PREFIX}-${seqNumber}` : spec.code
|
||||
const code = specCode === '' ? `${TEMPLATE_PREFIX}-${seqNumber}` : specCode
|
||||
|
||||
let path: Array<Ref<DocumentMeta>> = []
|
||||
|
||||
@@ -225,20 +313,25 @@ export async function createDocumentTemplate (
|
||||
docPrefix: prefix
|
||||
})
|
||||
|
||||
const metaId = await ops.createDoc(documents.class.DocumentMeta, space, {
|
||||
documents: 0,
|
||||
title: `${TEMPLATE_PREFIX}-${seqNumber} ${spec.title}`
|
||||
})
|
||||
const documentMetaId = await ops.createDoc(
|
||||
documents.class.DocumentMeta,
|
||||
space,
|
||||
{
|
||||
documents: 0,
|
||||
title: `${TEMPLATE_PREFIX}-${seqNumber} ${specTitle}`
|
||||
},
|
||||
metaId
|
||||
)
|
||||
|
||||
const projectMetaId = await ops.createDoc(documents.class.ProjectMeta, space, {
|
||||
project: projectId,
|
||||
meta: metaId,
|
||||
meta: documentMetaId,
|
||||
path,
|
||||
parent: path[0] ?? documents.ids.NoParent,
|
||||
documents: 0
|
||||
})
|
||||
|
||||
await client.addCollection(
|
||||
const projectDocumentId = await client.addCollection(
|
||||
documents.class.ProjectDocument,
|
||||
space,
|
||||
projectMetaId,
|
||||
@@ -251,31 +344,7 @@ export async function createDocumentTemplate (
|
||||
}
|
||||
)
|
||||
|
||||
await ops.addCollection<DocumentMeta, HierarchyDocument>(
|
||||
_class,
|
||||
space,
|
||||
metaId,
|
||||
documents.class.DocumentMeta,
|
||||
'documents',
|
||||
{
|
||||
...spec,
|
||||
code,
|
||||
seqNumber,
|
||||
category,
|
||||
prefix: TEMPLATE_PREFIX,
|
||||
author,
|
||||
owner: author,
|
||||
content: null
|
||||
},
|
||||
templateId
|
||||
)
|
||||
|
||||
await ops.createMixin(templateId, documents.class.Document, space, _mixin, {
|
||||
sequence: 0,
|
||||
docPrefix: prefix
|
||||
})
|
||||
|
||||
const success = await ops.commit()
|
||||
|
||||
return { seqNumber, success: success.result }
|
||||
return { success: success.result, seqNumber, code, documentMetaId, projectDocumentId }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user