From d6ff5a204c7bd8d63f9dac2690a0d04f131ba714 Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 2 Jul 2025 15:13:21 +0530 Subject: [PATCH] changes in get request bookings of particular customer --- src/controllers/userController.js | 45 +++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/src/controllers/userController.js b/src/controllers/userController.js index f23324b7..bcccb50f 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -1297,19 +1297,59 @@ exports.getuserOrders = async (req, reply) => { } }; + + exports.getuserRequestbookings = async (req, reply) => { try { const { customerId } = req.params; - const orders = await RequestedBooking.find({ customerId }).sort({ createdAt: -1 }).lean(); + // 1. Get all bookings + const bookings = await RequestedBooking.find({ customerId }).sort({ createdAt: -1 }).lean(); + // 2. Collect all supplierIds used + const allSupplierIds = new Set(); + bookings.forEach(booking => { + booking.requested_suppliers?.forEach(s => { + if (s.supplierId) allSupplierIds.add(s.supplierId); + }); + }); + + // 3. Query all supplier details at once + const supplierList = await Supplier.find({ + supplierId: { $in: [...allSupplierIds] } + }).lean(); + + const supplierMap = {}; + supplierList.forEach(s => { + supplierMap[s.supplierId] = { + supplierId: s.supplierId, + supplierName: s.suppliername, + phone: s.phone, + longitude: s.longitude, + latitude: s.latitude, + address: s.profile?.office_address, + status: s.status + }; + }); + + // 4. Attach supplier_details inside each requested_suppliers[] object + const enrichedBookings = bookings.map(booking => { + booking.requested_suppliers = booking.requested_suppliers.map(supplier => ({ + ...supplier, + supplier_details: supplierMap[supplier.supplierId] || null + })); + return booking; + }); + + // 5. Send final response return reply.send({ status_code: 200, message: `Orders for customer ${customerId} fetched successfully`, - data: orders + data: enrichedBookings }); } catch (err) { + console.error(err); throw boom.boomify(err); } }; @@ -1317,6 +1357,7 @@ exports.getuserRequestbookings = async (req, reply) => { + exports.acceptRequestedBooking = async (req, reply) => { const { supplierId } = req.params; const { action, _id } = req.body;