Deleting a collection should detach associated drafts from it (#5082)

Co-authored-by: Tom Moor <tom.moor@gmail.com>
This commit is contained in:
Apoorv Mishra
2023-04-24 00:50:44 +05:30
committed by GitHub
parent 7250c0ed64
commit 86062f396d
39 changed files with 363 additions and 112 deletions

View File

@@ -0,0 +1,33 @@
import { Document } from "@server/models";
import { buildCollection, buildDocument } from "@server/test/factories";
import { setupTestDatabase } from "@server/test/support";
import DetachDraftsFromCollectionTask from "./DetachDraftsFromCollectionTask";
setupTestDatabase();
describe("DetachDraftsFromCollectionTask", () => {
const ip = "127.0.0.1";
it("should detach drafts from deleted collection", async () => {
const collection = await buildCollection();
const document = await buildDocument({
title: "test",
collectionId: collection.id,
publishedAt: null,
createdById: collection.createdById,
teamId: collection.teamId,
});
await collection.destroy();
const task = new DetachDraftsFromCollectionTask();
await task.perform({
collectionId: collection.id,
ip,
actorId: collection.createdById,
});
const draft = await Document.findByPk(document.id);
expect(draft).not.toBe(null);
expect(draft?.deletedAt).toBe(null);
expect(draft?.collectionId).toBe(null);
});
});

View File

@@ -0,0 +1,48 @@
import { Op } from "sequelize";
import documentMover from "@server/commands/documentMover";
import { sequelize } from "@server/database/sequelize";
import { Collection, Document, User } from "@server/models";
import BaseTask from "./BaseTask";
type Props = {
collectionId: string;
actorId: string;
ip: string;
};
export default class DetachDraftsFromCollectionTask extends BaseTask<Props> {
async perform(props: Props) {
const [collection, actor] = await Promise.all([
Collection.findByPk(props.collectionId, {
paranoid: false,
}),
User.findByPk(props.actorId),
]);
if (!actor || !collection || !collection.deletedAt) {
return;
}
const documents = await Document.scope("withDrafts").findAll({
where: {
collectionId: props.collectionId,
publishedAt: {
[Op.is]: null,
},
},
paranoid: false,
});
return sequelize.transaction(async (transaction) => {
for (const document of documents) {
await documentMover({
document,
user: actor,
ip: props.ip,
collectionId: null,
transaction,
});
}
});
}
}

View File

@@ -31,7 +31,7 @@ describe("documents.publish", () => {
await processor.perform({
name: "documents.publish",
documentId: document.id,
collectionId: document.collectionId,
collectionId: document.collectionId!,
teamId: document.teamId,
actorId: document.createdById,
data: {
@@ -55,7 +55,7 @@ describe("documents.publish", () => {
await processor.perform({
name: "documents.publish",
documentId: document.id,
collectionId: document.collectionId,
collectionId: document.collectionId!,
teamId: document.teamId,
actorId: document.createdById,
data: {
@@ -95,7 +95,7 @@ describe("documents.publish", () => {
await processor.perform({
name: "documents.publish",
documentId: document.id,
collectionId: document.collectionId,
collectionId: document.collectionId!,
teamId: document.teamId,
actorId: document.createdById,
data: {
@@ -124,7 +124,7 @@ describe("documents.publish", () => {
await processor.perform({
name: "documents.publish",
documentId: document.id,
collectionId: document.collectionId,
collectionId: document.collectionId!,
teamId: document.teamId,
actorId: document.createdById,
data: {

View File

@@ -34,7 +34,7 @@ describe("revisions.create", () => {
await task.perform({
name: "revisions.create",
documentId: document.id,
collectionId: document.collectionId,
collectionId: document.collectionId!,
teamId: document.teamId,
actorId: collaborator.id,
modelId: revision.id,
@@ -63,7 +63,7 @@ describe("revisions.create", () => {
await task.perform({
name: "revisions.create",
documentId: document.id,
collectionId: document.collectionId,
collectionId: document.collectionId!,
teamId: document.teamId,
actorId: collaborator.id,
modelId: revision.id,
@@ -88,7 +88,7 @@ describe("revisions.create", () => {
await task.perform({
name: "revisions.create",
documentId: document.id,
collectionId: document.collectionId,
collectionId: document.collectionId!,
teamId: document.teamId,
actorId: user.id,
modelId: revision.id,
@@ -123,7 +123,7 @@ describe("revisions.create", () => {
await task.perform({
name: "revisions.create",
documentId: document.id,
collectionId: document.collectionId,
collectionId: document.collectionId!,
teamId: document.teamId,
actorId: collaborator.id,
modelId: revision.id,
@@ -152,7 +152,7 @@ describe("revisions.create", () => {
await task.perform({
name: "revisions.create",
documentId: document.id,
collectionId: document.collectionId,
collectionId: document.collectionId!,
teamId: document.teamId,
actorId: collaborator0.id,
modelId: revision.id,
@@ -202,7 +202,7 @@ describe("revisions.create", () => {
await task.perform({
name: "revisions.create",
documentId: document.id,
collectionId: document.collectionId,
collectionId: document.collectionId!,
teamId: document.teamId,
actorId: collaborator0.id,
modelId: revision.id,
@@ -247,7 +247,7 @@ describe("revisions.create", () => {
await task.perform({
name: "revisions.create",
documentId: document.id,
collectionId: document.collectionId,
collectionId: document.collectionId!,
teamId: document.teamId,
actorId: collaborator0.id,
modelId: revision.id,
@@ -303,7 +303,7 @@ describe("revisions.create", () => {
await task.perform({
name: "revisions.create",
documentId: document.id,
collectionId: document.collectionId,
collectionId: document.collectionId!,
teamId: document.teamId,
actorId: collaborator.id,
modelId: revision.id,
@@ -345,7 +345,7 @@ describe("revisions.create", () => {
await task.perform({
name: "revisions.create",
documentId: document.id,
collectionId: document.collectionId,
collectionId: document.collectionId!,
teamId: document.teamId,
actorId: collaborator.id,
modelId: revision.id,
@@ -391,7 +391,7 @@ describe("revisions.create", () => {
await task.perform({
name: "revisions.create",
documentId: document.id,
collectionId: document.collectionId,
collectionId: document.collectionId!,
teamId: document.teamId,
actorId: collaborator.id,
modelId: revision.id,
@@ -421,7 +421,7 @@ describe("revisions.create", () => {
await task.perform({
name: "revisions.create",
documentId: document.id,
collectionId: document.collectionId,
collectionId: document.collectionId!,
teamId: document.teamId,
actorId: collaborator.id,
modelId: revision.id,
@@ -444,7 +444,7 @@ describe("revisions.create", () => {
await task.perform({
name: "revisions.create",
documentId: document.id,
collectionId: document.collectionId,
collectionId: document.collectionId!,
teamId: document.teamId,
actorId: user.id,
modelId: revision.id,