-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c34221f
commit 26fee65
Showing
1 changed file
with
33 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ const OTP = require("../models/otp.model"); | |
|
||
const nodemailer = require("nodemailer"); | ||
const bcrypt = require("bcrypt"); | ||
const {kSaltRounds} = require('../constants') | ||
|
||
const { | ||
USER_NOT_FOUND_ERR, | ||
|
@@ -22,7 +23,7 @@ let mailTransporter = nodemailer.createTransport({ | |
pass: "mxzc acbf revb xcxh", | ||
}); | ||
|
||
// --------------------- create new user --------------------------------- | ||
// --------------------- create new Vendor --------------------------------- | ||
|
||
exports.createNewVendor = async (req, res, next) => { | ||
try { | ||
|
@@ -42,14 +43,16 @@ exports.createNewVendor = async (req, res, next) => { | |
|
||
console.log(description); | ||
|
||
// let images = [image_url]; | ||
|
||
|
||
const emailExist = await Vendor.findOne({ email }); | ||
if (emailExist) { | ||
next({ status: 400, message: EMAIL_ALREADY_EXISTS_ERR }); | ||
return; | ||
} | ||
|
||
const hashedPassword = await bcrypt.hash(password, kSaltRounds); | ||
|
||
const createVendor = new Vendor({ | ||
ownerName, | ||
email, | ||
|
@@ -66,7 +69,7 @@ exports.createNewVendor = async (req, res, next) => { | |
|
||
const createVendorCredentials = new VendorCredentials({ | ||
email, | ||
password, | ||
password: hashedPassword, | ||
vendor_id: vendor._id, | ||
}); | ||
await createVendorCredentials.save(); | ||
|
@@ -77,32 +80,32 @@ exports.createNewVendor = async (req, res, next) => { | |
}); | ||
await menu.save(); | ||
|
||
const otp = Math.floor(1000 + Math.random() * 9000); | ||
const sentOtp = new OTP({ | ||
code: otp, | ||
expiresAt: new Date(new Date().getTime() + 2 * 60 * 1000), | ||
entity: vendor._id, | ||
entityModel: "Vendor", | ||
}); | ||
await sentOtp.save(); | ||
|
||
let mailDetails = { | ||
from: "[email protected]", | ||
to: email, | ||
subject: "Test mail", | ||
text: `Your OTP is: ${otp}`, | ||
}; | ||
|
||
mailTransporter.sendMail(mailDetails, function (err, data) { | ||
if (err) { | ||
console.log("Error Occurs"); | ||
console.log(err); | ||
} else { | ||
console.log("Email sent successfully"); | ||
} | ||
}); | ||
|
||
res.status(200).json("OTP send successfully"); | ||
// const otp = Math.floor(1000 + Math.random() * 9000); | ||
// const sentOtp = new OTP({ | ||
// code: otp, | ||
// expiresAt: new Date(new Date().getTime() + 2 * 60 * 1000), | ||
// entity: vendor._id, | ||
// entityModel: "Vendor", | ||
// }); | ||
// await sentOtp.save(); | ||
|
||
// let mailDetails = { | ||
// from: "[email protected]", | ||
// to: email, | ||
// subject: "Test mail", | ||
// text: `Your OTP is: ${otp}`, | ||
// };c | ||
|
||
// mailTransporter.sendMail(mailDetails, function (err, data) { | ||
// if (err) { | ||
// console.log("Error Occurs"); | ||
// console.log(err); | ||
// } else { | ||
// console.log("Email sent successfully"); | ||
// } | ||
// }); | ||
|
||
res.status(200).json("Register successfully"); | ||
} catch (error) { | ||
next(error); | ||
} | ||
|
@@ -120,7 +123,7 @@ exports.vendorLogin = async (req, res, next) => { | |
return; | ||
} | ||
|
||
const passwordMatch = vendor.password === password; | ||
const passwordMatch = await bcrypt.compare(password, vendor.password); | ||
if (passwordMatch) { | ||
// Generate JWT token | ||
const token = createJwtToken({ userId: vendor.vendor_id }); | ||
|