Type server models (#6326)
* fix: type server models * fix: make ParanoidModel generic * fix: ApiKey * fix: Attachment * fix: AuthenticationProvider * fix: Backlink * fix: Collection * fix: Comment * fix: Document * fix: FileOperation * fix: Group * fix: GroupPermission * fix: GroupUser * fix: Integration * fix: IntegrationAuthentication * fix: Notification * fix: Pin * fix: Revision * fix: SearchQuery * fix: Share * fix: Star * fix: Subscription * fix: TypeError * fix: Imports * fix: Team * fix: TeamDomain * fix: User * fix: UserAuthentication * fix: UserPermission * fix: View * fix: WebhookDelivery * fix: WebhookSubscription * Remove type duplication --------- Co-authored-by: Tom Moor <tom.moor@gmail.com>
This commit is contained in:
@@ -8,6 +8,9 @@ import {
|
||||
SaveOptions,
|
||||
Op,
|
||||
FindOptions,
|
||||
InferAttributes,
|
||||
InferCreationAttributes,
|
||||
InstanceUpdateOptions,
|
||||
} from "sequelize";
|
||||
import {
|
||||
Table,
|
||||
@@ -40,6 +43,7 @@ import {
|
||||
} from "@shared/types";
|
||||
import { stringToColor } from "@shared/utils/color";
|
||||
import env from "@server/env";
|
||||
import Model from "@server/models/base/Model";
|
||||
import DeleteAttachmentTask from "@server/queues/tasks/DeleteAttachmentTask";
|
||||
import parseAttachmentIds from "@server/utils/parseAttachmentIds";
|
||||
import { ValidationError } from "../errors";
|
||||
@@ -117,7 +121,10 @@ export enum UserFlag {
|
||||
}))
|
||||
@Table({ tableName: "users", modelName: "user" })
|
||||
@Fix
|
||||
class User extends ParanoidModel {
|
||||
class User extends ParanoidModel<
|
||||
InferAttributes<User>,
|
||||
Partial<InferCreationAttributes<User>>
|
||||
> {
|
||||
@IsEmail
|
||||
@Length({ max: 255, msg: "User email must be 255 characters or less" })
|
||||
@Column
|
||||
@@ -519,7 +526,10 @@ class User extends ParanoidModel {
|
||||
],
|
||||
});
|
||||
|
||||
demote = async (to: UserRole, options?: SaveOptions<User>) => {
|
||||
demote: (
|
||||
to: UserRole,
|
||||
options?: InstanceUpdateOptions<InferAttributes<Model>>
|
||||
) => Promise<void> = async (to, options) => {
|
||||
const res = await (this.constructor as typeof User).findAndCountAll({
|
||||
where: {
|
||||
teamId: this.teamId,
|
||||
@@ -568,7 +578,9 @@ class User extends ParanoidModel {
|
||||
}
|
||||
};
|
||||
|
||||
promote = (options?: SaveOptions<User>) =>
|
||||
promote: (
|
||||
options?: InstanceUpdateOptions<InferAttributes<User>>
|
||||
) => Promise<User> = (options) =>
|
||||
this.update(
|
||||
{
|
||||
isAdmin: true,
|
||||
@@ -605,14 +617,9 @@ class User extends ParanoidModel {
|
||||
|
||||
@AfterUpdate
|
||||
static deletePreviousAvatar = async (model: User) => {
|
||||
if (
|
||||
model.previous("avatarUrl") &&
|
||||
model.previous("avatarUrl") !== model.avatarUrl
|
||||
) {
|
||||
const attachmentIds = parseAttachmentIds(
|
||||
model.previous("avatarUrl"),
|
||||
true
|
||||
);
|
||||
const previousAvatarUrl = model.previous("avatarUrl");
|
||||
if (previousAvatarUrl && previousAvatarUrl !== model.avatarUrl) {
|
||||
const attachmentIds = parseAttachmentIds(previousAvatarUrl, true);
|
||||
if (!attachmentIds.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user