correct the address of user and add unique id for salescoordinator and principal distributor
This commit is contained in:
parent
0d0a2978c0
commit
b4c88496fc
2
app.js
2
app.js
@ -171,7 +171,7 @@ import CouponRoute from "./resources/Affiliate&Coupon/Coupon/CouponRoute.js";
|
|||||||
//support Ticket
|
//support Ticket
|
||||||
// attandance
|
// attandance
|
||||||
import attendance from "./resources/Attendance/AttandanceRoute.js"
|
import attendance from "./resources/Attendance/AttandanceRoute.js"
|
||||||
app.use("/api/v1/", user);
|
app.use("/api/v1", user);
|
||||||
|
|
||||||
//Product
|
//Product
|
||||||
app.use("/api", ProductRouter);
|
app.use("/api", ProductRouter);
|
||||||
|
@ -3,7 +3,7 @@ const { PAYPAL_CLIENT_ID, PAYPAL_CLIENT_SECRET } = process.env;
|
|||||||
const base = "https://api-m.sandbox.paypal.com";
|
const base = "https://api-m.sandbox.paypal.com";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
import { shippingAddress } from "../ShippingAddresses/ShippingAddressModel.js";
|
import ShippingAddress from "../ShippingAddresses/ShippingAddressModel.js";
|
||||||
//paypal client id get
|
//paypal client id get
|
||||||
export const getClientId = async (req, res) => {
|
export const getClientId = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
@ -108,7 +108,7 @@ export const createOrderCheckout = async (req, res) => {
|
|||||||
price_With_Tax: 0,
|
price_With_Tax: 0,
|
||||||
taxId: "",
|
taxId: "",
|
||||||
}));
|
}));
|
||||||
let addss = await shippingAddress.findById(address);
|
let addss = await ShippingAddress.findById(address);
|
||||||
let shipping = {
|
let shipping = {
|
||||||
first_Name: addss.first_Name,
|
first_Name: addss.first_Name,
|
||||||
last_Name: addss.last_Name,
|
last_Name: addss.last_Name,
|
||||||
@ -116,6 +116,7 @@ export const createOrderCheckout = async (req, res) => {
|
|||||||
street: addss.street,
|
street: addss.street,
|
||||||
city: addss.city,
|
city: addss.city,
|
||||||
state: addss.state,
|
state: addss.state,
|
||||||
|
panpanNumber: addss.panNumber,
|
||||||
postalCode: addss?.postalCode,
|
postalCode: addss?.postalCode,
|
||||||
country: addss.country,
|
country: addss.country,
|
||||||
addressId: address,
|
addressId: address,
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { shippingAddress } from "../ShippingAddresses/ShippingAddressModel.js";
|
import ShippingAddress from "../ShippingAddresses/ShippingAddressModel.js";
|
||||||
import { Order } from "./orderModel.js";
|
import { Order } from "./orderModel.js";
|
||||||
|
|
||||||
import sendEmail from "../../Utils/sendEmail.js";
|
import sendEmail from "../../Utils/sendEmail.js";
|
||||||
@ -40,7 +40,7 @@ export const poscreateOrderCheckout = async (req, res) => {
|
|||||||
if (!userr) return res.status(400).json({ message: "User is not defined" });
|
if (!userr) return res.status(400).json({ message: "User is not defined" });
|
||||||
|
|
||||||
// Retrieve shipping address from database
|
// Retrieve shipping address from database
|
||||||
let addss = await shippingAddress.findById(address);
|
let addss = await ShippingAddress.findById(address);
|
||||||
|
|
||||||
let shipping = {
|
let shipping = {
|
||||||
first_Name: addss.first_Name,
|
first_Name: addss.first_Name,
|
||||||
|
@ -3,7 +3,7 @@ import crypto from "crypto";
|
|||||||
import Razorpay from "razorpay";
|
import Razorpay from "razorpay";
|
||||||
import { Order } from "./orderModel.js";
|
import { Order } from "./orderModel.js";
|
||||||
|
|
||||||
import { shippingAddress } from "../ShippingAddresses/ShippingAddressModel.js";
|
import ShippingAddress from "../ShippingAddresses/ShippingAddressModel.js";
|
||||||
import sendEmail from "../../Utils/sendEmail.js";
|
import sendEmail from "../../Utils/sendEmail.js";
|
||||||
import { AffiliateModel } from "../Affiliate&Coupon/Affiliate/AffiliateModel.js";
|
import { AffiliateModel } from "../Affiliate&Coupon/Affiliate/AffiliateModel.js";
|
||||||
const instance = new Razorpay({
|
const instance = new Razorpay({
|
||||||
@ -177,7 +177,7 @@ export const checkout = async (req, res) => {
|
|||||||
// const { email } = req.user;
|
// const { email } = req.user;
|
||||||
// if (!email)
|
// if (!email)
|
||||||
// return res.status(400).send({ message: "Please enter the email" });
|
// return res.status(400).send({ message: "Please enter the email" });
|
||||||
let addss = await shippingAddress.findById(address);
|
let addss = await ShippingAddress.findById(address);
|
||||||
|
|
||||||
let shipping = {
|
let shipping = {
|
||||||
first_Name: addss.first_Name,
|
first_Name: addss.first_Name,
|
||||||
@ -659,7 +659,7 @@ export const handlePayment = async (req, res) => {
|
|||||||
return res.status(404).json({ msg: "please provide product subtotal" });
|
return res.status(404).json({ msg: "please provide product subtotal" });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let addss = await shippingAddress.findById(address);
|
let addss = await ShippingAddress.findById(address);
|
||||||
// console.log(addss?.postalCode);
|
// console.log(addss?.postalCode);
|
||||||
let shipping = {
|
let shipping = {
|
||||||
first_Name: addss.first_Name,
|
first_Name: addss.first_Name,
|
||||||
|
@ -7,7 +7,7 @@ const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);
|
|||||||
// const stripe = require("stripe")("Your Secret Key");
|
// const stripe = require("stripe")("Your Secret Key");
|
||||||
import { Order } from "./orderModel.js";
|
import { Order } from "./orderModel.js";
|
||||||
|
|
||||||
import { shippingAddress } from "../ShippingAddresses/ShippingAddressModel.js";
|
import ShippingAddress from "../ShippingAddresses/ShippingAddressModel.js";
|
||||||
import sendEmail from "../../Utils/sendEmail.js";
|
import sendEmail from "../../Utils/sendEmail.js";
|
||||||
// const endpointSecret = STRIPE_SECRET_KEY;
|
// const endpointSecret = STRIPE_SECRET_KEY;
|
||||||
//generate unique order id
|
//generate unique order id
|
||||||
@ -48,7 +48,7 @@ export const handlePayment = async (req, res) => {
|
|||||||
return res.status(404).json({ msg: "please provide product subtotal" });
|
return res.status(404).json({ msg: "please provide product subtotal" });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let addss = await shippingAddress.findById(address);
|
let addss = await ShippingAddress.findById(address);
|
||||||
|
|
||||||
let shipping = {
|
let shipping = {
|
||||||
first_Name: addss.first_Name,
|
first_Name: addss.first_Name,
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
// import hashPassword from '../utils/hashPassword';
|
// import hashPassword from '../utils/hashPassword';
|
||||||
|
|
||||||
import crypto from "crypto";
|
import crypto from "crypto";
|
||||||
import SalesCoOrdinator from "./SalesCoOrdinatorModel.js";
|
import SalesCoOrdinator from "./SalesCoOrdinatorModel.js";
|
||||||
import sendEmail, { sendOtp } from "../../Utils/sendEmail.js";
|
import sendEmail, { sendOtp } from "../../Utils/sendEmail.js";
|
||||||
@ -8,11 +7,9 @@ import password from "secure-random-password";
|
|||||||
|
|
||||||
export const register = async (req, res) => {
|
export const register = async (req, res) => {
|
||||||
let { name, email, countryCode, mobileNumber } = req.body;
|
let { name, email, countryCode, mobileNumber } = req.body;
|
||||||
// console.log(req.body);
|
|
||||||
countryCode = countryCode?.trim();
|
countryCode = countryCode?.trim();
|
||||||
mobileNumber = mobileNumber?.trim();
|
mobileNumber = mobileNumber?.trim();
|
||||||
const fullMobileNumber = `${countryCode}${mobileNumber}`;
|
const fullMobileNumber = `${countryCode}${mobileNumber}`;
|
||||||
// console.log(fullMobileNumber);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let salesCoordinator = await SalesCoOrdinator.findOne({
|
let salesCoordinator = await SalesCoOrdinator.findOne({
|
||||||
@ -25,10 +22,9 @@ export const register = async (req, res) => {
|
|||||||
"SalesCoordinator already registered and verified for this mobile number.",
|
"SalesCoordinator already registered and verified for this mobile number.",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// const otp = crypto.randomInt(100000, 1000000).toString();
|
// const otp = crypto.randomInt(100000, 1000000).toString();
|
||||||
const otp = "123456";
|
const otp = "123456";
|
||||||
const otpExpires = Date.now() + 3 * 60 * 1000; // 3 minutes
|
const otpExpires = Date.now() + 1 * 60 * 1000;
|
||||||
|
|
||||||
if (salesCoordinator) {
|
if (salesCoordinator) {
|
||||||
salesCoordinator.otp = otp;
|
salesCoordinator.otp = otp;
|
||||||
@ -42,13 +38,17 @@ export const register = async (req, res) => {
|
|||||||
otpExpires,
|
otpExpires,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
// Generate uniqueId if not already present
|
||||||
|
if (!salesCoordinator.uniqueId) {
|
||||||
|
const currentYear = new Date().getFullYear().toString().slice(-2);
|
||||||
|
const randomChars = crypto.randomBytes(4).toString("hex").toUpperCase();
|
||||||
|
salesCoordinator.uniqueId = `${currentYear}-${randomChars}`;
|
||||||
|
}
|
||||||
await salesCoordinator.save();
|
await salesCoordinator.save();
|
||||||
// await sendOtp(
|
// await sendOtp(
|
||||||
// fullMobileNumber,
|
// fullMobileNumber,
|
||||||
// `Your Cheminova verification OTP is: ${otp}`
|
// Your Cheminova verification OTP is: ${otp}
|
||||||
// );
|
// );
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
message: `OTP sent to your mobile number ${fullMobileNumber} successfully`,
|
message: `OTP sent to your mobile number ${fullMobileNumber} successfully`,
|
||||||
});
|
});
|
||||||
@ -253,7 +253,7 @@ export const forgotPassword = async (req, res) => {
|
|||||||
password.digits,
|
password.digits,
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
// console.log(passwords);
|
// console.log(passwords);
|
||||||
salescoordinator.password = passwords;
|
salescoordinator.password = passwords;
|
||||||
await salescoordinator.save();
|
await salescoordinator.save();
|
||||||
// const message = `Your password reset token are :- \n\n ${resetPasswordUrl} \n\nyour new password is:${password}\n\nIf you have not requested this email then, please ignore it.`;
|
// const message = `Your password reset token are :- \n\n ${resetPasswordUrl} \n\nyour new password is:${password}\n\nIf you have not requested this email then, please ignore it.`;
|
||||||
@ -302,14 +302,16 @@ export const updateMobileNumber = async (req, res) => {
|
|||||||
"New mobile number cannot be the same as the old mobile number",
|
"New mobile number cannot be the same as the old mobile number",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// Check if the new mobile number already exists in another document
|
// Check if the new mobile number already exists in another document
|
||||||
const existingSalesCoordinator = await SalesCoOrdinator.findOne({
|
const existingSalesCoordinator = await SalesCoOrdinator.findOne({
|
||||||
mobileNumber: newFullMobileNumber,
|
mobileNumber: newFullMobileNumber,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (existingSalesCoordinator) {
|
if (existingSalesCoordinator) {
|
||||||
return res.status(400).json({ message: "Mobile number already registered and verified by someone" });
|
return res.status(400).json({
|
||||||
}
|
message: "Mobile number already registered and verified by someone",
|
||||||
|
});
|
||||||
|
}
|
||||||
// const otp = crypto.randomInt(100000, 1000000).toString();
|
// const otp = crypto.randomInt(100000, 1000000).toString();
|
||||||
const otp = "123456";
|
const otp = "123456";
|
||||||
const otpExpires = Date.now() + 3 * 60 * 1000; // 3 minutes
|
const otpExpires = Date.now() + 3 * 60 * 1000; // 3 minutes
|
||||||
|
@ -5,6 +5,7 @@ import validator from "validator";
|
|||||||
import bcrypt from "bcryptjs";
|
import bcrypt from "bcryptjs";
|
||||||
import jwt from "jsonwebtoken";
|
import jwt from "jsonwebtoken";
|
||||||
import crypto from "crypto";
|
import crypto from "crypto";
|
||||||
|
|
||||||
const salescoordinatorSchema = new mongoose.Schema(
|
const salescoordinatorSchema = new mongoose.Schema(
|
||||||
{
|
{
|
||||||
name: {
|
name: {
|
||||||
@ -31,16 +32,21 @@ const salescoordinatorSchema = new mongoose.Schema(
|
|||||||
type: String,
|
type: String,
|
||||||
required: false,
|
required: false,
|
||||||
unique: true,
|
unique: true,
|
||||||
validate: [validator.isEmail, "Please Enter a valid Email"],
|
validate: [validator.isEmail, "Please enter a valid Email"],
|
||||||
},
|
},
|
||||||
password: {
|
password: {
|
||||||
type: String,
|
type: String,
|
||||||
minLength: [6, "Password should be greater than 6 characters"],
|
minLength: [6, "Password should be greater than 6 characters"],
|
||||||
select: false, //find not got passpord
|
select: false,
|
||||||
},
|
},
|
||||||
newMobileNumber: { type: String },
|
newMobileNumber: { type: String },
|
||||||
resetPasswordToken: String,
|
resetPasswordToken: String,
|
||||||
resetPasswordExpire: Date,
|
resetPasswordExpire: Date,
|
||||||
|
uniqueId: {
|
||||||
|
type: String,
|
||||||
|
unique: true,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{ timestamps: true }
|
{ timestamps: true }
|
||||||
);
|
);
|
||||||
@ -53,6 +59,15 @@ salescoordinatorSchema.pre("save", async function (next) {
|
|||||||
this.password = await bcrypt.hash(this.password, 10);
|
this.password = await bcrypt.hash(this.password, 10);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
salescoordinatorSchema.pre("save", function (next) {
|
||||||
|
if (!this.uniqueId) {
|
||||||
|
const currentYear = new Date().getFullYear().toString().slice(-2);
|
||||||
|
const randomChars = crypto.randomBytes(4).toString("hex").toUpperCase();
|
||||||
|
this.uniqueId = `${currentYear}-${randomChars}`;
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
// JWT TOKEN
|
// JWT TOKEN
|
||||||
salescoordinatorSchema.methods.getJWTToken = function () {
|
salescoordinatorSchema.methods.getJWTToken = function () {
|
||||||
return jwt.sign({ id: this._id }, process.env.JWT_SECRET);
|
return jwt.sign({ id: this._id }, process.env.JWT_SECRET);
|
||||||
@ -63,19 +78,16 @@ salescoordinatorSchema.methods.comparePassword = async function (password) {
|
|||||||
return await bcrypt.compare(password, this.password);
|
return await bcrypt.compare(password, this.password);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Generating Reset Token
|
// Generating Reset Token
|
||||||
salescoordinatorSchema.methods.getResetPasswordToken = function () {
|
salescoordinatorSchema.methods.getResetPasswordToken = function () {
|
||||||
// Generating Token
|
|
||||||
const resetToken = crypto.randomBytes(20).toString("hex");
|
const resetToken = crypto.randomBytes(20).toString("hex");
|
||||||
|
|
||||||
// Hashing and adding reset PasswordToken to salescoordinatorSchema
|
|
||||||
this.resetPasswordToken = crypto
|
this.resetPasswordToken = crypto
|
||||||
.createHash("sha256")
|
.createHash("sha256")
|
||||||
.update(resetToken)
|
.update(resetToken)
|
||||||
.digest("hex");
|
.digest("hex");
|
||||||
//expire password time
|
|
||||||
// console.log(this.resetPasswordToken)
|
this.resetPasswordExpire = Date.now() + 15 * 60 * 1000; // 15 minutes
|
||||||
this.resetPasswordExpire = Date.now() + 15 * 60 * 1000; //15 minut
|
|
||||||
|
|
||||||
return resetToken;
|
return resetToken;
|
||||||
};
|
};
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { shippingAddress } from "./ShippingAddressModel.js";
|
import ShippingAddress from "./ShippingAddressModel.js";
|
||||||
export const AddshippingAddress = async (req, res) => {
|
export const AddshippingAddress = async (req, res) => {
|
||||||
// console.log("request came here");
|
// console.log("request came here");
|
||||||
try {
|
try {
|
||||||
@ -42,7 +42,7 @@ export const AddshippingAddress = async (req, res) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
req.body.user = req.user._id;
|
req.body.user = req.user._id;
|
||||||
const address = await shippingAddress.create(req.body);
|
const address = await ShippingAddress.create(req.body);
|
||||||
|
|
||||||
res.status(201).json({
|
res.status(201).json({
|
||||||
success: true,
|
success: true,
|
||||||
@ -59,28 +59,54 @@ export const AddshippingAddress = async (req, res) => {
|
|||||||
};
|
};
|
||||||
export const AddshippingAddressByAdmin = async (req, res) => {
|
export const AddshippingAddressByAdmin = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { street, city, state, postalCode, country } = req.body;
|
const {
|
||||||
|
street,
|
||||||
|
city,
|
||||||
|
state,
|
||||||
|
postalCode,
|
||||||
|
country,
|
||||||
|
panNumber,
|
||||||
|
tradeName,
|
||||||
|
gstNumber,
|
||||||
|
} = req.body;
|
||||||
// console.log(req.body);
|
// console.log(req.body);
|
||||||
if (!street) return res.status(404).json({ msg: "please provide street" });
|
// console.log(req.params._id);
|
||||||
if (!city) return res.status(404).json({ msg: "please provide city" });
|
// Validate required fields
|
||||||
if (!state) return res.status(404).json({ msg: "please provide state" });
|
if (!street || !city || !state || !postalCode || !panNumber) {
|
||||||
if (!postalCode)
|
return res
|
||||||
return res.status(404).json({ msg: "please provide postalCode" });
|
.status(400)
|
||||||
if (!country)
|
.json({ msg: "Please provide all required fields" });
|
||||||
return res.status(404).json({ msg: "please provide country" });
|
}
|
||||||
req.body.user = req.params._id;
|
|
||||||
const address = await shippingAddress.create(req.body);
|
|
||||||
|
|
||||||
|
// Validate PAN number format
|
||||||
|
const panNumberRegex = /^[A-Z]{5}[0-9]{4}[A-Z]{1}$/;
|
||||||
|
if (!panNumberRegex.test(panNumber)) {
|
||||||
|
return res.status(400).json({ msg: "Invalid PAN number format" });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create shipping address object
|
||||||
|
const newAddress = await ShippingAddress.create({
|
||||||
|
street,
|
||||||
|
city,
|
||||||
|
state,
|
||||||
|
postalCode,
|
||||||
|
country,
|
||||||
|
panNumber,
|
||||||
|
tradeName,
|
||||||
|
gstNumber,
|
||||||
|
user: req.params._id, // Assuming req.params._id contains the correct user ID
|
||||||
|
});
|
||||||
|
// console.log(newAddress);
|
||||||
res.status(201).json({
|
res.status(201).json({
|
||||||
success: true,
|
success: true,
|
||||||
address,
|
address: newAddress,
|
||||||
message: "shipping Address Added",
|
message: "Shipping address added successfully",
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error.message);
|
console.error("Error creating shipping address:", error.message);
|
||||||
res.status(500).json({
|
res.status(500).json({
|
||||||
success: false,
|
success: false,
|
||||||
message: error.message ? error.message : "Something went Wrong",
|
message: error.message ? error.message : "Something went wrong",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -88,10 +114,9 @@ export const AddshippingAddressByAdmin = async (req, res) => {
|
|||||||
// For website
|
// For website
|
||||||
export const getSingleUserSippingAddress = async (req, res) => {
|
export const getSingleUserSippingAddress = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const UserShippingAddress = await shippingAddress
|
const UserShippingAddress = await ShippingAddress.find({
|
||||||
.find({ user: req.user._id })
|
user: req.user._id,
|
||||||
|
}).sort({ createdAt: -1 });
|
||||||
.sort({ createdAt: -1 });
|
|
||||||
if (UserShippingAddress) {
|
if (UserShippingAddress) {
|
||||||
res.status(201).json({
|
res.status(201).json({
|
||||||
success: true,
|
success: true,
|
||||||
@ -110,9 +135,9 @@ export const getSingleUserSippingAddress = async (req, res) => {
|
|||||||
export const getSingleUserSippingAddressForAdmin = async (req, res) => {
|
export const getSingleUserSippingAddressForAdmin = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
// console.log(req.params._id);
|
// console.log(req.params._id);
|
||||||
const UserShippingAddress = await shippingAddress
|
const UserShippingAddress = await ShippingAddress.find({
|
||||||
.find({ user: req.params._id })
|
user: req.params._id,
|
||||||
.sort({ createdAt: -1 });
|
}).sort({ createdAt: -1 });
|
||||||
// console.log(UserShippingAddress);
|
// console.log(UserShippingAddress);
|
||||||
if (UserShippingAddress) {
|
if (UserShippingAddress) {
|
||||||
res.status(201).json({
|
res.status(201).json({
|
||||||
@ -136,7 +161,7 @@ export const deleteSelfShippingAddress = async (req, res) => {
|
|||||||
return res
|
return res
|
||||||
.status(400)
|
.status(400)
|
||||||
.json({ message: "please Provide shipping Address Id" });
|
.json({ message: "please Provide shipping Address Id" });
|
||||||
const getselfAddress = await shippingAddress.findById(req.params.id);
|
const getselfAddress = await ShippingAddress.findById(req.params.id);
|
||||||
if (!getselfAddress) {
|
if (!getselfAddress) {
|
||||||
return res.status(404).json({
|
return res.status(404).json({
|
||||||
success: false,
|
success: false,
|
||||||
@ -144,7 +169,7 @@ export const deleteSelfShippingAddress = async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (getselfAddress?.user.toString() === req.user._id.toString()) {
|
if (getselfAddress?.user.toString() === req.user._id.toString()) {
|
||||||
const address = await shippingAddress.findByIdAndDelete(req.params.id);
|
const address = await ShippingAddress.findByIdAndDelete(req.params.id);
|
||||||
await address.remove();
|
await address.remove();
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
@ -182,7 +207,7 @@ export const updateShippingAddress = async (req, res) => {
|
|||||||
return res
|
return res
|
||||||
.status(400)
|
.status(400)
|
||||||
.json({ message: "please Provide shipping Address Id" });
|
.json({ message: "please Provide shipping Address Id" });
|
||||||
const getselfAddress = await shippingAddress.findById(req.params.id);
|
const getselfAddress = await ShippingAddress.findById(req.params.id);
|
||||||
if (!getselfAddress) {
|
if (!getselfAddress) {
|
||||||
return res.status(404).json({
|
return res.status(404).json({
|
||||||
success: false,
|
success: false,
|
||||||
@ -226,7 +251,7 @@ export const updateShippingAddress = async (req, res) => {
|
|||||||
postalCode,
|
postalCode,
|
||||||
country,
|
country,
|
||||||
};
|
};
|
||||||
const updateShippingAddress = await shippingAddress.findByIdAndUpdate(
|
const updateShippingAddress = await ShippingAddress.findByIdAndUpdate(
|
||||||
{ _id: _id },
|
{ _id: _id },
|
||||||
{ $set: updateAddressData },
|
{ $set: updateAddressData },
|
||||||
{ new: true }
|
{ new: true }
|
||||||
@ -248,7 +273,7 @@ export const updateShippingAddress = async (req, res) => {
|
|||||||
export const getSingleSippingAddress = async (req, res) => {
|
export const getSingleSippingAddress = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
let _id = req.params.id;
|
let _id = req.params.id;
|
||||||
const address = await shippingAddress.findById({ _id: _id });
|
const address = await ShippingAddress.findById({ _id: _id });
|
||||||
|
|
||||||
if (address) {
|
if (address) {
|
||||||
res.status(201).json({
|
res.status(201).json({
|
||||||
|
@ -1,17 +1,15 @@
|
|||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
|
|
||||||
const shippingAddressSchema = new mongoose.Schema(
|
const shippingAddressSchema = new mongoose.Schema(
|
||||||
{
|
{
|
||||||
first_Name: {
|
firstName: {
|
||||||
type: String,
|
type: String,
|
||||||
// required: true,
|
|
||||||
},
|
},
|
||||||
last_Name: {
|
lastName: {
|
||||||
type: String,
|
type: String,
|
||||||
// required: true,
|
|
||||||
},
|
},
|
||||||
phone_Number: {
|
phoneNumber: {
|
||||||
type: Number,
|
type: Number,
|
||||||
// required: true,
|
|
||||||
},
|
},
|
||||||
street: {
|
street: {
|
||||||
type: String,
|
type: String,
|
||||||
@ -30,28 +28,34 @@ const shippingAddressSchema = new mongoose.Schema(
|
|||||||
type: String,
|
type: String,
|
||||||
required: true,
|
required: true,
|
||||||
trim: true,
|
trim: true,
|
||||||
// Add a regular expression to enforce a specific postal code format
|
|
||||||
// For example, assuming a 5-digit format for the United States
|
|
||||||
match: /^\d{6}$/,
|
match: /^\d{6}$/,
|
||||||
},
|
},
|
||||||
country: {
|
country: {
|
||||||
type: String,
|
type: String,
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
// company_name: {
|
panNumber: {
|
||||||
// type: String,
|
type: String,
|
||||||
// },
|
required: true,
|
||||||
// gst_number: {
|
},
|
||||||
// type: String,
|
tradeName: {
|
||||||
// validate: {
|
type: String,
|
||||||
// validator: function (v) {
|
},
|
||||||
// // Regular expression for Indian GST number validation
|
gstNumber: {
|
||||||
// return /^(\d{2}[A-Z]{5}\d{4}[A-Z]{1}\d[Z]{1}[A-Z\d]{1})$/.test(v);
|
type: String,
|
||||||
// },
|
validate: {
|
||||||
// message: (props) => `${props.value} is not a valid Indian GST number!`,
|
validator: function (v) {
|
||||||
// },
|
// Check if gstNumber is provided before applying validation
|
||||||
// },
|
if (v && v.trim().length > 0) {
|
||||||
default: {
|
return /^(\d{2}[A-Z]{5}\d{4}[A-Z]{1}\d[Z]{1}[A-Z\d]{1})$/.test(v);
|
||||||
|
}
|
||||||
|
// If gstNumber is not provided, it's considered valid
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
message: (props) => `${props.value} is not a valid GST number!`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
isDefault: {
|
||||||
type: Boolean,
|
type: Boolean,
|
||||||
default: false,
|
default: false,
|
||||||
},
|
},
|
||||||
@ -64,7 +68,6 @@ const shippingAddressSchema = new mongoose.Schema(
|
|||||||
{ timestamps: true, versionKey: false }
|
{ timestamps: true, versionKey: false }
|
||||||
);
|
);
|
||||||
|
|
||||||
export const shippingAddress = mongoose.model(
|
const ShippingAddress = mongoose.model("ShippingAddress", shippingAddressSchema);
|
||||||
"ShippingAddress",
|
|
||||||
shippingAddressSchema
|
export default ShippingAddress;
|
||||||
);
|
|
||||||
|
@ -86,7 +86,7 @@ import { Config } from "../setting/Configration/Config_model.js";
|
|||||||
export const registerUser = async (req, res) => {
|
export const registerUser = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { name, email, password, phone, accessTo, role } = req.body;
|
const { name, email, password, phone, accessTo, role } = req.body;
|
||||||
// console.log(req.body);
|
|
||||||
let findUser = await User.findOne({ email });
|
let findUser = await User.findOne({ email });
|
||||||
if (findUser) {
|
if (findUser) {
|
||||||
return res
|
return res
|
||||||
@ -94,41 +94,39 @@ export const registerUser = async (req, res) => {
|
|||||||
.json({ success: false, message: "User already exists" });
|
.json({ success: false, message: "User already exists" });
|
||||||
}
|
}
|
||||||
|
|
||||||
// let avatar = { public_id: "", url: "" };
|
const newUser = new User({
|
||||||
|
|
||||||
// if (req.files) {
|
|
||||||
// const files = req.files.avatar;
|
|
||||||
// const myCloud = await cloudinary.uploader.upload(files.tempFilePath, {
|
|
||||||
// folder: "Cheminova/user-image",
|
|
||||||
// });
|
|
||||||
// avatar = {
|
|
||||||
// public_id: myCloud.public_id,
|
|
||||||
// url: myCloud.secure_url,
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
|
|
||||||
const user = await User.create({
|
|
||||||
name,
|
name,
|
||||||
email,
|
email,
|
||||||
password,
|
password,
|
||||||
phone,
|
phone,
|
||||||
role,
|
role,
|
||||||
accessTo,
|
accessTo,
|
||||||
// avatar,
|
|
||||||
});
|
});
|
||||||
// Send email with the new password
|
|
||||||
|
// Generate uniqueId
|
||||||
|
const currentYear = new Date().getFullYear().toString().slice(-2);
|
||||||
|
const randomChars = crypto.randomBytes(4).toString("hex").toUpperCase();
|
||||||
|
newUser.uniqueId = `${currentYear}-${randomChars}`;
|
||||||
|
|
||||||
|
// Save the new user to the database
|
||||||
|
await newUser.save();
|
||||||
|
|
||||||
|
// Send email with the new user details
|
||||||
await sendEmail({
|
await sendEmail({
|
||||||
to: `${email}`, // Change to your recipient
|
to: email,
|
||||||
from: `${process.env.SEND_EMAIL_FROM}`, // Change to your verified sender
|
from: process.env.SEND_EMAIL_FROM,
|
||||||
subject: `Cheminova Account Created`,
|
subject: `Cheminova Account Created`,
|
||||||
html: `Your Principal Distributor Account is created successfully.
|
html: `Your Principal Distributor Account is created successfully.
|
||||||
<br/>name is: <strong>${name}</strong><br/>
|
<br/>Name: <strong>${name}</strong><br/>
|
||||||
<br/>MobileNumber is: <strong>${phone}</strong><br/>
|
<br/>Mobile Number: <strong>${phone}</strong><br/>
|
||||||
<br/>password is: <strong>${password}</strong><br/><br/>If you have not requested this email, please ignore it.`,
|
<br/>Password: <strong>${password}</strong><br/><br/>If you have not requested this email, please ignore it.`,
|
||||||
});
|
});
|
||||||
sendToken(user, 201, res);
|
|
||||||
} catch (e) {
|
// Respond with success and token
|
||||||
return res.status(400).json({ success: false, message: e.message });
|
res.status(201).json({ success: true, message: "User created successfully", userId: newUser._id });
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
res.status(400).json({ success: false, message: error.message });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// 2.Login User
|
// 2.Login User
|
||||||
@ -368,7 +366,7 @@ export const updatePassword = catchAsyncErrors(async (req, res, next) => {
|
|||||||
export const updateProfile = catchAsyncErrors(async (req, res, next) => {
|
export const updateProfile = catchAsyncErrors(async (req, res, next) => {
|
||||||
const newUserData = {
|
const newUserData = {
|
||||||
name: req.body.name,
|
name: req.body.name,
|
||||||
|
phone:req.body.phone,
|
||||||
email: req.body.email,
|
email: req.body.email,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -8,6 +8,11 @@ import crypto from "crypto";
|
|||||||
|
|
||||||
const userSchema = new mongoose.Schema(
|
const userSchema = new mongoose.Schema(
|
||||||
{
|
{
|
||||||
|
uniqueId: {
|
||||||
|
type: String,
|
||||||
|
unique: true,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
name: {
|
name: {
|
||||||
type: String,
|
type: String,
|
||||||
required: [true, "Please Enter Your Name"],
|
required: [true, "Please Enter Your Name"],
|
||||||
@ -20,7 +25,7 @@ const userSchema = new mongoose.Schema(
|
|||||||
unique: true,
|
unique: true,
|
||||||
validate: [validator.isEmail, "Please Enter a valid Email"],
|
validate: [validator.isEmail, "Please Enter a valid Email"],
|
||||||
},
|
},
|
||||||
phone: { // Change to 'mobileno' to match frontend field
|
phone: {
|
||||||
type: String,
|
type: String,
|
||||||
required: [true, "Please Enter Your phone no."],
|
required: [true, "Please Enter Your phone no."],
|
||||||
validate: {
|
validate: {
|
||||||
@ -64,6 +69,16 @@ userSchema.pre("save", async function (next) {
|
|||||||
this.password = await bcrypt.hash(this.password, 10);
|
this.password = await bcrypt.hash(this.password, 10);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Generate uniqueId before saving
|
||||||
|
userSchema.pre("save", function (next) {
|
||||||
|
if (!this.uniqueId) {
|
||||||
|
const currentYear = new Date().getFullYear().toString().slice(-2);
|
||||||
|
const randomChars = crypto.randomBytes(4).toString("hex").toUpperCase();
|
||||||
|
this.uniqueId = `${currentYear}-${randomChars}`;
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
// JWT TOKEN
|
// JWT TOKEN
|
||||||
userSchema.methods.getJWTToken = function () {
|
userSchema.methods.getJWTToken = function () {
|
||||||
return jwt.sign({ id: this._id }, process.env.JWT_SECRET, {
|
return jwt.sign({ id: this._id }, process.env.JWT_SECRET, {
|
||||||
|
Loading…
Reference in New Issue
Block a user