Fix: nested document order when duplicating (#10543)

* duplicate nested documents in correct order

* include document structure

* use more accurate structure

* minor fix

* debug test on remote

* remove failing test

* fix test

* fix test

* fix test

* fix tests
This commit is contained in:
Salihu authored and GitHub committed 2025-11-08 11:06:42 -05:00
1 parent c44a3c0f69
commit 636153a56b
3 files changed
+90 -8

No files matched your search

+45 -6
View File
@@ -1,6 +1,10 @@
import { createContext } from "@server/context";
import { sequelize } from "@server/storage/database";
import { buildDocument, buildUser } from "@server/test/factories";
import {
buildCollection,
buildDocument,
buildUser,
} from "@server/test/factories";
import documentDuplicator from "./documentDuplicator";
describe("documentDuplicator", () => {
@@ -54,23 +58,50 @@ describe("documentDuplicator", () => {
expect(response[0].publishedAt).toBeInstanceOf(Date);
});
it("should duplicate child documents with recursive=true", async () => {
it("should duplicate child documents, in the correct order with recursive=true", async () => {
const user = await buildUser();
const collection = await buildCollection({
teamId: user.teamId,
userId: user.id,
});
const original = await buildDocument({
userId: user.id,
teamId: user.teamId,
icon: "👋",
title: "doc 1",
collectionId: collection.id,
});
await buildDocument({
const child1 = await buildDocument({
userId: user.id,
teamId: user.teamId,
parentDocumentId: original.id,
collection: original.collection,
title: "doc 1.1",
});
const response = await sequelize.transaction((transaction) =>
const child2 = await buildDocument({
userId: user.id,
teamId: user.teamId,
parentDocumentId: original.id,
title: "doc 1.2",
});
const child3 = await buildDocument({
userId: user.id,
teamId: user.teamId,
parentDocumentId: original.id,
title: "doc 1.3",
});
await collection.addDocumentToStructure(original);
await collection.addDocumentToStructure(child1);
await collection.addDocumentToStructure(child2);
await collection.addDocumentToStructure(child3);
await sequelize.transaction((transaction) =>
documentDuplicator({
title: "duplicate",
document: original,
collection: original.collection,
user,
@@ -79,7 +110,15 @@ describe("documentDuplicator", () => {
})
);
expect(response).toHaveLength(2);
await collection.reload();
const duplicate = collection.documentStructure![0];
const childTitles = duplicate.children!.map((child) => child.title);
expect(duplicate.title).toEqual("duplicate");
expect(childTitles.length).toBe(3);
expect(childTitles[0]).toBe(child1.title);
expect(childTitles[1]).toBe(child2.title);
expect(childTitles[2]).toBe(child3.title);
});
it("should duplicate existing document as draft", async () => {
+14 -1
View File
@@ -62,6 +62,14 @@ export default async function documentDuplicator({
duplicated.collection = collection ?? null;
newDocuments.push(duplicated);
const originalCollection = document?.collectionId
? await Collection.findByPk(document.collectionId, {
attributes: {
include: ["documentStructure"],
},
})
: null;
async function duplicateChildDocuments(
original: Document,
duplicatedDocument: Document
@@ -79,7 +87,12 @@ export default async function documentDuplicator({
ctx
);
for (const childDocument of childDocuments) {
const sorted = DocumentHelper.sortDocumentsByStructure(
childDocuments,
originalCollection?.getDocumentTree(original.id)?.children ?? []
).reverse(); // we have to reverse since the child documents will be added in reverse order
for (const childDocument of sorted) {
const duplicatedChildDocument = await documentCreator({
parentDocumentId: duplicatedDocument.id,
icon: childDocument.icon,
+31 -1
View File
@@ -5,7 +5,7 @@ import { updateYFragment, yDocToProsemirrorJSON } from "y-prosemirror";
import * as Y from "yjs";
import textBetween from "@shared/editor/lib/textBetween";
import { EditorStyleHelper } from "@shared/editor/styles/EditorStyleHelper";
import { IconType, ProsemirrorData } from "@shared/types";
import { IconType, NavigationNode, ProsemirrorData } from "@shared/types";
import { determineIconType } from "@shared/utils/icon";
import { parser, serializer, schema } from "@server/editor";
import { addTags } from "@server/logging/tracer";
@@ -521,4 +521,34 @@ export class DocumentHelper {
const distance = ukkonen(first, second, threshold + 1);
return distance > threshold;
}
/**
* Sorts an array of documents based on their order in the collection's document structure.
* Documents are ordered according to their position in the navigation structure, with
* documents not found in the structure placed at the end. The result is reversed to
* account for documents being added in reverse order during processing.
*
* @param documents - Array of Document objects to be sorted
* @param documentStructure - Array of NavigationNode objects representing the collection's document hierarchy
* @returns Sorted array of documents in the order they appear in the document structure
*
**/
public static sortDocumentsByStructure(
documents: Document[],
documentStructure: NavigationNode[]
): Document[] {
if (!documentStructure.length) {return documents;}
const orderMap = new Map<string, number>();
documentStructure.forEach((node, index) => {
orderMap.set(node.id, index);
});
return documents.sort((a, b) => {
const orderA = orderMap.get(a.id) ?? Number.MAX_SAFE_INTEGER;
const orderB = orderMap.get(b.id) ?? Number.MAX_SAFE_INTEGER;
return orderA - orderB;
});
}
}