changes in supplier get all suppliers

master^2
Varun 4 months ago
parent 36dcb19db6
commit 6082b83af4

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

@ -52,6 +52,10 @@ module.exports = function (fastify, opts, next) {
});
fastify.get("/api/connectedSuppliers/:customerId", {
schema: {
tags: ["Supplier-Data"],

Loading…
Cancel
Save