diff --git a/server/commands/documentUpdater.test.ts b/server/commands/documentUpdater.test.ts index 6083335bcf..c7feb44f51 100644 --- a/server/commands/documentUpdater.test.ts +++ b/server/commands/documentUpdater.test.ts @@ -12,7 +12,11 @@ import { Document, Event } from "@server/models"; import { parser, schema } from "@server/editor"; import { DocumentHelper } from "@server/models/helpers/DocumentHelper"; import { ProsemirrorHelper } from "@server/models/helpers/ProsemirrorHelper"; -import { buildDocument, buildUser } from "@server/test/factories"; +import { + buildCollection, + buildDocument, + buildUser, +} from "@server/test/factories"; import { withAPIContext } from "@server/test/support"; import documentUpdater from "./documentUpdater"; @@ -192,6 +196,37 @@ describe("documentUpdater", () => { notifyUpdateSpy.mockRestore(); }); + it("should keep the loaded collection when the title changes", async () => { + const user = await buildUser(); + const collection = await buildCollection({ + userId: user.id, + teamId: user.teamId, + }); + const { id } = await buildDocument({ + userId: user.id, + teamId: user.teamId, + collectionId: collection.id, + }); + const document = await Document.findByPk(id, { + userId: user.id, + rejectOnEmpty: true, + }); + const loadedCollection = document.collection; + + const result = await withAPIContext(user, (ctx) => + documentUpdater(ctx, { + title: "New Title", + document, + }) + ); + + expect(result).toBe(document); + expect(result.collection).toBe(loadedCollection); + expect(result.collection?.documentStructure?.[0].title).toEqual( + "New Title" + ); + }); + describe("replace", () => { it("should update document content when changing text", async () => { const user = await buildUser(); diff --git a/server/commands/documentUpdater.ts b/server/commands/documentUpdater.ts index f99d38012e..84abe17c08 100644 --- a/server/commands/documentUpdater.ts +++ b/server/commands/documentUpdater.ts @@ -178,9 +178,8 @@ export default async function documentUpdater( } // Publishing can move the document to another collection, and the save - // hooks that maintain the collection structure replace the loaded - // collection with one that has no membership data. Reload in those cases - // so that policies stay accurate. + // hooks can attach a collection that has no membership data. Reload in + // those cases so that policies stay accurate. if (publish || document.collection !== collection) { return await Document.findByPk(document.id, { userId: user.id, diff --git a/server/models/Document.ts b/server/models/Document.ts index a3809603f5..6bc696b829 100644 --- a/server/models/Document.ts +++ b/server/models/Document.ts @@ -542,7 +542,13 @@ class Document extends ArchivableModel< } await collection.updateDocument(model, { transaction }); - model.collection = collection; + + // Keep a loaded collection so that its membership data stays available. + if (model.collection?.id === collection.id) { + model.collection.documentStructure = collection.documentStructure; + } else { + model.collection = collection; + } } @AfterCreate