api/resources/SalesCoOrdinators/SalesCoOrdinatorModel.js

89 lines
2.1 KiB
JavaScript

import dotenv from "dotenv";
dotenv.config();
import mongoose from "mongoose";
import validator from "validator";
import bcrypt from "bcryptjs";
import jwt from "jsonwebtoken";
import crypto from "crypto";
const salescoordinatorSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
},
mobileNumber: {
type: String,
required: true,
unique: true,
},
otp: {
type: String,
required: false,
},
otpExpires: {
type: Date,
},
isVerified: {
type: Boolean,
default: false,
},
email: {
type: String,
required: false,
unique: true,
validate: [validator.isEmail, "Please Enter a valid Email"],
},
password: {
type: String,
minLength: [6, "Password should be greater than 6 characters"],
select: false, //find not got passpord
},
newMobileNumber: { type: String },
resetPasswordToken: String,
resetPasswordExpire: Date,
},
{ timestamps: true }
);
salescoordinatorSchema.pre("save", async function (next) {
if (!this.isModified("password")) {
next();
}
this.password = await bcrypt.hash(this.password, 10);
});
// JWT TOKEN
salescoordinatorSchema.methods.getJWTToken = function () {
return jwt.sign({ id: this._id }, process.env.JWT_SECRET);
};
// Compare Password
salescoordinatorSchema.methods.comparePassword = async function (password) {
return await bcrypt.compare(password, this.password);
};
// Generating Reset Token
salescoordinatorSchema.methods.getResetPasswordToken = function () {
// Generating Token
const resetToken = crypto.randomBytes(20).toString("hex");
// Hashing and adding reset PasswordToken to salescoordinatorSchema
this.resetPasswordToken = crypto
.createHash("sha256")
.update(resetToken)
.digest("hex");
//expire password time
// console.log(this.resetPasswordToken)
this.resetPasswordExpire = Date.now() + 15 * 60 * 1000; //15 minut
return resetToken;
};
const SalesCoOrdinator = mongoose.model(
"SalesCoOrdinator",
salescoordinatorSchema
);
export default SalesCoOrdinator;