Files
outline/server/models/Group.ts
Apoorv Mishra 7e61a519f1 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>
2024-01-12 22:33:05 +05:30

134 lines
2.7 KiB
TypeScript

import { InferAttributes, InferCreationAttributes, Op } from "sequelize";
import {
AfterDestroy,
BelongsTo,
Column,
ForeignKey,
Table,
HasMany,
BelongsToMany,
DefaultScope,
DataType,
Scopes,
} from "sequelize-typescript";
import GroupPermission from "./GroupPermission";
import GroupUser from "./GroupUser";
import Team from "./Team";
import User from "./User";
import ParanoidModel from "./base/ParanoidModel";
import Fix from "./decorators/Fix";
import Length from "./validators/Length";
import NotContainsUrl from "./validators/NotContainsUrl";
@DefaultScope(() => ({
include: [
{
association: "groupMemberships",
required: false,
},
],
}))
@Scopes(() => ({
withMember: (memberId: string) => ({
include: [
{
association: "groupMemberships",
required: true,
},
{
association: "members",
required: true,
where: {
userId: memberId,
},
},
],
}),
}))
@Table({
tableName: "groups",
modelName: "group",
validate: {
async isUniqueNameInTeam() {
const foundItem = await Group.findOne({
where: {
teamId: this.teamId,
name: {
[Op.iLike]: this.name,
},
id: {
[Op.not]: this.id,
},
},
});
if (foundItem) {
throw new Error("The name of this group is already in use");
}
},
},
})
@Fix
class Group extends ParanoidModel<
InferAttributes<Group>,
Partial<InferCreationAttributes<Group>>
> {
@Length({ min: 0, max: 255, msg: "name must be be 255 characters or less" })
@NotContainsUrl
@Column
name: string;
// hooks
@AfterDestroy
static async deleteGroupUsers(model: Group) {
if (!model.deletedAt) {
return;
}
await GroupUser.destroy({
where: {
groupId: model.id,
},
});
await GroupPermission.destroy({
where: {
groupId: model.id,
},
});
}
static filterByMember(memberId: string | undefined) {
return memberId
? this.scope({ method: ["withMember", memberId] })
: this.scope("defaultScope");
}
// associations
@HasMany(() => GroupUser, "groupId")
@HasMany(() => GroupUser, { as: "members", foreignKey: "groupId" })
groupMemberships: GroupUser[];
@HasMany(() => GroupPermission, "groupId")
collectionGroupMemberships: GroupPermission[];
@BelongsTo(() => Team, "teamId")
team: Team;
@ForeignKey(() => Team)
@Column(DataType.UUID)
teamId: string;
@BelongsTo(() => User, "createdById")
createdBy: User;
@ForeignKey(() => User)
@Column(DataType.UUID)
createdById: string;
@BelongsToMany(() => User, () => GroupUser)
users: User[];
}
export default Group;