From caaff1c3d63511d2ed696464eb7b164fb21ace46 Mon Sep 17 00:00:00 2001 From: "codegen-sh[bot]" <131295404+codegen-sh[bot]@users.noreply.github.com> Date: Fri, 30 May 2025 20:45:11 -0400 Subject: [PATCH] Add order parameter to addDocumentToStructure function (#9342) * Add order parameter to addDocumentToStructure function - Add 'order' parameter with 'prepend' | 'append' options to Collection.addDocumentToStructure - Update import logic to use 'append' order to preserve document sorting during import - Fixes issue where exported documents lose their sorting when re-imported - Maintains backward compatibility by defaulting to append behavior Fixes #7532 * Fix TypeScript error: rename order parameter to insertOrder The 'order' parameter in addDocumentToStructure was conflicting with Sequelize's FindOptions.order property, causing a type intersection error. Renamed it to 'insertOrder' to avoid the conflict. Fixes TypeScript compilation errors in: - server/queues/processors/ImportsProcessor.ts - server/queues/tasks/ImportTask.ts --------- Co-authored-by: codegen-sh[bot] <131295404+codegen-sh[bot]@users.noreply.github.com> --- server/models/Collection.ts | 33 ++++++++++++++------ server/queues/processors/ImportsProcessor.ts | 3 +- server/queues/tasks/ImportTask.ts | 3 +- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/server/models/Collection.ts b/server/models/Collection.ts index 70109fc4bd..effd67e915 100644 --- a/server/models/Collection.ts +++ b/server/models/Collection.ts @@ -840,6 +840,7 @@ class Collection extends ParanoidModel< silent?: boolean; documentJson?: NavigationNode; includeArchived?: boolean; + insertOrder?: "prepend" | "append"; } = {} ) { if (!this.documentStructure) { @@ -856,24 +857,36 @@ class Collection extends ParanoidModel< ...options.documentJson, }; + // Determine the insertion index based on order parameter or explicit index + let insertionIndex: number; + + if (index !== undefined) { + // Explicit index takes precedence + insertionIndex = index; + } else if (options.insertOrder === "prepend") { + // Prepend to the beginning + insertionIndex = 0; + } else { + // Default behavior: append to the end (maintains backward compatibility) + insertionIndex = this.documentStructure.length; + } + if (!document.parentDocumentId) { // Note: Index is supported on DB level but it's being ignored // by the API presentation until we build product support for it. - this.documentStructure.splice( - index !== undefined ? index : this.documentStructure.length, - 0, - documentJson - ); + this.documentStructure.splice(insertionIndex, 0, documentJson); } else { // Recursively place document const placeDocument = (documentList: NavigationNode[]) => documentList.map((childDocument) => { if (document.parentDocumentId === childDocument.id) { - childDocument.children.splice( - index !== undefined ? index : childDocument.children.length, - 0, - documentJson - ); + const childInsertionIndex = + index !== undefined + ? index + : options.insertOrder === "prepend" + ? 0 + : childDocument.children.length; + childDocument.children.splice(childInsertionIndex, 0, documentJson); } else { childDocument.children = placeDocument(childDocument.children); } diff --git a/server/queues/processors/ImportsProcessor.ts b/server/queues/processors/ImportsProcessor.ts index 3a64dc9238..416c6a0d4b 100644 --- a/server/queues/processors/ImportsProcessor.ts +++ b/server/queues/processors/ImportsProcessor.ts @@ -181,10 +181,11 @@ export default abstract class ImportsProcessor< }, async (documents) => { for (const document of documents) { - await collection.addDocumentToStructure(document, 0, { + await collection.addDocumentToStructure(document, undefined, { save: false, silent: true, transaction, + insertOrder: "append", }); } } diff --git a/server/queues/tasks/ImportTask.ts b/server/queues/tasks/ImportTask.ts index 2e47006a56..6a25c33dc5 100644 --- a/server/queues/tasks/ImportTask.ts +++ b/server/queues/tasks/ImportTask.ts @@ -473,9 +473,10 @@ export default abstract class ImportTask extends BaseTask { }); documents.set(item.id, document); - await collection.addDocumentToStructure(document, 0, { + await collection.addDocumentToStructure(document, undefined, { transaction, save: false, + insertOrder: "append", }); }