master^2
Varun 5 months ago
parent 0c9fa6430e
commit 40a3b205a3

@ -899,16 +899,26 @@ exports.addingfavoratesupplier = async (req, reply) => {
return reply.code(400).send({ status_code: 400, message: "supplierId is required" }); return reply.code(400).send({ status_code: 400, message: "supplierId is required" });
} }
// Find supplier by supplierId string to get the _id
const supplier = await Supplier.findOne({ supplierId });
if (!supplier) {
return reply.code(404).send({ status_code: 404, message: "Supplier not found" });
}
// Find user // Find user
const user = await User.findOne({ customerId }); const user = await User.findOne({ customerId });
if (!user) { if (!user) {
return reply.code(404).send({ status_code: 404, message: "User not found" }); return reply.code(404).send({ status_code: 404, message: "User not found" });
} }
// Add supplierId to favorate_suppliers if not already there // Add supplier._id to favorate_suppliers if not already present
if (!user.favorate_suppliers.includes(supplierId)) { const supplierObjectId = supplier._id.toString();
user.favorate_suppliers.push(supplierId); const isAlreadyFavorite = user.favorate_suppliers.some(
(id) => id.toString() === supplierObjectId
);
if (!isAlreadyFavorite) {
user.favorate_suppliers.push(supplier._id);
await user.save(); await user.save();
} }
@ -929,10 +939,11 @@ exports.addingfavoratesupplier = async (req, reply) => {
}); });
} catch (err) { } catch (err) {
console.error(err); console.error(err);
throw boom.boomify(err); reply.status(500).send({ status_code: 500, message: err.message });
} }
}; };
exports.editFavoriteSupplier = async (req, reply) => { exports.editFavoriteSupplier = async (req, reply) => {
try { try {
const { customerId } = req.params; const { customerId } = req.params;

Loading…
Cancel
Save