diff --git a/server/routes/api/documents/documents.test.ts b/server/routes/api/documents/documents.test.ts index 5af9098827..28ea064eed 100644 --- a/server/routes/api/documents/documents.test.ts +++ b/server/routes/api/documents/documents.test.ts @@ -2944,6 +2944,32 @@ describe("#documents.drafts", () => { expect(body.data.length).toEqual(drafts.length); }); + it("should only return drafts in the given collection", async () => { + const user = await buildUser(); + const collection = await buildCollection({ + userId: user.id, + teamId: user.teamId, + }); + const document = await buildDraftDocument({ + userId: user.id, + collectionId: collection.id, + teamId: user.teamId, + }); + await buildDraftDocument({ + userId: user.id, + collectionId: null, + teamId: user.teamId, + }); + + const res = await server.post("/api/documents.drafts", user, { + body: { collectionId: collection.id }, + }); + const body = await res.json(); + expect(res.status).toEqual(200); + expect(body.data.length).toEqual(1); + expect(body.data[0].id).toEqual(document.id); + }); + it("should not return drafts in private collections not a member of", async () => { const user = await buildUser(); const collection = await buildCollection({ diff --git a/server/routes/api/documents/documents.ts b/server/routes/api/documents/documents.ts index 6526c6131d..85c6677bc4 100644 --- a/server/routes/api/documents/documents.ts +++ b/server/routes/api/documents/documents.ts @@ -662,15 +662,17 @@ router.post( authorize(user, "readDocument", collection); } - const collectionIds = collectionId - ? [collectionId] - : await user.collectionIds(); const where: WhereOptions = { teamId: user.teamId, createdById: user.id, - collectionId: { - [Op.or]: [{ [Op.in]: collectionIds }, { [Op.is]: null }], - }, + collectionId: collectionId + ? collectionId + : { + [Op.or]: [ + { [Op.in]: await user.collectionIds() }, + { [Op.is]: null }, + ], + }, publishedAt: { [Op.is]: null, },