unoffical wafrn mirror
wafrn.net
atproto
social-network
activitypub
1import {
2 Model, Table, Column, DataType, ForeignKey, BelongsTo
3} from "sequelize-typescript";
4import { generateRandomStringInviteCode } from "../utils/generateRandomString.js";
5import { User } from "./index.js";
6
7export interface InviteCodeAttributes {
8 code?: string
9 createdAt?: Date
10 updatedAt?: Date
11 createdByUserId: string
12 expirationDate: Date
13 usedByUserId?: string | null
14}
15
16@Table({
17 tableName: "inviteCodes",
18 modelName: "inviteCodes",
19 timestamps: true
20})
21export class InviteCode extends Model<InviteCodeAttributes, InviteCodeAttributes> implements InviteCodeAttributes {
22 @Column({
23 primaryKey: true,
24 type: DataType.CHAR,
25 defaultValue: () => generateRandomStringInviteCode()
26 })
27 declare code: string;
28
29 @ForeignKey(() => User)
30 @Column({
31 type: DataType.UUID
32 })
33 declare createdByUserId: string
34
35 @Column({
36 type: DataType.DATE,
37 defaultValue: () => {
38 const date = new Date()
39 date.setDate(date.getDate() + 7)
40 return date
41 }
42 })
43 declare expirationDate: Date;
44
45 @ForeignKey(() => User)
46 @Column({
47 allowNull: true,
48 type: DataType.UUID
49 })
50 declare usedByUserId: string | null
51
52 @BelongsTo(() => User, 'createdByUserId')
53 declare createdBy: User
54
55 @BelongsTo(() => User, 'usedByUserId')
56 declare usedBy: User
57
58 get isUsedOrExpired() {
59 return this.usedByUserId || new Date() > this.expirationDate
60 }
61}