Skip to content

Commit

Permalink
fix: Deleting a collection should not deleted archived documents with…
Browse files Browse the repository at this point in the history
…in it automatially (#1776)

closes #1775
  • Loading branch information
tommoor committed Jan 8, 2021
1 parent cbfa25f commit 34598b3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
2 changes: 0 additions & 2 deletions app/components/Breadcrumb.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ const Breadcrumb = ({ document, onlyText }: Props) => {

let collection = collections.get(document.collectionId);
if (!collection) {
if (!document.deletedAt) return <div />;

collection = {
id: document.collectionId,
name: t("Deleted Collection"),
Expand Down
46 changes: 41 additions & 5 deletions server/api/collections.test.js
Original file line number Diff line number Diff line change
@@ -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());

Expand Down Expand Up @@ -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,
});
Expand All @@ -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({
Expand Down
5 changes: 4 additions & 1 deletion server/models/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -182,6 +182,9 @@ Collection.addHook("afterDestroy", async (model: Collection) => {
await Document.destroy({
where: {
collectionId: model.id,
archivedAt: {
[Op.eq]: null,
},
},
});
});
Expand Down

0 comments on commit 34598b3

Please sign in to comment.