From 43e62cd0f3b9105d230834ad62444bd1afa85870 Mon Sep 17 00:00:00 2001 From: Varun Date: Fri, 19 Sep 2025 11:58:43 +0530 Subject: [PATCH] added customer details to request bookings --- src/controllers/userController.js | 38 +++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/src/controllers/userController.js b/src/controllers/userController.js index 2fda58fa..4da87f69 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -1552,11 +1552,32 @@ exports.getuserRequestbookingsForSupplier = async (req, reply) => { .sort({ createdAt: -1 }) .lean(); - // 2) For each booking, expose only this supplier's subdocument + if (!bookings.length) { + return reply.send({ + status_code: 200, + message: `No orders found for supplier ${supplierId}`, + data: [], + }); + } + + // 2) Collect all unique customerIds + const customerIds = [...new Set(bookings.map((b) => b.customerId))]; + + // 3) Fetch user details for those customers + const users = await User.find({ customerId: { $in: customerIds } }).lean(); + + // 4) Build map for quick lookup + const userMap = users.reduce((acc, u) => { + acc[u.customerId] = u; + return acc; + }, {}); + + // 5) Format data with user info const data = bookings.map((b) => { - const mySupplierEntry = (b.requested_suppliers || []).find( - (s) => s.supplierId === supplierId - ) || null; + const mySupplierEntry = + (b.requested_suppliers || []).find( + (s) => s.supplierId === supplierId + ) || null; return { _id: b._id, @@ -1567,13 +1588,15 @@ exports.getuserRequestbookingsForSupplier = async (req, reply) => { total_required_capacity: b.total_required_capacity, date: b.date, time: b.time, - // booking-wide status (e.g., pending/confirmed/cancelled) booking_status: b.status, createdAt: b.createdAt, updatedAt: b.updatedAt, - // only the supplier's own requested_suppliers entry - my_supplier_entry: mySupplierEntry, // { supplierId, quoted_amount, time, status } + // supplier-specific entry + my_supplier_entry: mySupplierEntry, + + // attach full user details here + customer_details: userMap[b.customerId] || null, }; }); @@ -1589,6 +1612,7 @@ exports.getuserRequestbookingsForSupplier = async (req, reply) => { }; + exports.getuserRequestbookingsforplansforsupplier = async (req, reply) => { try { const { supplierId } = req.params;