|
|
|
@ -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;
|
|
|
|
@ -1792,6 +1816,7 @@ const today = new Date();
|
|
|
|
|
const datePart = today.toISOString().slice(0, 10).replace(/-/g, ''); // YYYYMMDD
|
|
|
|
|
const randomDigit = Math.floor(Math.random() * 10); // 0–9
|
|
|
|
|
const bookingId = `ARM${datePart}${randomDigit}`;
|
|
|
|
|
const amount_due = matchedSupplier.quoted_amount-matchedSupplier.advance_paid
|
|
|
|
|
|
|
|
|
|
const newBooking = new Tankerbooking({
|
|
|
|
|
bookingid: bookingId,
|
|
|
|
@ -1806,7 +1831,9 @@ const bookingId = `ARM${datePart}${randomDigit}`;
|
|
|
|
|
supplierName: supplier.suppliername,
|
|
|
|
|
supplierPhone: supplier.phone,
|
|
|
|
|
supplierAddress: customer.address,
|
|
|
|
|
|
|
|
|
|
amount_paid: String(matchedSupplier.advance_paid),
|
|
|
|
|
amount_due: String(amount_due),
|
|
|
|
|
advance_reference_number: matchedSupplier.advance_ref_number,
|
|
|
|
|
type_of_water: requestedBooking.type_of_water,
|
|
|
|
|
capacity: requestedBooking.capacity,
|
|
|
|
|
quantity: requestedBooking.quantity,
|
|
|
|
@ -2052,3 +2079,55 @@ exports.updatestatusForSupplier = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.updateadvanceForSupplier = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { _id } = req.params;
|
|
|
|
|
const { supplierId, advance_paid,advance_ref_number } = req.body;
|
|
|
|
|
|
|
|
|
|
if (!_id) {
|
|
|
|
|
return reply.code(400).send({ status_code: 400, message: '_id (param) is required' });
|
|
|
|
|
}
|
|
|
|
|
if (!supplierId) {
|
|
|
|
|
return reply.code(400).send({ status_code: 400, message: 'supplierId (body) is required' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Map short keywords to the stored values
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// otherwise keep the original (but normalized) value
|
|
|
|
|
|
|
|
|
|
// Atomic update using positional $ operator
|
|
|
|
|
const filter = { _id, 'requested_suppliers.supplierId': supplierId };
|
|
|
|
|
const update = { $set: { 'requested_suppliers.$.advance_paid': advance_paid,'requested_suppliers.$.advance_ref_number': advance_ref_number } };
|
|
|
|
|
|
|
|
|
|
const updated = await RequestedBooking.findOneAndUpdate(filter, update, {
|
|
|
|
|
new: true,
|
|
|
|
|
runValidators: true,
|
|
|
|
|
}).lean();
|
|
|
|
|
|
|
|
|
|
if (!updated) {
|
|
|
|
|
// either booking _id not found OR supplierId not found inside requested_suppliers
|
|
|
|
|
const bookingExists = await RequestedBooking.findById(_id).lean();
|
|
|
|
|
if (!bookingExists) {
|
|
|
|
|
return reply.code(404).send({ status_code: 404, message: `Booking with _id ${_id} not found` });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// booking exists but supplier entry missing
|
|
|
|
|
return reply.code(404).send({
|
|
|
|
|
status_code: 404,
|
|
|
|
|
message: `Supplier ${supplierId} not found in requested_suppliers for booking ${_id}`,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return reply.code(200).send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
message: `status updated for supplier ${supplierId}`,
|
|
|
|
|
data: updated,
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('updatestatusForSupplier error:', err);
|
|
|
|
|
throw boom.boomify(err);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|