From 74d1dcc83d3cde1554ba1d07ab40e9bb843f1aef Mon Sep 17 00:00:00 2001 From: Anna Khismatullina Date: Tue, 22 Apr 2025 18:00:12 +0700 Subject: [PATCH] Support bulk update step in init scripts (for tracex needs) (#8663) Signed-off-by: Anna Khismatullina --- .../[SOP-001] Document Control.md | 2 +- packages/importer/src/huly/huly.ts | 6 ++++- packages/importer/src/importer/importer.ts | 2 +- server/tool/src/initializer.ts | 22 +++++++++++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/dev/import-tool/docs/huly/example-workspace/QMS Documents/[SOP-001] Document Control.md b/dev/import-tool/docs/huly/example-workspace/QMS Documents/[SOP-001] Document Control.md index 8daf8101ad..52ea1c74fd 100644 --- a/dev/import-tool/docs/huly/example-workspace/QMS Documents/[SOP-001] Document Control.md +++ b/dev/import-tool/docs/huly/example-workspace/QMS Documents/[SOP-001] Document Control.md @@ -2,7 +2,7 @@ class: documents:mixin:DocumentTemplate title: 'Standard Operating Procedure Template' docPrefix: SOP -category: DOC +category: DC author: John Appleseed owner: John Appleseed abstract: Template for Standard Operating Procedures diff --git a/packages/importer/src/huly/huly.ts b/packages/importer/src/huly/huly.ts index ede29fcb89..03749fb1ce 100644 --- a/packages/importer/src/huly/huly.ts +++ b/packages/importer/src/huly/huly.ts @@ -855,8 +855,12 @@ export class HulyFormatImporter { throw new Error(`Author or owner not found: ${header.author} or ${header.owner}`) } + const category = this.controlledDocumentCategories.get(header.category) + if (category === undefined) { + throw new Error(`Category not found: ${header.category}`) + } + const codeMatch = path.basename(docPath).match(/^\[([^\]]+)\]/) - const category = header.category !== undefined ? this.controlledDocumentCategories.get(header.category) : undefined return { id, metaId, diff --git a/packages/importer/src/importer/importer.ts b/packages/importer/src/importer/importer.ts index d0516553f0..2581f93b02 100644 --- a/packages/importer/src/importer/importer.ts +++ b/packages/importer/src/importer/importer.ts @@ -196,7 +196,7 @@ export interface ImportControlledDocumentTemplate extends ImportDoc { major: number minor: number state: DocumentState - category?: Ref + category: Ref author?: Ref owner?: Ref abstract?: string diff --git a/server/tool/src/initializer.ts b/server/tool/src/initializer.ts index 1eaff8b9ec..4d5bee8088 100644 --- a/server/tool/src/initializer.ts +++ b/server/tool/src/initializer.ts @@ -35,6 +35,7 @@ export type InitStep = | DefaultStep | MixinStep | UpdateStep + | BulkUpdateStep | FindStep | UploadStep | ImportStep @@ -70,6 +71,15 @@ export interface UpdateStep { data: Props } +export interface BulkUpdateStep { + type: 'bulkUpdate' + _class: Ref> + query: Partial + markdownFields?: string[] + collabFields?: string[] + data: Props +} + export interface FindStep { type: 'find' _class: Ref> @@ -120,6 +130,8 @@ export class WorkspaceInitializer { await this.processCreate(step, vars, defaults) } else if (step.type === 'update') { await this.processUpdate(step, vars) + } else if (step.type === 'bulkUpdate') { + await this.processBulkUpdate(step, vars) } else if (step.type === 'mixin') { await this.processMixin(step, vars) } else if (step.type === 'find') { @@ -203,6 +215,16 @@ export class WorkspaceInitializer { await this.client.updateDoc(step._class, space, _id as Ref, props) } + private async processBulkUpdate(step: BulkUpdateStep, vars: Record): Promise { + const ops = this.client.apply() + const docs = await this.client.findAll(step._class, { ...(step.query as any) }) + const data = await this.fillPropsWithMarkdown(step.data, vars, step.markdownFields) + for (const doc of docs) { + await ops.updateDoc(step._class, doc.space, doc._id, data) + } + await ops.commit() + } + private async processCreate( step: CreateStep, vars: Record,