diff --git a/src/handlers/supplierHandler.js b/src/handlers/supplierHandler.js index 2bcd56b8..58e02674 100644 --- a/src/handlers/supplierHandler.js +++ b/src/handlers/supplierHandler.js @@ -952,7 +952,7 @@ exports.getSuppliers = async (req, reply) => { time, price_from, price_to, - radius_from, // even if sent we will not apply now + radius_from, radius_to, rating_from, rating_to @@ -977,7 +977,6 @@ exports.getSuppliers = async (req, reply) => { tankerBookings.map(booking => `${booking.supplierId}_${booking.tankerName}`) ); - // 🟢 Initial tanker query condition const tankerQuery = {}; if (type_of_water && type_of_water.trim() !== "") { @@ -986,7 +985,6 @@ exports.getSuppliers = async (req, reply) => { let tankers = await Tanker.find(tankerQuery); - // 🟢 Apply price filter only if both price_from and price_to are given (not empty) if (price_from && price_to) { tankers = tankers.filter(tanker => { const tankerPrice = parsePrice(tanker.price); @@ -994,13 +992,11 @@ exports.getSuppliers = async (req, reply) => { }); } - // 🟢 Exclude booked tankers tankers = tankers.filter(tanker => { const key = `${tanker.supplierId}_${tanker.tankerName}`; return !bookedTankerSet.has(key); }); - // 🟢 Group tankers by supplierId const supplierTankerMap = {}; for (let tanker of tankers) { if (!supplierTankerMap[tanker.supplierId]) { @@ -1017,7 +1013,6 @@ exports.getSuppliers = async (req, reply) => { 0 ); - // 🟢 Apply capacity filtering only if both are given if (requestedCapacity > 0 && requestedQuantity > 0) { if (totalAvailableCapacity < totalRequiredCapacity) { continue; @@ -1040,11 +1035,27 @@ exports.getSuppliers = async (req, reply) => { const isConnected = friendRequest && friendRequest.status === "accepted"; const isFavorite = favorateSuppliers.includes(supplierObj.supplierId); + // 🔥 Now check RequestedBooking collection + const requestedBookingRecord = await RequestedBooking.findOne({ + customerId: customerId, + "requested_suppliers.supplierId": supplierObj.supplierId + }); + + let requestedBooking = { status: false }; + + if (requestedBookingRecord) { + requestedBooking = { + status: true, + time: requestedBookingRecord.time + }; + } + suppliers.push({ supplier: supplierData, tankers: supplierObj.tankers, isConnected: isConnected, - isFavorite: isFavorite + isFavorite: isFavorite, + requestedBooking: requestedBooking }); } diff --git a/src/routes/supplierRoute.js b/src/routes/supplierRoute.js index 7aea5b39..46ac1926 100644 --- a/src/routes/supplierRoute.js +++ b/src/routes/supplierRoute.js @@ -75,7 +75,7 @@ fastify.post("/api/requestedbookings", { properties: { supplierId: { type: "string" }, quoted_amount: { type: "number" }, - custom_field: { type: "string" } // ✅ New field + time: { type: "string" } // ✅ New field } } }