Prashikshak / API /src /model /user.model.ts
Abhisingh-18's picture
Initial commit: Prashikshak - disaster management training platform
9a92a42
Raw
History Blame Contribute Delete
4.84 kB
import { Document, Schema, Model, model, Types } from 'mongoose';
interface IGeoLocation {
type: "Point";
coordinates: [number, number]; // [longitude, latitude]
}
interface IOrganizationDocs {
registrationCertificate?: string;
gstCertificate?: string;
authorizationLetter?: string;
additionalDocs?: string[];
}
// Interface representing a User document in MongoDB
export interface IUser extends Document {
username: string;
email: string;
password: string;
// If user uses password to signup -> they can use both OAuth as well as password to login but if they used OAuth to signup they can't use password to login they must use OAuth only to login
usedOAuth?: boolean; //Added this instead, it makes more sense so that if user tries to login with password, I can tell them that no you used OAuth go with that only
profilePhoto?: string;
lastActive?: Date;
isActive: boolean;
// 2FA
twoFactorSecret?: string;
isTwoFactorEnabled?: boolean;
// for GIS mapping
location?: IGeoLocation;
role: "trainer" | "trainee" | "organization" | "admin";
// ---------- ORGANIZATION SPECIFIC ----------
organizationType?: "NDMA" | "SDMA" | "ATI" | "NGO" | "OTHER";
documents?: IOrganizationDocs;
verificationStatus?: "pending" | "approved" | "rejected";
verifiedBy?: Types.ObjectId;
documentsUploaded?: boolean; // Track if verification documents have been uploaded
// ---------- TRAINER SPECIFIC ----------
organization?: Types.ObjectId; // belongs to which org
organizationVerificationStatus?: "pending" | "verified" | "rejected";
govtIdCard?: string;
workDesignation?: string;
rejectionReason?: string;
// ---------- TRAINEE SPECIFIC ----------
traineeCategory?: "community_volunteer" | "govt_officer" | "responder" | "student" | "other";
// Admin permissions
permissions?: string[];
resetPasswordToken?: string;
resetPasswordExpires?: Date | number;
createdAt: Date;
updatedAt: Date;
}
const UserSchema = new Schema<IUser>({
username: {
type: String,
required: true,
unique: true,
},
email: {
type: String,
required: true,
unique: true,
lowercase: true,
},
password: {
type: String,
required: false, // oauth me password nahi hoga
},
resetPasswordToken: {
type: String,
default: null
},
resetPasswordExpires: {
type: Date, // <— MUST be Date, not number
default: null
},
usedOAuth: {
type: Boolean,
required: false // old users might not have this field
},
profilePhoto: {
type: String,
default: 'https://www.gravatar.com/avatar/?d=mp',
},
lastActive: {
type: Date,
default: Date.now,
},
isActive: {
type: Boolean,
default: true,
},
twoFactorSecret: {
type: String,
},
isTwoFactorEnabled: {
type: Boolean,
default: false,
},
role: {
type: String,
enum: ["trainer", "trainee", "organization", "admin"],
required: true
},
documents: {
registrationCertificate: String,
gstCertificate: String,
authorizationLetter: String,
additionalDocs: [String]
},
verificationStatus: {
type: String,
enum: ["pending", "approved", "rejected"],
default: "pending"
},
verifiedBy: {
type: Schema.Types.ObjectId,
ref: "User"
},
documentsUploaded: {
type: Boolean,
default: false
},
organizationVerificationStatus: {
type: String,
enum: ["pending", "verified", "rejected"],
default: "pending"
},
govtIdCard: String,
workDesignation: String,
rejectionReason: String,
// -----------------------------
// Geo-location (GIS mapping)
// -----------------------------
location: {
type: {
type: String,
enum: ["Point"],
default: "Point"
},
coordinates: {
type: [Number],
default: [0, 0] // long, lat
}
},
// ------------------------------------
// ADMIN FIELDS
// ------------------------------------
permissions: {
type: [String],
default: []
},
// ------------------------------------
// TRAINER FIELDS
// ------------------------------------
organization: {
type: Schema.Types.ObjectId,
ref: "User"
},
// ------------------------------------
// TRAINEE FIELDS
// ------------------------------------
traineeCategory: {
type: String,
enum: [
"community_volunteer",
"govt_officer",
"responder",
"student",
"other"
],
required: false
}
}, { timestamps: true });
// Index for GIS
UserSchema.index({ location: "2dsphere" });
// Trainer → org lookup
UserSchema.index({ organization: 1 });
// Org verification search
UserSchema.index({ verificationStatus: 1 });
// Roles filtering
UserSchema.index({ role: 1 });
const User: Model<IUser> = model<IUser>('User', UserSchema);
export default User;