From 13a6f89640f4a2d8e82919b574ba57ed5785ff54 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 23 Nov 2023 11:28:11 -0500 Subject: [PATCH] fix: Deleted unpublished drafts in trash --- server/models/User.ts | 10 +++++-- server/routes/api/documents/documents.test.ts | 10 ++++++- server/routes/api/documents/documents.ts | 29 +++++++++---------- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/server/models/User.ts b/server/models/User.ts index 142f3bc35..8388f17d9 100644 --- a/server/models/User.ts +++ b/server/models/User.ts @@ -2,7 +2,13 @@ import crypto from "crypto"; import { addHours, addMinutes, subMinutes } from "date-fns"; import JWT from "jsonwebtoken"; import { Context } from "koa"; -import { Transaction, QueryTypes, SaveOptions, Op } from "sequelize"; +import { + Transaction, + QueryTypes, + SaveOptions, + Op, + FindOptions, +} from "sequelize"; import { Table, Column, @@ -361,7 +367,7 @@ class User extends ParanoidModel { UserPreferenceDefaults[preference] ?? false; - collectionIds = async (options = {}) => { + collectionIds = async (options: FindOptions = {}) => { const collectionStubs = await Collection.scope({ method: ["withMembership", this.id], }).findAll({ diff --git a/server/routes/api/documents/documents.test.ts b/server/routes/api/documents/documents.test.ts index f014f7a0a..41cab9abf 100644 --- a/server/routes/api/documents/documents.test.ts +++ b/server/routes/api/documents/documents.test.ts @@ -1841,8 +1841,9 @@ describe("#documents.deleted", () => { expect(body.data.length).toEqual(1); }); - it("should return deleted documents, including drafts without collection", async () => { + it("should return deleted documents, including users drafts without collection", async () => { const user = await buildUser(); + const user2 = await buildUser(); const document = await buildDocument({ userId: user.id, teamId: user.teamId, @@ -1850,10 +1851,17 @@ describe("#documents.deleted", () => { const draftDocument = await buildDraftDocument({ userId: user.id, teamId: user.teamId, + collectionId: null, + }); + const otherUserDraft = await buildDraftDocument({ + userId: user2.id, + teamId: user.teamId, + collectionId: null, }); await Promise.all([ document.delete(user.id), draftDocument.delete(user.id), + otherUserDraft.delete(user2.id), ]); const res = await server.post("/api/documents.deleted", { body: { diff --git a/server/routes/api/documents/documents.ts b/server/routes/api/documents/documents.ts index 8654c3b54..a82bb88f1 100644 --- a/server/routes/api/documents/documents.ts +++ b/server/routes/api/documents/documents.ts @@ -243,28 +243,27 @@ router.post( const documents = await Document.scope([ collectionScope, viewScope, + "withDrafts", ]).findAll({ where: { teamId: user.teamId, - collectionId: { - [Op.or]: [{ [Op.in]: collectionIds }, { [Op.is]: null }], - }, deletedAt: { [Op.ne]: null, }, + [Op.or]: [ + { + collectionId: { + [Op.in]: collectionIds, + }, + }, + { + createdById: user.id, + collectionId: { + [Op.is]: null, + }, + }, + ], }, - include: [ - { - model: User, - as: "createdBy", - paranoid: false, - }, - { - model: User, - as: "updatedBy", - paranoid: false, - }, - ], paranoid: false, order: [[sort, direction]], offset: ctx.state.pagination.offset,