cart added in user

master^2
Varun 4 months ago
parent 5e618a137e
commit 29683f044b

@ -12,7 +12,7 @@ const boom = require("boom");
// Get Data Models
const { Supplier, generateSupplierId, FriendRequest,DeliveryBoy} = require("../models/supplier")
const { User,Counter, generateBookingId,resetCounter,generateCustomerId,ProfilePicture, AddTeamMembers} = require('../models/User')
const { User,Counter, generateBookingId,resetCounter,generateCustomerId,ProfilePicture, AddTeamMembers,Cart} = require('../models/User')
//const User = require("../models/User");
const customJwtAuth = require("../customAuthJwt");
@ -1178,4 +1178,97 @@ exports.getFavoriteSuppliers = async (req, reply) => {
};
exports.getCartByUserId = async (req, reply) => {
try {
const { customerId } = req.params;
const cart = await Cart.findOne({ customerId }) || { customerId, items: [] };
reply.send({
status_code: 200,
message: "Cart fetched successfully",
data: cart,
});
} catch (err) {
console.error("Error fetching cart:", err);
reply.status(500).send({ error: "Internal server error" });
}
};
exports.addItemToCart = async (req, reply) => {
try {
const { customerId } = req.params;
const { productId, name, quantity, price } = req.body;
let cart = await Cart.findOne({ customerId });
if (!cart) {
cart = new Cart({ customerId, items: [] });
}
const existingItem = cart.items.find(item => item.productId === productId);
if (existingItem) {
existingItem.quantity += quantity;
} else {
cart.items.push({ productId, name, quantity, price });
}
await cart.save();
reply.send({
status_code: 200,
message: "Item added to cart",
data: cart,
});
} catch (err) {
console.error("Error adding item:", err);
reply.status(500).send({ error: "Internal server error" });
}
};
exports.removeItemFromCart = async (req, reply) => {
try {
const { customerId } = req.params;
const { productId } = req.body;
const cart = await Cart.findOne({ customerId });
if (!cart) {
return reply.status(404).send({ error: "Cart not found" });
}
cart.items = cart.items.filter(item => item.productId !== productId);
await cart.save();
reply.send({
status_code: 200,
message: "Item removed from cart",
data: cart,
});
} catch (err) {
console.error("Error removing item:", err);
reply.status(500).send({ error: "Internal server error" });
}
};
exports.clearCart = async (req, reply) => {
try {
const { customerId } = req.params;
const cart = await Cart.findOneAndUpdate(
{ customerId },
{ items: [] },
{ new: true }
);
reply.send({
status_code: 200,
message: "Cart cleared",
data: cart,
});
} catch (err) {
console.error("Error clearing cart:", err);
reply.status(500).send({ error: "Internal server error" });
}
};

@ -194,6 +194,20 @@ const teamMembersSchema = new mongoose.Schema({
fcmId: { type: String, default: null },
});
const cartItemSchema = new mongoose.Schema({
productId: { type: String, required: true },
name: { type: String, default: null },
quantity: { type: Number, default: 1 },
price: { type: Number, default: 0 },
});
const cartSchema = new mongoose.Schema({
customerId: { type: String, required: true },
items: [cartItemSchema],
}, { timestamps: true });
const Cart = mongoose.model("Cart", cartSchema);
const ProfilePicture = mongoose.model('ProfilePicture', profilePictureSchema);
const Counter = mongoose.model('Counter', CounterSchema);
@ -207,4 +221,4 @@ const AddTeamMembers = mongoose.model("AddTeamMembers", teamMembersSchema);
//module.exports = mongoose.model("User", userSchema);
module.exports = { User,Counter, generateCustomerId,generateBookingId ,resetCounter,ProfilePicture,AddTeamMembers};
module.exports = { User,Counter, generateCustomerId,generateBookingId ,resetCounter,ProfilePicture,AddTeamMembers,Cart};

@ -1040,7 +1040,86 @@ fastify.route({
handler: userController.getFavoriteSuppliers
});
fastify.get("/api/cart/:customerId", {
schema: {
tags: ["User"],
description: "Fetch cart by userId",
summary: "Get cart",
params: {
type: "object",
properties: {
customerId: { type: "string" },
},
required: ["customerId"],
},
},
handler: userController.getCartByUserId,
});
fastify.post("/api/cart/:customerId/add", {
schema: {
tags: ["User"],
description: "Add item to cart",
summary: "Add item",
params: {
type: "object",
properties: {
customerId: { type: "string" },
},
required: ["customerId"],
},
body: {
type: "object",
properties: {
productId: { type: "string" },
name: { type: "string" },
quantity: { type: "number" },
price: { type: "number" },
},
required: ["productId", "quantity", "price"],
},
},
handler: userController.addItemToCart,
});
fastify.post("/api/cart/:customerId/remove", {
schema: {
tags: ["User"],
description: "Remove item from cart",
summary: "Remove item",
params: {
type: "object",
properties: {
customerId: { type: "string" },
},
required: ["customerId"],
},
body: {
type: "object",
properties: {
productId: { type: "string" },
},
required: ["productId"],
},
},
handler: userController.removeItemFromCart,
});
fastify.delete("/api/cart/:customerId/clear", {
schema: {
tags: ["User"],
description: "Clear entire cart",
summary: "Clear cart",
params: {
type: "object",
properties: {
customerId: { type: "string" },
},
required: ["customerId"],
},
},
handler: userController.clearCart,
});

Loading…
Cancel
Save