ashok 4 months ago
commit 917d402720

@ -939,6 +939,8 @@ exports.getCurrentSupplier = async (req, reply) => {
// } // }
// }; // };
exports.getSuppliers = async (req, reply) => { exports.getSuppliers = async (req, reply) => {
const customerId = req.params.customerId; const customerId = req.params.customerId;
@ -947,31 +949,46 @@ exports.getSuppliers = async (req, reply) => {
capacity: requestedCapacityStr, capacity: requestedCapacityStr,
quantity: requestedQuantityStr, quantity: requestedQuantityStr,
date, date,
time time,
price_from,
price_to
} = req.body; } = req.body;
const parseCapacity = (value) => parseFloat((value || "0").toString().replace(/,/g, "")); const parseCapacity = (value) => parseFloat((value || "0").toString().replace(/,/g, ""));
const parsePrice = (value) => parseInt((value || "0").toString().replace(/,/g, ""));
const requestedCapacity = parseCapacity(requestedCapacityStr); const requestedCapacity = parseCapacity(requestedCapacityStr);
const requestedQuantity = parseInt(requestedQuantityStr || "0"); const requestedQuantity = parseInt(requestedQuantityStr || "0");
const totalRequiredCapacity = requestedCapacity * requestedQuantity; const totalRequiredCapacity = requestedCapacity * requestedQuantity;
const priceFrom = parsePrice(price_from);
const priceTo = parsePrice(price_to);
try { try {
const customerData = await User.findOne({ customerId });
const favorateSuppliers = customerData?.favorate_suppliers || [];
const tankerBookings = await Tankerbooking.find({ date }); const tankerBookings = await Tankerbooking.find({ date });
const bookedTankerSet = new Set( const bookedTankerSet = new Set(
tankerBookings.map(booking => `${booking.supplierId}_${booking.tankerName}`) tankerBookings.map(booking => `${booking.supplierId}_${booking.tankerName}`)
); );
// 1Only filter typeofwater first // 1Filter tankers by typeofwater first
let tankers = await Tanker.find({ typeofwater: type_of_water }); 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 => { tankers = tankers.filter(tanker => {
const key = `${tanker.supplierId}_${tanker.tankerName}`; const key = `${tanker.supplierId}_${tanker.tankerName}`;
return !bookedTankerSet.has(key); return !bookedTankerSet.has(key);
}); });
// 3⃣ Group by supplierId // 4⃣ Group tankers by supplierId
const supplierTankerMap = {}; const supplierTankerMap = {};
for (let tanker of tankers) { for (let tanker of tankers) {
if (!supplierTankerMap[tanker.supplierId]) { if (!supplierTankerMap[tanker.supplierId]) {
@ -980,7 +997,7 @@ exports.getSuppliers = async (req, reply) => {
supplierTankerMap[tanker.supplierId].push(tanker); supplierTankerMap[tanker.supplierId].push(tanker);
} }
// 4️⃣ Aggregate supplier capacities // 5️⃣ Aggregate supplier capacities
const qualifiedSuppliers = []; const qualifiedSuppliers = [];
for (let [supplierId, supplierTankers] of Object.entries(supplierTankerMap)) { for (let [supplierId, supplierTankers] of Object.entries(supplierTankerMap)) {
const totalAvailableCapacity = supplierTankers.reduce( const totalAvailableCapacity = supplierTankers.reduce(
@ -993,34 +1010,30 @@ exports.getSuppliers = async (req, reply) => {
} }
} }
const connected_suppliers = []; const suppliers = [];
const non_connected_suppliers = [];
for (let supplierObj of qualifiedSuppliers) { for (let supplierObj of qualifiedSuppliers) {
const supplierData = await Supplier.findOne({ supplierId: supplierObj.supplierId }); 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({ const friendRequest = await FriendRequest.findOne({
customerId: customerId, customerId: customerId,
supplierId: supplierObj.supplierId supplierId: supplierObj.supplierId
}); });
if (friendRequest && friendRequest.status === "accepted") { const isConnected = friendRequest && friendRequest.status === "accepted";
connected_suppliers.push(responseData); const isFavorite = favorateSuppliers.includes(supplierObj.supplierId);
} else {
non_connected_suppliers.push(responseData); suppliers.push({
} supplier: supplierData,
tankers: supplierObj.tankers,
isConnected: isConnected,
isFavorite: isFavorite
});
} }
reply.send({ reply.send({
status_code: 200, status_code: 200,
connected_suppliers, suppliers
non_connected_suppliers
}); });
} catch (err) { } catch (err) {
@ -1038,6 +1051,7 @@ exports.getSuppliers = async (req, reply) => {
// Get single user by ID // Get single user by ID
exports.getSingleSupplier = async (req, reply) => { exports.getSingleSupplier = async (req, reply) => {
try { try {

@ -30,6 +30,12 @@ module.exports = function (fastify, opts, next) {
capacity: { type: "string" }, capacity: { type: "string" },
quantity: { type: "string" }, quantity: { type: "string" },
date: { 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" }, time: { type: "string" },
}, },

Loading…
Cancel
Save