forked from Chatbot/v3-api
38 lines
970 B
TypeScript
38 lines
970 B
TypeScript
import * as crypto from 'node:crypto';
|
|
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
|
|
import { ChatModel } from './chat.model';
|
|
import { UsersBaseModel } from './users-base.model';
|
|
|
|
@Schema({
|
|
timestamps: { createdAt: 'createdAt', updatedAt: 'updatedAt' },
|
|
versionKey: false,
|
|
collection: 'user',
|
|
_id: true,
|
|
})
|
|
export class UserModel extends UsersBaseModel {
|
|
@Prop({ default: [] })
|
|
chat: ChatModel[];
|
|
|
|
@Prop({
|
|
required: false,
|
|
select: true,
|
|
set: (otp: string) =>
|
|
otp ? crypto.createHash('sha256').update(otp).digest('hex') : null,
|
|
expires: 120, // The OTP will automatically be removed after 120 seconds
|
|
})
|
|
otp: string | null;
|
|
|
|
@Prop({ type: Date, default: null })
|
|
otpCreatedAt: Date | null;
|
|
|
|
@Prop({ type: Number, default: 0 })
|
|
otpAttempts: number;
|
|
}
|
|
|
|
export const UserSchema = SchemaFactory.createForClass(UserModel);
|
|
|
|
UserSchema.pre('save', function (next) {
|
|
this.updatedAt = new Date();
|
|
next();
|
|
});
|