fix: Deleting a collection should not deleted archived documents within it automatially (#1776)

closes #1775
This commit is contained in:
Tom Moor
2021-01-07 19:46:12 -08:00
committed by GitHub
parent cbfa25fa2f
commit 34598b317d
3 changed files with 45 additions and 8 deletions

View File

@ -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"),

View File

@ -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({

View File

@ -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,
},
},
});
});