forked from Shared/esg
database migrated to POSTGRESQL
This commit is contained in:
@@ -1,85 +1,89 @@
|
||||
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
|
||||
import { HydratedDocument, Types } from 'mongoose';
|
||||
import { Column, CreateDateColumn, Entity, Index, PrimaryGeneratedColumn, UpdateDateColumn } from 'typeorm';
|
||||
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.
|
||||
* Unified user model — internal admins and external API clients share one table.
|
||||
* Designed for future extensions: API keys, HMAC, IP whitelist, permissions, multi-tenant.
|
||||
*/
|
||||
@Schema({ timestamps: true, collection: 'users' })
|
||||
@Entity('users')
|
||||
@Index(['role', 'isActive'])
|
||||
@Index(['clientType', 'appName'])
|
||||
export class User {
|
||||
@Prop({ required: true, trim: true })
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id!: string;
|
||||
|
||||
@Column({ type: 'text' })
|
||||
fullName!: string;
|
||||
|
||||
@Prop({ required: true, unique: true, lowercase: true, trim: true, index: true })
|
||||
@Index({ unique: true })
|
||||
@Column({ type: 'text' })
|
||||
username!: string;
|
||||
|
||||
@Prop({ required: true, unique: true, lowercase: true, trim: true, index: true })
|
||||
@Index({ unique: true })
|
||||
@Column({ type: 'text' })
|
||||
email!: string;
|
||||
|
||||
@Prop({ trim: true })
|
||||
mobile?: string;
|
||||
@Column({ type: 'text', nullable: true })
|
||||
mobile?: string | null;
|
||||
|
||||
@Prop({ required: true, select: false })
|
||||
@Column({ type: 'text', select: false })
|
||||
password!: string;
|
||||
|
||||
@Prop({ required: true, enum: Role, default: Role.USER, index: true })
|
||||
@Index()
|
||||
@Column({ type: 'text' })
|
||||
role!: Role;
|
||||
|
||||
@Prop({ required: true, enum: ClientType, default: ClientType.EXTERNAL, index: true })
|
||||
@Index()
|
||||
@Column({ type: 'text' })
|
||||
clientType!: ClientType;
|
||||
|
||||
@Prop({ trim: true, default: '' })
|
||||
@Column({ type: 'text', default: '' })
|
||||
appName!: string;
|
||||
|
||||
@Prop({ trim: true, default: '' })
|
||||
@Column({ type: 'text', default: '' })
|
||||
clientName!: string;
|
||||
|
||||
@Prop({ default: true })
|
||||
@Column({ type: 'boolean', default: true })
|
||||
isActive!: boolean;
|
||||
|
||||
@Prop({ default: false })
|
||||
@Column({ type: 'boolean', default: false })
|
||||
isBlocked!: boolean;
|
||||
|
||||
/** Future: machine-to-machine auth without JWT. */
|
||||
@Prop({ select: false })
|
||||
apiKey?: string;
|
||||
@Column({ type: 'text', nullable: true, select: false })
|
||||
apiKey?: string | null;
|
||||
|
||||
@Prop({ type: [String], default: [] })
|
||||
@Column({ type: 'text', array: true, default: () => "'{}'" })
|
||||
allowedInquiries!: string[];
|
||||
|
||||
@Prop({ default: 60 })
|
||||
@Column({ type: 'int', default: 60 })
|
||||
requestLimitPerMinute!: number;
|
||||
|
||||
@Prop({ default: 10000 })
|
||||
@Column({ type: 'int', default: 10000 })
|
||||
requestLimitPerDay!: number;
|
||||
|
||||
@Prop({ default: 0 })
|
||||
@Column({ type: 'int', default: 0 })
|
||||
totalRequests!: number;
|
||||
|
||||
@Prop()
|
||||
lastLoginAt?: Date;
|
||||
@Column({ type: 'timestamptz', nullable: true })
|
||||
lastLoginAt?: Date | null;
|
||||
|
||||
@Prop()
|
||||
passwordChangedAt?: Date;
|
||||
@Column({ type: 'timestamptz', nullable: true })
|
||||
passwordChangedAt?: Date | null;
|
||||
|
||||
@Prop({ type: Types.ObjectId, ref: 'User' })
|
||||
createdBy?: Types.ObjectId;
|
||||
@Column({ type: 'uuid', nullable: true })
|
||||
createdBy?: string | null;
|
||||
|
||||
@Prop({ select: false })
|
||||
refreshToken?: string;
|
||||
@Column({ type: 'text', nullable: true, select: false })
|
||||
refreshToken?: string | null;
|
||||
|
||||
@Prop({ type: Object, default: {} })
|
||||
@Column({ type: 'jsonb', default: () => "'{}'::jsonb" })
|
||||
metadata!: Record<string, unknown>;
|
||||
|
||||
@CreateDateColumn({ type: 'timestamptz' })
|
||||
createdAt!: Date;
|
||||
|
||||
@UpdateDateColumn({ type: 'timestamptz' })
|
||||
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