fix: Correct user on documents in deleted collection (#6116)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Transaction } from "sequelize";
|
||||
import { Collection, Event, User } from "@server/models";
|
||||
import { Transaction, Op } from "sequelize";
|
||||
import { Collection, Document, Event, User } from "@server/models";
|
||||
|
||||
type Props = {
|
||||
/** The collection to delete */
|
||||
@@ -20,6 +20,23 @@ export default async function collectionDestroyer({
|
||||
}: Props) {
|
||||
await collection.destroy({ transaction });
|
||||
|
||||
await Document.update(
|
||||
{
|
||||
lastModifiedById: user.id,
|
||||
deletedAt: new Date(),
|
||||
},
|
||||
{
|
||||
transaction,
|
||||
where: {
|
||||
teamId: collection.teamId,
|
||||
collectionId: collection.id,
|
||||
archivedAt: {
|
||||
[Op.is]: null,
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
await Event.create(
|
||||
{
|
||||
name: "collections.delete",
|
||||
|
||||
@@ -20,7 +20,6 @@ import {
|
||||
Default,
|
||||
BeforeValidate,
|
||||
BeforeSave,
|
||||
AfterDestroy,
|
||||
AfterCreate,
|
||||
HasMany,
|
||||
BelongsToMany,
|
||||
@@ -279,18 +278,6 @@ class Collection extends ParanoidModel {
|
||||
}
|
||||
}
|
||||
|
||||
@AfterDestroy
|
||||
static async onAfterDestroy(model: Collection) {
|
||||
await Document.destroy({
|
||||
where: {
|
||||
collectionId: model.id,
|
||||
archivedAt: {
|
||||
[Op.is]: null,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@AfterCreate
|
||||
static async onAfterCreate(
|
||||
model: Collection,
|
||||
|
||||
@@ -2,12 +2,19 @@ import teamUpdater from "@server/commands/teamUpdater";
|
||||
import { Team, User } from "@server/models";
|
||||
import { sequelize } from "@server/storage/database";
|
||||
import { Event as TEvent, CollectionEvent } from "@server/types";
|
||||
import DetachDraftsFromCollectionTask from "../tasks/DetachDraftsFromCollectionTask";
|
||||
import BaseProcessor from "./BaseProcessor";
|
||||
|
||||
export default class CollectionDeletedProcessor extends BaseProcessor {
|
||||
static applicableEvents: TEvent["name"][] = ["collections.delete"];
|
||||
|
||||
async perform(event: CollectionEvent) {
|
||||
await DetachDraftsFromCollectionTask.schedule({
|
||||
collectionId: event.collectionId,
|
||||
actorId: event.actorId,
|
||||
ip: event.ip,
|
||||
});
|
||||
|
||||
await sequelize.transaction(async (transaction) => {
|
||||
const team = await Team.findByPk(event.teamId, {
|
||||
rejectOnEmpty: true,
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
import { CollectionEvent, Event } from "@server/types";
|
||||
import DetachDraftsFromCollectionTask from "../tasks/DetachDraftsFromCollectionTask";
|
||||
import BaseProcessor from "./BaseProcessor";
|
||||
|
||||
export default class CollectionsProcessor extends BaseProcessor {
|
||||
static applicableEvents: Event["name"][] = ["collections.delete"];
|
||||
|
||||
async perform(event: CollectionEvent) {
|
||||
switch (event.name) {
|
||||
case "collections.delete":
|
||||
return this.collectionDeleted(event);
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
async collectionDeleted(event: CollectionEvent) {
|
||||
await DetachDraftsFromCollectionTask.schedule({
|
||||
collectionId: event.collectionId,
|
||||
actorId: event.actorId,
|
||||
ip: event.ip,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user