-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
front/lib/resources/storage/models/group_memberships.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import type { | ||
CreationOptional, | ||
ForeignKey, | ||
InferAttributes, | ||
InferCreationAttributes, | ||
} from "sequelize"; | ||
import { DataTypes, Model, Op } from "sequelize"; | ||
|
||
import { User } from "@app/lib/models/user"; | ||
import { Workspace } from "@app/lib/models/workspace"; | ||
import { frontSequelize } from "@app/lib/resources/storage"; | ||
import { GroupModel } from "@app/lib/resources/storage/models/groups"; | ||
|
||
export class GroupMembershipModel extends Model< | ||
InferAttributes<GroupMembershipModel>, | ||
InferCreationAttributes<GroupMembershipModel> | ||
> { | ||
declare id: CreationOptional<number>; | ||
declare createdAt: CreationOptional<Date>; | ||
declare updatedAt: CreationOptional<Date>; | ||
|
||
declare startAt: Date; | ||
declare endAt: Date | null; | ||
|
||
declare groupId: ForeignKey<GroupModel["id"]>; | ||
declare userId: ForeignKey<User["id"]>; | ||
declare workspaceId: ForeignKey<Workspace["id"]>; | ||
|
||
static async checkOverlap( | ||
userId: number, | ||
groupId: number, | ||
startAt: Date, | ||
endAt: Date | null, | ||
excludeId?: number | ||
) { | ||
const where: any = { | ||
userId, | ||
groupId, | ||
[Op.or]: [{ endAt: null }, { endAt: { [Op.gt]: startAt } }], | ||
startAt: { [Op.lt]: endAt || new Date(8640000000000000) }, // Max date if endAt is null | ||
}; | ||
|
||
if (excludeId) { | ||
where.id = { [Op.ne]: excludeId }; | ||
} | ||
|
||
const overlapping = await this.findOne({ where }); | ||
return !!overlapping; | ||
} | ||
} | ||
GroupMembershipModel.init( | ||
{ | ||
id: { | ||
type: DataTypes.INTEGER, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
}, | ||
createdAt: { | ||
type: DataTypes.DATE, | ||
allowNull: false, | ||
defaultValue: DataTypes.NOW, | ||
}, | ||
updatedAt: { | ||
type: DataTypes.DATE, | ||
allowNull: false, | ||
defaultValue: DataTypes.NOW, | ||
}, | ||
startAt: { | ||
type: DataTypes.DATE, | ||
allowNull: false, | ||
}, | ||
endAt: { | ||
type: DataTypes.DATE, | ||
allowNull: true, | ||
}, | ||
}, | ||
{ | ||
modelName: "group_memberships", | ||
sequelize: frontSequelize, | ||
indexes: [{ fields: ["userId", "groupId"] }], | ||
validate: { | ||
async noOverlap() { | ||
if ( | ||
await GroupMembershipModel.checkOverlap( | ||
this.userId as number, | ||
this.groupId as number, | ||
this.startAt as Date, | ||
this.endAt as Date | null, | ||
this.id as number | ||
) | ||
) { | ||
throw new Error("Overlapping group membership period"); | ||
} | ||
}, | ||
}, | ||
} | ||
); | ||
User.hasMany(GroupMembershipModel, { | ||
foreignKey: { allowNull: false }, | ||
onDelete: "CASCADE", | ||
}); | ||
GroupModel.hasMany(GroupMembershipModel, { | ||
foreignKey: { allowNull: false }, | ||
onDelete: "CASCADE", | ||
}); | ||
Workspace.hasMany(GroupMembershipModel, { | ||
foreignKey: { allowNull: false }, | ||
onDelete: "CASCADE", | ||
}); | ||
GroupMembershipModel.belongsTo(User); | ||
GroupMembershipModel.belongsTo(GroupModel); | ||
GroupMembershipModel.belongsTo(Workspace); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
-- Migration created on Jul 25, 2024 | ||
CREATE TABLE IF NOT EXISTS "group_memberships" ( | ||
"id" SERIAL , | ||
"createdAt" TIMESTAMP WITH TIME ZONE NOT NULL, | ||
"updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL, | ||
"startAt" TIMESTAMP WITH TIME ZONE NOT NULL, | ||
"endAt" TIMESTAMP WITH TIME ZONE, | ||
"userId" INTEGER NOT NULL REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE, | ||
"groupId" INTEGER NOT NULL REFERENCES "groups" ("id") ON DELETE CASCADE ON UPDATE CASCADE, | ||
"workspaceId" INTEGER NOT NULL REFERENCES "workspaces" ("id") ON DELETE CASCADE ON UPDATE CASCADE, | ||
PRIMARY KEY ("id") | ||
); | ||
CREATE INDEX "group_memberships_user_id_group_id" ON "group_memberships" ("userId", "groupId"); |