File size: 4,841 Bytes
9a92a42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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;