forked from Shared/esg
initial commit
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
|
||||
import { HydratedDocument, Types } from 'mongoose';
|
||||
import { ClientType } from '../../common/enums/client-type.enum';
|
||||
import { Role } from '../../common/enums/role.enum';
|
||||
|
||||
export type UserDocument = HydratedDocument<User>;
|
||||
|
||||
/**
|
||||
* Unified user model — internal admins and external API clients share one collection.
|
||||
* Designed for future extensions: API keys, HMAC, IP whitelist, permissions, multi-tenant.
|
||||
*/
|
||||
@Schema({ timestamps: true, collection: 'users' })
|
||||
export class User {
|
||||
@Prop({ required: true, trim: true })
|
||||
fullName!: string;
|
||||
|
||||
@Prop({ required: true, unique: true, lowercase: true, trim: true, index: true })
|
||||
username!: string;
|
||||
|
||||
@Prop({ required: true, unique: true, lowercase: true, trim: true, index: true })
|
||||
email!: string;
|
||||
|
||||
@Prop({ trim: true })
|
||||
mobile?: string;
|
||||
|
||||
@Prop({ required: true, select: false })
|
||||
password!: string;
|
||||
|
||||
@Prop({ required: true, enum: Role, default: Role.USER, index: true })
|
||||
role!: Role;
|
||||
|
||||
@Prop({ required: true, enum: ClientType, default: ClientType.EXTERNAL, index: true })
|
||||
clientType!: ClientType;
|
||||
|
||||
@Prop({ trim: true, default: '' })
|
||||
appName!: string;
|
||||
|
||||
@Prop({ trim: true, default: '' })
|
||||
clientName!: string;
|
||||
|
||||
@Prop({ default: true })
|
||||
isActive!: boolean;
|
||||
|
||||
@Prop({ default: false })
|
||||
isBlocked!: boolean;
|
||||
|
||||
/** Future: machine-to-machine auth without JWT. */
|
||||
@Prop({ select: false })
|
||||
apiKey?: string;
|
||||
|
||||
@Prop({ type: [String], default: [] })
|
||||
allowedInquiries!: string[];
|
||||
|
||||
@Prop({ default: 60 })
|
||||
requestLimitPerMinute!: number;
|
||||
|
||||
@Prop({ default: 10000 })
|
||||
requestLimitPerDay!: number;
|
||||
|
||||
@Prop({ default: 0 })
|
||||
totalRequests!: number;
|
||||
|
||||
@Prop()
|
||||
lastLoginAt?: Date;
|
||||
|
||||
@Prop()
|
||||
passwordChangedAt?: Date;
|
||||
|
||||
@Prop({ type: Types.ObjectId, ref: 'User' })
|
||||
createdBy?: Types.ObjectId;
|
||||
|
||||
@Prop({ select: false })
|
||||
refreshToken?: string;
|
||||
|
||||
@Prop({ type: Object, default: {} })
|
||||
metadata!: Record<string, unknown>;
|
||||
|
||||
createdAt!: Date;
|
||||
updatedAt!: Date;
|
||||
}
|
||||
|
||||
export const UserSchema = SchemaFactory.createForClass(User);
|
||||
|
||||
UserSchema.index({ role: 1, isActive: 1 });
|
||||
UserSchema.index({ clientType: 1, appName: 1 });
|
||||
Reference in New Issue
Block a user