diff --git a/server/routes/api/documents/documents.test.ts b/server/routes/api/documents/documents.test.ts index 7d4aa9bbb0..c7f2b8b053 100644 --- a/server/routes/api/documents/documents.test.ts +++ b/server/routes/api/documents/documents.test.ts @@ -40,6 +40,7 @@ import { DocumentHelper } from "@server/models/helpers/DocumentHelper"; import HTMLHelper from "@server/models/helpers/HTMLHelper"; import { ProsemirrorHelper } from "@server/models/helpers/ProsemirrorHelper"; import DocumentImportTask from "@server/queues/tasks/DocumentImportTask"; +import EmptyTrashTask from "@server/queues/tasks/EmptyTrashTask"; import { sequelize } from "@server/storage/database"; import FileStorage from "@server/storage/files"; import { @@ -10143,6 +10144,30 @@ describe("#documents.empty_trash", () => { expect(res.status).toEqual(403); expect(body).toMatchSnapshot(); }); + it("should not delete trashed drafts of other collection members", async () => { + const admin = await buildAdmin(); + const other = await buildUser({ teamId: admin.teamId }); + const collection = await buildCollection({ teamId: admin.teamId }); + const otherDraft = await buildDraftDocument({ + userId: other.id, + teamId: admin.teamId, + collectionId: collection.id, + }); + const otherDocument = await buildDocument({ + userId: other.id, + teamId: admin.teamId, + collectionId: collection.id, + }); + await withContext(other, (ctx) => otherDraft.destroyWithCtx(ctx)); + await withContext(other, (ctx) => otherDocument.destroyWithCtx(ctx)); + const schedule = vi.spyOn(EmptyTrashTask.prototype, "schedule"); + + const res = await server.post("/api/documents.empty_trash", admin); + expect(res.status).toEqual(200); + expect(schedule).toHaveBeenCalledWith({ + documentIds: [otherDocument.id], + }); + }); }); describe("#documents.documents", () => { diff --git a/server/routes/api/documents/documents.ts b/server/routes/api/documents/documents.ts index 631f62e0d9..0db52a7833 100644 --- a/server/routes/api/documents/documents.ts +++ b/server/routes/api/documents/documents.ts @@ -2356,17 +2356,27 @@ router.post( deletedAt: { [Op.ne]: null, }, - [Op.or]: [ + [Op.and]: [ { - collectionId: { - [Op.in]: collectionIds, - }, + [Op.or]: [ + { + collectionId: { + [Op.in]: collectionIds, + }, + }, + { + createdById: user.id, + collectionId: { + [Op.is]: null, + }, + }, + ], }, { - createdById: user.id, - collectionId: { - [Op.is]: null, - }, + [Op.or]: [ + { publishedAt: { [Op.ne]: null } }, + ...(await draftVisibilityClauses(user)), + ], }, ], },