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}`) 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 = [];

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

Loading…
Cancel
Save