Skip to content

Commit

Permalink
fix: vendorController
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharbansal22 committed Apr 8, 2024
1 parent c34221f commit 26fee65
Showing 1 changed file with 33 additions and 30 deletions.
63 changes: 33 additions & 30 deletions controllers/vendorAuth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -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();
Expand All @@ -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);
}
Expand All @@ -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 });
Expand Down

0 comments on commit 26fee65

Please sign in to comment.