50 lines
1.4 KiB
JavaScript
50 lines
1.4 KiB
JavaScript
// models/User.js
|
|
// User model definition
|
|
|
|
class User {
|
|
constructor(id, email, passwordHash, firstName, lastName, userType, tenantId, createdAt, updatedAt) {
|
|
this.id = id;
|
|
this.email = email;
|
|
this.passwordHash = passwordHash;
|
|
this.firstName = firstName;
|
|
this.lastName = lastName;
|
|
this.userType = userType; // 'job-seeker' or 'job-provider'
|
|
this.tenantId = tenantId;
|
|
this.createdAt = createdAt || new Date();
|
|
this.updatedAt = updatedAt || new Date();
|
|
}
|
|
|
|
// Static method to create a new user
|
|
static create(userData) {
|
|
const id = userData.id || require('uuid').v4();
|
|
return new User(
|
|
id,
|
|
userData.email,
|
|
userData.passwordHash,
|
|
userData.firstName,
|
|
userData.lastName,
|
|
userData.userType,
|
|
userData.tenantId
|
|
);
|
|
}
|
|
|
|
// Method to validate a user
|
|
validate() {
|
|
if (!this.email || !this.passwordHash || !this.firstName || !this.lastName || !this.userType || !this.tenantId) {
|
|
throw new Error('Missing required fields');
|
|
}
|
|
|
|
if (!['job-seeker', 'job-provider'].includes(this.userType)) {
|
|
throw new Error('User type must be either job-seeker or job-provider');
|
|
}
|
|
|
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
if (!emailRegex.test(this.email)) {
|
|
throw new Error('Invalid email format');
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
module.exports = User; |