diff --git a/src/handlers/supplierHandler.js b/src/handlers/supplierHandler.js index bf4e5765..c6e5832f 100644 --- a/src/handlers/supplierHandler.js +++ b/src/handlers/supplierHandler.js @@ -939,6 +939,8 @@ exports.getCurrentSupplier = async (req, reply) => { // } // }; + + exports.getSuppliers = async (req, reply) => { const customerId = req.params.customerId; @@ -947,31 +949,46 @@ exports.getSuppliers = async (req, reply) => { capacity: requestedCapacityStr, quantity: requestedQuantityStr, date, - time + time, + price_from, + price_to } = req.body; const parseCapacity = (value) => parseFloat((value || "0").toString().replace(/,/g, "")); - + const parsePrice = (value) => parseInt((value || "0").toString().replace(/,/g, "")); + const requestedCapacity = parseCapacity(requestedCapacityStr); const requestedQuantity = parseInt(requestedQuantityStr || "0"); const totalRequiredCapacity = requestedCapacity * requestedQuantity; + const priceFrom = parsePrice(price_from); + const priceTo = parsePrice(price_to); + try { + const customerData = await User.findOne({ customerId }); + const favorateSuppliers = customerData?.favorate_suppliers || []; + const tankerBookings = await Tankerbooking.find({ date }); const bookedTankerSet = new Set( tankerBookings.map(booking => `${booking.supplierId}_${booking.tankerName}`) ); - // 1️⃣ Only filter typeofwater first + // 1️⃣ Filter tankers by typeofwater first let tankers = await Tanker.find({ typeofwater: type_of_water }); - // 2️⃣ Exclude booked tankers + // 2️⃣ Filter by price range + tankers = tankers.filter(tanker => { + const tankerPrice = parsePrice(tanker.price); + return tankerPrice >= priceFrom && tankerPrice <= priceTo; + }); + + // 3️⃣ Exclude booked tankers tankers = tankers.filter(tanker => { const key = `${tanker.supplierId}_${tanker.tankerName}`; return !bookedTankerSet.has(key); }); - // 3️⃣ Group by supplierId + // 4️⃣ Group tankers by supplierId const supplierTankerMap = {}; for (let tanker of tankers) { if (!supplierTankerMap[tanker.supplierId]) { @@ -980,7 +997,7 @@ exports.getSuppliers = async (req, reply) => { supplierTankerMap[tanker.supplierId].push(tanker); } - // 4️⃣ Aggregate supplier capacities + // 5️⃣ Aggregate supplier capacities const qualifiedSuppliers = []; for (let [supplierId, supplierTankers] of Object.entries(supplierTankerMap)) { const totalAvailableCapacity = supplierTankers.reduce( @@ -993,34 +1010,30 @@ exports.getSuppliers = async (req, reply) => { } } - const connected_suppliers = []; - const non_connected_suppliers = []; + const suppliers = []; for (let supplierObj of qualifiedSuppliers) { const supplierData = await Supplier.findOne({ supplierId: supplierObj.supplierId }); - const responseData = { - supplier: supplierData, - tankers: supplierObj.tankers - }; - - // 🔥 The correct friend request check: const friendRequest = await FriendRequest.findOne({ customerId: customerId, supplierId: supplierObj.supplierId }); - if (friendRequest && friendRequest.status === "accepted") { - connected_suppliers.push(responseData); - } else { - non_connected_suppliers.push(responseData); - } + const isConnected = friendRequest && friendRequest.status === "accepted"; + const isFavorite = favorateSuppliers.includes(supplierObj.supplierId); + + suppliers.push({ + supplier: supplierData, + tankers: supplierObj.tankers, + isConnected: isConnected, + isFavorite: isFavorite + }); } reply.send({ status_code: 200, - connected_suppliers, - non_connected_suppliers + suppliers }); } catch (err) { @@ -1038,6 +1051,7 @@ exports.getSuppliers = async (req, reply) => { + // Get single user by ID exports.getSingleSupplier = async (req, reply) => { try { diff --git a/src/routes/supplierRoute.js b/src/routes/supplierRoute.js index 0eb38114..368d45ae 100644 --- a/src/routes/supplierRoute.js +++ b/src/routes/supplierRoute.js @@ -30,8 +30,14 @@ module.exports = function (fastify, opts, next) { capacity: { type: "string" }, quantity: { type: "string" }, date: { type: "string" }, + radius_from:{ type: "string" }, + radius_to:{ type: "string" }, + rating_to:{ type: "string" }, + rating_to:{ type: "string" }, + price_from: { type: "string" }, + price_to: { type: "string" }, time: { type: "string" }, - + }, },