From 69bcc2f3f447b6e941d9d36bdc4ddcf27ef2eedc Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 19 Sep 2026 08:40:01 -0400 Subject: [PATCH] fix: Create sidebar memberships for personal documents moved to the root --- server/models/Document.ts | 22 +++++---- server/routes/api/documents/documents.test.ts | 45 +++++++++++++++++++ 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/server/models/Document.ts b/server/models/Document.ts index e09d0a6568..879a49e4b4 100644 --- a/server/models/Document.ts +++ b/server/models/Document.ts @@ -563,6 +563,20 @@ class Document extends ArchivableModel< @AfterCreate @AfterUpdate static async cascadePersonalOwner(model: Document, ctx: HookContext) { + // A child moved to the root needs its own sidebar record even when it + // stays in the same personal space. + if ( + model.personalOwnerId && + !model.parentDocumentId && + (model.changed("personalOwnerId") || model.changed("parentDocumentId")) + ) { + await UserMembership.findOrCreateForPersonalDocument( + model, + model.personalOwnerId, + ctx + ); + } + if (!model.changed("personalOwnerId")) { return; } @@ -585,14 +599,6 @@ class Document extends ArchivableModel< ); } - if (personalOwnerId && !model.parentDocumentId) { - await UserMembership.findOrCreateForPersonalDocument( - model, - personalOwnerId, - ctx - ); - } - const previousOwnerId = model.previous("personalOwnerId"); if (previousOwnerId && previousOwnerId !== personalOwnerId) { // the sidebar membership would otherwise linger once the document has diff --git a/server/routes/api/documents/documents.test.ts b/server/routes/api/documents/documents.test.ts index bfbaf4b64b..290c51a6a5 100644 --- a/server/routes/api/documents/documents.test.ts +++ b/server/routes/api/documents/documents.test.ts @@ -9629,6 +9629,51 @@ describe("#documents.documents - personal", () => { }); describe("#documents.move - personal", () => { + it("should create a sidebar membership when a personal child moves to the root", async () => { + const user = await buildUser(); + const parent = await buildPersonalDocument({ + teamId: user.teamId, + userId: user.id, + }); + const child = await buildPersonalDocument({ + teamId: user.teamId, + userId: user.id, + parentDocumentId: parent.id, + }); + const grandchild = await buildPersonalDocument({ + teamId: user.teamId, + userId: user.id, + parentDocumentId: child.id, + }); + + const res = await server.post("/api/documents.move", user, { + body: { id: child.id, personalOwnerId: user.id }, + }); + expect(res.status).toEqual(200); + + const membership = await UserMembership.findOne({ + where: { documentId: child.id, userId: user.id, sourceId: null }, + rejectOnEmpty: true, + }); + expect(membership.index).toBeTruthy(); + + const list = await server.post("/api/documents.personal", user, { + body: {}, + }); + const body = await list.json(); + expect(body.data.memberships).toEqual( + expect.arrayContaining([expect.objectContaining({ id: membership.id })]) + ); + + const tree = await server.post("/api/documents.documents", user, { + body: { id: child.id }, + }); + const treeBody = await tree.json(); + expect(treeBody.data.children).toEqual([ + expect.objectContaining({ id: grandchild.id }), + ]); + }); + it("should move a published document and its children into the personal space", async () => { const user = await buildUser(); const collection = await buildCollection({