|
|
|
@ -1297,3 +1297,149 @@ exports.getuserOrders = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getuserRequestbookings = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { customerId } = req.params;
|
|
|
|
|
|
|
|
|
|
// 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: enrichedBookings
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
throw boom.boomify(err);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.acceptRequestedBooking = async (req, reply) => {
|
|
|
|
|
const { supplierId } = req.params;
|
|
|
|
|
const { action, _id } = req.body;
|
|
|
|
|
|
|
|
|
|
if (!["accept", "reject"].includes(action)) {
|
|
|
|
|
return reply.code(400).send({ message: "Invalid action. Must be 'accept' or 'reject'." });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const requestedBooking = await RequestedBooking.findOne({
|
|
|
|
|
_id: new mongoose.Types.ObjectId(_id),
|
|
|
|
|
'requested_suppliers.supplierId': supplierId
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!requestedBooking) {
|
|
|
|
|
return reply.code(404).send({ message: "No matching request for given ID and supplier" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const matchedSupplier = requestedBooking.requested_suppliers.find(s => s.supplierId === supplierId);
|
|
|
|
|
if (!matchedSupplier) {
|
|
|
|
|
return reply.code(404).send({ message: "Supplier not found in requested_suppliers array" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (action === "reject") {
|
|
|
|
|
matchedSupplier.status = "rejected_by_user";
|
|
|
|
|
await requestedBooking.save();
|
|
|
|
|
|
|
|
|
|
return reply.code(200).send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
message: "Supplier request rejected by user",
|
|
|
|
|
data: requestedBooking
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Accept path
|
|
|
|
|
requestedBooking.status = 'accepted';
|
|
|
|
|
await requestedBooking.save();
|
|
|
|
|
|
|
|
|
|
const customer = await User.findOne({ customerId: requestedBooking.customerId }).lean();
|
|
|
|
|
if (!customer) return reply.code(404).send({ message: "Customer not found" });
|
|
|
|
|
|
|
|
|
|
const supplier = await Supplier.findOne({ supplierId }).lean();
|
|
|
|
|
if (!supplier) return reply.code(404).send({ message: "Supplier not found" });
|
|
|
|
|
|
|
|
|
|
if (!matchedSupplier.quoted_amount) {
|
|
|
|
|
return reply.code(400).send({ message: "Quoted amount missing for this supplier" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const newBooking = new Tankerbooking({
|
|
|
|
|
customerId: customer.customerId,
|
|
|
|
|
customerName: customer.profile.firstName,
|
|
|
|
|
customerPhone: customer.phone,
|
|
|
|
|
address: customer.address1,
|
|
|
|
|
latitude: customer.latitude,
|
|
|
|
|
longitude: customer.longitude,
|
|
|
|
|
|
|
|
|
|
supplierId: supplier.supplierId,
|
|
|
|
|
supplierName: supplier.suppliername,
|
|
|
|
|
supplierPhone: supplier.phone,
|
|
|
|
|
supplierAddress: customer.address,
|
|
|
|
|
|
|
|
|
|
type_of_water: requestedBooking.type_of_water,
|
|
|
|
|
capacity: requestedBooking.capacity,
|
|
|
|
|
quantity: requestedBooking.quantity,
|
|
|
|
|
total_required_capacity: requestedBooking.total_required_capacity,
|
|
|
|
|
expectedDateOfDelivery: requestedBooking.date,
|
|
|
|
|
time: requestedBooking.time,
|
|
|
|
|
price: matchedSupplier.quoted_amount,
|
|
|
|
|
|
|
|
|
|
status: 'pending'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await newBooking.save();
|
|
|
|
|
|
|
|
|
|
reply.code(200).send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
message: "Booking accepted and moved to tanker bookings",
|
|
|
|
|
data: newBooking
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
throw boom.internal("Failed to handle booking action", err);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|