2022-01-06 01:26:15 +00:00
|
|
|
import {
|
|
|
|
Table,
|
|
|
|
Column,
|
|
|
|
CreatedAt,
|
|
|
|
UpdatedAt,
|
|
|
|
Model,
|
|
|
|
DataType,
|
|
|
|
BeforeCreate,
|
|
|
|
BeforeUpdate,
|
|
|
|
PrimaryKey,
|
|
|
|
AutoIncrement,
|
|
|
|
Default,
|
|
|
|
HasMany,
|
2022-05-03 21:20:58 +00:00
|
|
|
BelongsToMany,
|
2022-01-06 01:26:15 +00:00
|
|
|
} from "sequelize-typescript";
|
|
|
|
import { hash, compare } from "bcryptjs";
|
|
|
|
import Ticket from "./Ticket";
|
|
|
|
import Queue from "./Queue";
|
|
|
|
import UserQueue from "./UserQueue";
|
2022-08-29 01:09:00 +00:00
|
|
|
import UserOnlineTime from "./UserOnlineTime";
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
@Table
|
|
|
|
class User extends Model<User> {
|
|
|
|
@PrimaryKey
|
|
|
|
@AutoIncrement
|
|
|
|
@Column
|
|
|
|
id: number;
|
|
|
|
|
|
|
|
@Column
|
|
|
|
name: string;
|
|
|
|
|
|
|
|
@Column
|
|
|
|
email: string;
|
|
|
|
|
|
|
|
@Column(DataType.VIRTUAL)
|
|
|
|
password: string;
|
|
|
|
|
|
|
|
@Column
|
|
|
|
passwordHash: string;
|
|
|
|
|
|
|
|
@Default(0)
|
|
|
|
@Column
|
|
|
|
tokenVersion: number;
|
|
|
|
|
|
|
|
@Default("admin")
|
|
|
|
@Column
|
|
|
|
profile: string;
|
|
|
|
|
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date;
|
|
|
|
|
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date;
|
|
|
|
|
|
|
|
@HasMany(() => Ticket)
|
|
|
|
tickets: Ticket[];
|
2022-08-29 01:09:00 +00:00
|
|
|
|
2022-05-03 21:20:58 +00:00
|
|
|
@HasMany(() => UserOnlineTime)
|
2022-08-29 01:09:00 +00:00
|
|
|
UserOnlineTime: UserOnlineTime[];
|
2022-05-03 21:20:58 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
@BelongsToMany(() => Queue, () => UserQueue)
|
|
|
|
queues: Queue[];
|
|
|
|
|
|
|
|
@BeforeUpdate
|
|
|
|
@BeforeCreate
|
|
|
|
static hashPassword = async (instance: User): Promise<void> => {
|
|
|
|
if (instance.password) {
|
|
|
|
instance.passwordHash = await hash(instance.password, 8);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public checkPassword = async (password: string): Promise<boolean> => {
|
|
|
|
return compare(password, this.getDataValue("passwordHash"));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default User;
|