|
|
|
@ -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;
|
|
|
|
|