feat: Allow deletion of imports (#5907)
This commit is contained in:
35
server/queues/processors/CollectionDeletedProcessor.ts
Normal file
35
server/queues/processors/CollectionDeletedProcessor.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
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 BaseProcessor from "./BaseProcessor";
|
||||
|
||||
export default class CollectionDeletedProcessor extends BaseProcessor {
|
||||
static applicableEvents: TEvent["name"][] = ["collections.delete"];
|
||||
|
||||
async perform(event: CollectionEvent) {
|
||||
await sequelize.transaction(async (transaction) => {
|
||||
const team = await Team.findByPk(event.teamId, {
|
||||
rejectOnEmpty: true,
|
||||
transaction,
|
||||
lock: transaction.LOCK.UPDATE,
|
||||
});
|
||||
|
||||
if (team?.defaultCollectionId === event.collectionId) {
|
||||
const user = await User.findByPk(event.actorId, {
|
||||
rejectOnEmpty: true,
|
||||
paranoid: false,
|
||||
transaction,
|
||||
});
|
||||
|
||||
await teamUpdater({
|
||||
params: { defaultCollectionId: null },
|
||||
user,
|
||||
team,
|
||||
transaction,
|
||||
ip: event.ip,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import { Notification } from "@server/models";
|
||||
import { Event, NotificationEvent } from "@server/types";
|
||||
import BaseProcessor from "./BaseProcessor";
|
||||
|
||||
export default class NotificationsProcessor extends BaseProcessor {
|
||||
export default class EmailsProcessor extends BaseProcessor {
|
||||
static applicableEvents: Event["name"][] = ["notifications.create"];
|
||||
|
||||
async perform(event: NotificationEvent) {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import invariant from "invariant";
|
||||
import { FileOperationFormat, FileOperationType } from "@shared/types";
|
||||
import { FileOperation } from "@server/models";
|
||||
import { Event as TEvent, FileOperationEvent } from "@server/types";
|
||||
@@ -10,16 +9,13 @@ import ImportMarkdownZipTask from "../tasks/ImportMarkdownZipTask";
|
||||
import ImportNotionTask from "../tasks/ImportNotionTask";
|
||||
import BaseProcessor from "./BaseProcessor";
|
||||
|
||||
export default class FileOperationsProcessor extends BaseProcessor {
|
||||
export default class FileOperationCreatedProcessor extends BaseProcessor {
|
||||
static applicableEvents: TEvent["name"][] = ["fileOperations.create"];
|
||||
|
||||
async perform(event: FileOperationEvent) {
|
||||
if (event.name !== "fileOperations.create") {
|
||||
return;
|
||||
}
|
||||
|
||||
const fileOperation = await FileOperation.findByPk(event.modelId);
|
||||
invariant(fileOperation, "fileOperation not found");
|
||||
const fileOperation = await FileOperation.findByPk(event.modelId, {
|
||||
rejectOnEmpty: true,
|
||||
});
|
||||
|
||||
// map file operation type and format to the appropriate task
|
||||
if (fileOperation.type === FileOperationType.Import) {
|
||||
54
server/queues/processors/FileOperationDeletedProcessor.ts
Normal file
54
server/queues/processors/FileOperationDeletedProcessor.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { FileOperationState, FileOperationType } from "@shared/types";
|
||||
import collectionDestroyer from "@server/commands/collectionDestroyer";
|
||||
import Logger from "@server/logging/Logger";
|
||||
import { Collection, FileOperation, User } from "@server/models";
|
||||
import { sequelize } from "@server/storage/database";
|
||||
import { Event as TEvent, FileOperationEvent } from "@server/types";
|
||||
import BaseProcessor from "./BaseProcessor";
|
||||
|
||||
export default class FileOperationDeletedProcessor extends BaseProcessor {
|
||||
static applicableEvents: TEvent["name"][] = ["fileOperations.delete"];
|
||||
|
||||
async perform(event: FileOperationEvent) {
|
||||
await sequelize.transaction(async (transaction) => {
|
||||
const fileOperation = await FileOperation.findByPk(event.modelId, {
|
||||
rejectOnEmpty: true,
|
||||
paranoid: false,
|
||||
transaction,
|
||||
});
|
||||
if (
|
||||
fileOperation.type === FileOperationType.Export ||
|
||||
fileOperation.state !== FileOperationState.Complete
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const user = await User.findByPk(event.actorId, {
|
||||
rejectOnEmpty: true,
|
||||
paranoid: false,
|
||||
transaction,
|
||||
});
|
||||
|
||||
const collections = await Collection.findAll({
|
||||
transaction,
|
||||
lock: transaction.LOCK.UPDATE,
|
||||
where: {
|
||||
teamId: fileOperation.teamId,
|
||||
importId: fileOperation.id,
|
||||
},
|
||||
});
|
||||
|
||||
for (const collection of collections) {
|
||||
Logger.debug("processor", "Destroying collection created from import", {
|
||||
collectionId: collection.id,
|
||||
});
|
||||
await collectionDestroyer({
|
||||
collection,
|
||||
transaction,
|
||||
user,
|
||||
ip: event.ip,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ describe("DetachDraftsFromCollectionTask", () => {
|
||||
createdById: collection.createdById,
|
||||
teamId: collection.teamId,
|
||||
});
|
||||
await collection.destroy();
|
||||
await collection.destroy({ hooks: false });
|
||||
|
||||
const task = new DetachDraftsFromCollectionTask();
|
||||
await task.perform({
|
||||
|
||||
Reference in New Issue
Block a user