diff --git a/app/components/Breadcrumb.js b/app/components/Breadcrumb.js index 33f12809..16f5d9e0 100644 --- a/app/components/Breadcrumb.js +++ b/app/components/Breadcrumb.js @@ -89,8 +89,6 @@ const Breadcrumb = ({ document, onlyText }: Props) => { let collection = collections.get(document.collectionId); if (!collection) { - if (!document.deletedAt) return
; - collection = { id: document.collectionId, name: t("Deleted Collection"), diff --git a/server/api/collections.test.js b/server/api/collections.test.js index 2da9c62b..a0b26e11 100644 --- a/server/api/collections.test.js +++ b/server/api/collections.test.js @@ -1,8 +1,13 @@ /* eslint-disable flowtype/require-valid-file-annotation */ import TestServer from "fetch-test-server"; import app from "../app"; -import { Collection, CollectionUser, CollectionGroup } from "../models"; -import { buildUser, buildGroup, buildCollection } from "../test/factories"; +import { Document, CollectionUser, CollectionGroup } from "../models"; +import { + buildUser, + buildGroup, + buildCollection, + buildDocument, +} from "../test/factories"; import { flushdb, seed } from "../test/support"; const server = new TestServer(app.callback()); @@ -1100,9 +1105,9 @@ describe("#collections.delete", () => { it("should delete collection", async () => { const { user, collection } = await seed(); - await Collection.create({ - name: "Blah", - urlId: "blah", + + // to ensure it isn't the last collection + await buildCollection({ teamId: user.teamId, creatorId: user.id, }); @@ -1116,6 +1121,37 @@ describe("#collections.delete", () => { expect(body.success).toBe(true); }); + it("should delete published documents", async () => { + const { user, collection } = await seed(); + + // to ensure it isn't the last collection + await buildCollection({ + teamId: user.teamId, + creatorId: user.id, + }); + + // archived document should not be deleted + await buildDocument({ + collectionId: collection.id, + archivedAt: new Date(), + }); + + const res = await server.post("/api/collections.delete", { + body: { token: user.getJwtToken(), id: collection.id }, + }); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.success).toBe(true); + expect( + await Document.count({ + where: { + collectionId: collection.id, + }, + }) + ).toEqual(1); + }); + it("allows deleting by read-write collection group user", async () => { const user = await buildUser(); const collection = await buildCollection({ diff --git a/server/models/Collection.js b/server/models/Collection.js index 2b68f530..c14b361e 100644 --- a/server/models/Collection.js +++ b/server/models/Collection.js @@ -2,7 +2,7 @@ import { find, findIndex, concat, remove, uniq } from "lodash"; import randomstring from "randomstring"; import slug from "slug"; -import { DataTypes, sequelize } from "../sequelize"; +import { Op, DataTypes, sequelize } from "../sequelize"; import CollectionUser from "./CollectionUser"; import Document from "./Document"; @@ -182,6 +182,9 @@ Collection.addHook("afterDestroy", async (model: Collection) => { await Document.destroy({ where: { collectionId: model.id, + archivedAt: { + [Op.eq]: null, + }, }, }); });