From 46ced93bc138618c5ddc5b2ae4c219f17633344c Mon Sep 17 00:00:00 2001 From: Alexander Onnikov Date: Thu, 29 Aug 2024 19:20:32 +0700 Subject: [PATCH] fix: copy template content when creating controlled document (#6441) Signed-off-by: Alexander Onnikov --- dev/doc-import-tool/src/import.ts | 18 +++++++++++++++- .../src/components/CreateDocument.svelte | 14 +++++++++++-- .../create-doc/QmsDocumentWizard.svelte | 6 ++++-- plugins/controlled-documents/src/docutils.ts | 21 ++++++++++++++++--- 4 files changed, 51 insertions(+), 8 deletions(-) diff --git a/dev/doc-import-tool/src/import.ts b/dev/doc-import-tool/src/import.ts index 1db7697f53..f8c414d409 100644 --- a/dev/doc-import-tool/src/import.ts +++ b/dev/doc-import-tool/src/import.ts @@ -15,6 +15,7 @@ import documents, { import core, { AttachedData, BackupClient, + CollaborativeDoc, Client as CoreClient, Data, MeasureContext, @@ -114,7 +115,22 @@ async function createDocument ( console.log('Creating controlled doc from template') - const { success } = await createControlledDocFromTemplate(txops, templateId, docId, data, space, undefined, undefined) + const copyContent = async (source: CollaborativeDoc, target: CollaborativeDoc): Promise => { + // intentionally left empty + // even though the template has some content, it won't be used + } + + const { success } = await createControlledDocFromTemplate( + txops, + templateId, + docId, + data, + space, + undefined, + undefined, + documents.class.ControlledDocument, + copyContent + ) if (!success) { throw new Error('Failed to create controlled document from template') } diff --git a/plugins/controlled-documents-resources/src/components/CreateDocument.svelte b/plugins/controlled-documents-resources/src/components/CreateDocument.svelte index d918ce1707..310275ad53 100644 --- a/plugins/controlled-documents-resources/src/components/CreateDocument.svelte +++ b/plugins/controlled-documents-resources/src/components/CreateDocument.svelte @@ -27,7 +27,7 @@ Ref, SortingOrder } from '@hcengineering/core' - import { Card, createQuery, getClient } from '@hcengineering/presentation' + import { Card, copyDocument, createQuery, getClient } from '@hcengineering/presentation' import { createFocusManager, EditBox, FocusHandler } from '@hcengineering/ui' import { ObjectBox } from '@hcengineering/view-resources' import { @@ -91,7 +91,17 @@ return } - await createControlledDocFromTemplate(client, templateId, id, object, space, undefined, undefined, documentClass) + await createControlledDocFromTemplate( + client, + templateId, + id, + object, + space, + undefined, + undefined, + documentClass, + copyDocument + ) dispatch('close', id) } diff --git a/plugins/controlled-documents-resources/src/components/create-doc/QmsDocumentWizard.svelte b/plugins/controlled-documents-resources/src/components/create-doc/QmsDocumentWizard.svelte index 7fd6f4f704..eebd20c597 100644 --- a/plugins/controlled-documents-resources/src/components/create-doc/QmsDocumentWizard.svelte +++ b/plugins/controlled-documents-resources/src/components/create-doc/QmsDocumentWizard.svelte @@ -24,7 +24,7 @@ type Data, type Ref } from '@hcengineering/core' - import { MessageBox, getClient } from '@hcengineering/presentation' + import { MessageBox, copyDocument, getClient } from '@hcengineering/presentation' import { AnySvelteComponent, addNotification, @@ -160,7 +160,9 @@ docObject, _space, $locationStep.project, - $locationStep.parent + $locationStep.parent, + documents.class.ControlledDocument, + copyDocument ) if (!success) { diff --git a/plugins/controlled-documents/src/docutils.ts b/plugins/controlled-documents/src/docutils.ts index 710871d5f1..532724476a 100644 --- a/plugins/controlled-documents/src/docutils.ts +++ b/plugins/controlled-documents/src/docutils.ts @@ -40,6 +40,7 @@ import { import documents from './plugin' import { TEMPLATE_PREFIX } from './utils' +import { setPlatformStatus, unknownError } from '@hcengineering/platform' async function getParentPath (client: TxOperations, parent: Ref): Promise>> { const parentDocObj = await client.findOne(documents.class.ProjectDocument, { @@ -71,7 +72,8 @@ export async function createControlledDocFromTemplate ( space: Ref, project: Ref | undefined, parent: Ref | undefined, - docClass: Ref> = documents.class.ControlledDocument + docClass: Ref> = documents.class.ControlledDocument, + copyContent: (source: CollaborativeDoc, target: CollaborativeDoc) => Promise = async () => {} ): Promise<{ seqNumber: number, success: boolean }> { if (templateId == null) { return { seqNumber: -1, success: false } @@ -99,6 +101,8 @@ export async function createControlledDocFromTemplate ( const seqNumber = template.sequence + 1 const prefix = template.docPrefix + const _copyContent = (doc: CollaborativeDoc): Promise => copyContent(template.content, doc) + return await createControlledDoc( client, templateId, @@ -109,7 +113,8 @@ export async function createControlledDocFromTemplate ( prefix, seqNumber, path, - docClass + docClass, + _copyContent ) } @@ -123,7 +128,8 @@ async function createControlledDoc ( prefix: string, seqNumber: number, path: Ref[] = [], - docClass: Ref> = documents.class.ControlledDocument + docClass: Ref> = documents.class.ControlledDocument, + copyContent: (doc: CollaborativeDoc) => Promise ): Promise<{ seqNumber: number, success: boolean }> { const projectId = project ?? documents.ids.NoProject @@ -185,6 +191,15 @@ async function createControlledDoc ( const success = await ops.commit() + if (success.result) { + try { + await copyContent(collaborativeDoc) + } catch (err) { + await setPlatformStatus(unknownError(err)) + return { seqNumber, success: false } + } + } + return { seqNumber, success: success.result } }