|
|
@ -973,22 +973,30 @@ exports.getSuppliers = async (req, reply) => {
|
|
|
|
tankerBookings.map(booking => `${booking.supplierId}_${booking.tankerName}`)
|
|
|
|
tankerBookings.map(booking => `${booking.supplierId}_${booking.tankerName}`)
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// 1️⃣ Filter tankers by typeofwater first
|
|
|
|
// 🟢 Initial tanker query condition
|
|
|
|
let tankers = await Tanker.find({ typeofwater: type_of_water });
|
|
|
|
const tankerQuery = {};
|
|
|
|
|
|
|
|
|
|
|
|
// 2️⃣ Filter by price range
|
|
|
|
if (type_of_water && type_of_water.trim() !== "") {
|
|
|
|
tankers = tankers.filter(tanker => {
|
|
|
|
tankerQuery.typeofwater = type_of_water;
|
|
|
|
const tankerPrice = parsePrice(tanker.price);
|
|
|
|
}
|
|
|
|
return tankerPrice >= priceFrom && tankerPrice <= priceTo;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 3️⃣ Exclude booked tankers
|
|
|
|
let tankers = await Tanker.find(tankerQuery);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 🟢 Apply price filter only if both price_from and price_to are given
|
|
|
|
|
|
|
|
if (price_from && price_to) {
|
|
|
|
|
|
|
|
tankers = tankers.filter(tanker => {
|
|
|
|
|
|
|
|
const tankerPrice = parsePrice(tanker.price);
|
|
|
|
|
|
|
|
return tankerPrice >= priceFrom && tankerPrice <= priceTo;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 🟢 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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 4️⃣ Group tankers by supplierId
|
|
|
|
// 🟢 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]) {
|
|
|
@ -997,17 +1005,23 @@ exports.getSuppliers = async (req, reply) => {
|
|
|
|
supplierTankerMap[tanker.supplierId].push(tanker);
|
|
|
|
supplierTankerMap[tanker.supplierId].push(tanker);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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(
|
|
|
|
(sum, t) => sum + parseCapacity(t.capacity),
|
|
|
|
(sum, t) => sum + parseCapacity(t.capacity),
|
|
|
|
0
|
|
|
|
0
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if (totalAvailableCapacity >= totalRequiredCapacity) {
|
|
|
|
// 🟢 Capacity filtering: apply only if both capacity & quantity are given
|
|
|
|
qualifiedSuppliers.push({ supplierId, tankers: supplierTankers });
|
|
|
|
if (requestedCapacity > 0 && requestedQuantity > 0) {
|
|
|
|
|
|
|
|
if (totalAvailableCapacity < totalRequiredCapacity) {
|
|
|
|
|
|
|
|
continue; // skip this supplier
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
qualifiedSuppliers.push({ supplierId, tankers: supplierTankers });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const suppliers = [];
|
|
|
|
const suppliers = [];
|
|
|
|