Skip to content
Open
46 changes: 24 additions & 22 deletions controllers/customerController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ const bcrypt = require('bcrypt');
const Customer = require('../models/customerSchema.js');
const { createNewToken } = require('../utils/token.js');




const customerRegister = async (req, res) => {
try {
const existingcustomerByEmail = await Customer.findOne({ email: req.body.email });
if (existingcustomerByEmail) {
res.send({ message: 'Email already exists' });
}

const salt = await bcrypt.genSalt(10);
const hashedPass = await bcrypt.hash(req.body.password, salt);

Expand All @@ -12,24 +20,18 @@ const customerRegister = async (req, res) => {
password: hashedPass
});

const existingcustomerByEmail = await Customer.findOne({ email: req.body.email });

if (existingcustomerByEmail) {
res.send({ message: 'Email already exists' });
}
else {
let result = await customer.save();
result.password = undefined;

const token = createNewToken(result._id)

let result = await customer.save();
result.password = undefined;

const token = createNewToken(result._id)

result = {
...result._doc,
token: token
};
result = {
...result._doc,
token: token
};

res.send(result);
}
res.status(200).json.send(result);
} catch (err) {
res.status(500).json(err);
}
Expand All @@ -38,9 +40,9 @@ const customerRegister = async (req, res) => {
const customerLogIn = async (req, res) => {
if (req.body.email && req.body.password) {
let customer = await Customer.findOne({ email: req.body.email });
if (!customer) {
if (customer) {
const validated = await bcrypt.compare(req.body.password, customer.password);
if (!validated) {
if (validated) {
customer.password = undefined;

const token = createNewToken(customer._id)
Expand All @@ -50,7 +52,7 @@ const customerLogIn = async (req, res) => {
token: token
};

res.send(customer);
res.status(200).send(customer);
} else {
res.send({ message: "Invalid password" });
}
Expand All @@ -64,12 +66,12 @@ const customerLogIn = async (req, res) => {

const getCartDetail = async (req, res) => {
try {
let customer = await Customer.findBy(req.params.id)
let customer = await Customer.findById(req.params.id)
if (customer) {
res.get(customer.cartDetails);
res.json(customer.cartDetails);
}
else {
res.send({ message: "No customer found" });
res.status(200).send({ message: "No customer found" });
}
} catch (err) {
res.status(500).json(err);
Expand Down
19 changes: 10 additions & 9 deletions controllers/productController.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const getProducts = async (req, res) => {

const getSellerProducts = async (req, res) => {
try {
let products = await Product.find({ seller: req.params.id })
const products = await Product.find({ seller: req.params.id })
if (products.length > 0) {
res.send(products)
} else {
Expand All @@ -41,16 +41,17 @@ const getSellerProducts = async (req, res) => {

const getProductDetail = async (req, res) => {
try {
let product = await Product.findById(req.params.id)
.populate("seller", "shopName")
.populate({
path: "reviews.reviewer",
model: "customer",
select: "name"
});
const product = await Product.findById(req.params.id)
console.log(`Fetching product with ID: ${product}`); // Debug output
// .populate("seller", "shopName")
// .populate({
// path: "reviews.reviewer",
// model: "Customer",
// select: "name"
// });

if (product) {
res.send(product);
res.status(200).json(product);
}
else {
res.send({ message: "No product found" });
Expand Down
4 changes: 2 additions & 2 deletions controllers/sellerController.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const sellerRegister = async (req, res) => {

const seller = new Seller({
...req.body,
password: bcrypt.hash
password: hashedPass
});

const existingSellerByEmail = await Seller.findOne({ email: req.body.email });
Expand Down Expand Up @@ -51,7 +51,7 @@ const sellerLogIn = async (req, res) => {

seller = {
...seller._doc,
token: tokens
token: token
};

res.send(seller);
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ const dotenv = require("dotenv")
const app = express()
const Routes = require("./routes/route.js")

const PORT = process.env.PORT || 5000

dotenv.config();
const PORT = process.env.PORT || 5000


app.use(express.json({ limit: '10mb' }))
app.use(cors())
Expand Down
8 changes: 4 additions & 4 deletions models/customerSchema.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const mongoose = require("mongoose")

const customerSchema = mongoose.Schema({
const customerSchema = new mongoose.Schema({
name: {
type: String,
required: true,
Expand Down Expand Up @@ -54,7 +54,7 @@ const customerSchema = mongoose.Schema({
},
seller: {
type: mongoose.Schema.Types.ObjectId,
ref: 'SELLER'
ref: 'Seller'
},
}],
shippingData: {
Expand All @@ -68,7 +68,7 @@ const customerSchema = mongoose.Schema({
type: String,
},
country: {
type: Number,
type: String,
},
pinCode: {
type: Number,
Expand All @@ -79,4 +79,4 @@ const customerSchema = mongoose.Schema({
}
});

module.exports = mongoose.model("customer", customerSchema)
module.exports = mongoose.model("Customer", customerSchema)
6 changes: 3 additions & 3 deletions models/orderSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const orderSchema = new mongoose.Schema(
{
buyer: {
type: mongoose.Schema.ObjectId,
ref: "customer",
ref: "Customer",
required: true,
},
shippingData: {
Expand Down Expand Up @@ -68,7 +68,7 @@ const orderSchema = new mongoose.Schema(
},
seller: {
type: mongoose.Schema.Types.ObjectId,
ref: 'seller'
ref: 'Seller'
},
}],
paymentInfo: {
Expand Down Expand Up @@ -117,4 +117,4 @@ const orderSchema = new mongoose.Schema(
},
});

module.exports = mongoose.model("customer", orderSchema);
module.exports = mongoose.model("Order", orderSchema);
11 changes: 6 additions & 5 deletions models/productSchema.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const mongoose = require("mongoose")

const productSchema = mongoose.Schema(
const productSchema = new mongoose.Schema(
{
productName: {
type: String
Expand Down Expand Up @@ -45,18 +45,19 @@ const productSchema = mongoose.Schema(
},
reviewer: {
type: mongoose.Schema.Types.ObjectId,
ref: "CUSTOMERS",
ref: "Customer",
},
date: {
type: Date,
default: Text,
default: Date.now()

},
},
],
seller: {
type: mongoose.Schema.Types.ObjectId,
ref: 'seller'
ref: 'Seller'
},
}, { timestamps: false});

module.exports = mongoose.mongoose("product", productSchema)
module.exports = mongoose.model("Product", productSchema)
4 changes: 3 additions & 1 deletion models/sellerSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ const sellerSchema = new mongoose.Schema({
}
});

moduleexports = mongoose.model("seller", sellerSchema)

module.exports = mongoose.model("Seller", sellerSchema)

Loading