feat: Allow deletion of imports (#5907)
This commit is contained in:
@@ -29,6 +29,7 @@ import {
|
||||
Scopes,
|
||||
DataType,
|
||||
Length as SimpleLength,
|
||||
BeforeDestroy,
|
||||
} from "sequelize-typescript";
|
||||
import isUUID from "validator/lib/isUUID";
|
||||
import type { CollectionSort } from "@shared/types";
|
||||
@@ -37,6 +38,7 @@ import { sortNavigationNodes } from "@shared/utils/collections";
|
||||
import slugify from "@shared/utils/slugify";
|
||||
import { SLUG_URL_REGEX } from "@shared/utils/urlHelpers";
|
||||
import { CollectionValidation } from "@shared/validations";
|
||||
import { ValidationError } from "@server/errors";
|
||||
import Document from "./Document";
|
||||
import FileOperation from "./FileOperation";
|
||||
import Group from "./Group";
|
||||
@@ -265,6 +267,18 @@ class Collection extends ParanoidModel {
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeDestroy
|
||||
static async checkLastCollection(model: Collection) {
|
||||
const total = await this.count({
|
||||
where: {
|
||||
teamId: model.teamId,
|
||||
},
|
||||
});
|
||||
if (total === 1) {
|
||||
throw ValidationError("Cannot delete last collection");
|
||||
}
|
||||
}
|
||||
|
||||
@AfterDestroy
|
||||
static async onAfterDestroy(model: Collection) {
|
||||
await Document.destroy({
|
||||
|
||||
@@ -459,16 +459,18 @@ class Document extends ParanoidModel {
|
||||
return null;
|
||||
}
|
||||
|
||||
const { includeState, userId, ...rest } = options;
|
||||
|
||||
// allow default preloading of collection membership if `userId` is passed in find options
|
||||
// almost every endpoint needs the collection membership to determine policy permissions.
|
||||
const scope = this.scope([
|
||||
...(options.includeState ? [] : ["withoutState"]),
|
||||
...(includeState ? [] : ["withoutState"]),
|
||||
"withDrafts",
|
||||
{
|
||||
method: ["withCollectionPermissions", options.userId, options.paranoid],
|
||||
method: ["withCollectionPermissions", userId, rest.paranoid],
|
||||
},
|
||||
{
|
||||
method: ["withViews", options.userId],
|
||||
method: ["withViews", userId],
|
||||
},
|
||||
]);
|
||||
|
||||
@@ -477,7 +479,7 @@ class Document extends ParanoidModel {
|
||||
where: {
|
||||
id,
|
||||
},
|
||||
...options,
|
||||
...rest,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -487,7 +489,7 @@ class Document extends ParanoidModel {
|
||||
where: {
|
||||
urlId: match[1],
|
||||
},
|
||||
...options,
|
||||
...rest,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ import FileStorage from "@server/storage/files";
|
||||
import Collection from "./Collection";
|
||||
import Team from "./Team";
|
||||
import User from "./User";
|
||||
import IdModel from "./base/IdModel";
|
||||
import ParanoidModel from "./base/ParanoidModel";
|
||||
import Fix from "./decorators/Fix";
|
||||
|
||||
@DefaultScope(() => ({
|
||||
@@ -36,7 +36,7 @@ import Fix from "./decorators/Fix";
|
||||
}))
|
||||
@Table({ tableName: "file_operations", modelName: "file_operation" })
|
||||
@Fix
|
||||
class FileOperation extends IdModel {
|
||||
class FileOperation extends ParanoidModel {
|
||||
@Column(DataType.ENUM(...Object.values(FileOperationType)))
|
||||
type: FileOperationType;
|
||||
|
||||
@@ -73,7 +73,7 @@ class FileOperation extends IdModel {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
await this.save();
|
||||
return this.save();
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user