|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
//Get the data models
|
|
|
|
|
const { Supplier, DeliveryBoy, profilePictureSupplier } = require("../models/supplier");
|
|
|
|
|
const { FriendRequest } = require("../models/supplier");
|
|
|
|
|
const { FriendRequest,RequestedBooking } = require("../models/supplier");
|
|
|
|
|
const { Tanker,Tankerbooking } = require("../models/tankers");
|
|
|
|
|
const { ProfilePicture, User } = require("../models/User");
|
|
|
|
|
const supplierController = require("../controllers/supplierController");
|
|
|
|
@ -951,7 +951,11 @@ exports.getSuppliers = async (req, reply) => {
|
|
|
|
|
date,
|
|
|
|
|
time,
|
|
|
|
|
price_from,
|
|
|
|
|
price_to
|
|
|
|
|
price_to,
|
|
|
|
|
radius_from, // even if sent we will not apply now
|
|
|
|
|
radius_to,
|
|
|
|
|
rating_from,
|
|
|
|
|
rating_to
|
|
|
|
|
} = req.body;
|
|
|
|
|
|
|
|
|
|
const parseCapacity = (value) => parseFloat((value || "0").toString().replace(/,/g, ""));
|
|
|
|
@ -973,22 +977,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
|
|
|
|
|
if (type_of_water && type_of_water.trim() !== "") {
|
|
|
|
|
tankerQuery.typeofwater = type_of_water;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let tankers = await Tanker.find(tankerQuery);
|
|
|
|
|
|
|
|
|
|
// 🟢 Apply price filter only if both price_from and price_to are given (not empty)
|
|
|
|
|
if (price_from && price_to) {
|
|
|
|
|
tankers = tankers.filter(tanker => {
|
|
|
|
|
const tankerPrice = parsePrice(tanker.price);
|
|
|
|
|
return tankerPrice >= priceFrom && tankerPrice <= priceTo;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3️⃣ Exclude booked tankers
|
|
|
|
|
// 🟢 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,19 +1009,24 @@ 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 });
|
|
|
|
|
// 🟢 Apply capacity filtering only if both are given
|
|
|
|
|
if (requestedCapacity > 0 && requestedQuantity > 0) {
|
|
|
|
|
if (totalAvailableCapacity < totalRequiredCapacity) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qualifiedSuppliers.push({ supplierId, tankers: supplierTankers });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const suppliers = [];
|
|
|
|
|
|
|
|
|
|
for (let supplierObj of qualifiedSuppliers) {
|
|
|
|
@ -1049,6 +1066,53 @@ exports.getSuppliers = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.createRequestedBooking = async (req, reply) => {
|
|
|
|
|
const {
|
|
|
|
|
customerId,
|
|
|
|
|
type_of_water,
|
|
|
|
|
capacity,
|
|
|
|
|
quantity,
|
|
|
|
|
date,
|
|
|
|
|
time,
|
|
|
|
|
requested_suppliers
|
|
|
|
|
} = req.body;
|
|
|
|
|
|
|
|
|
|
const parseCapacity = (value) => parseFloat((value || "0").toString().replace(/,/g, ""));
|
|
|
|
|
const requestedCapacity = parseCapacity(capacity);
|
|
|
|
|
const requestedQuantity = parseInt(quantity || "0");
|
|
|
|
|
const totalRequiredCapacity = requestedCapacity * requestedQuantity;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const requestedBooking = new RequestedBooking({
|
|
|
|
|
customerId,
|
|
|
|
|
type_of_water,
|
|
|
|
|
capacity,
|
|
|
|
|
quantity,
|
|
|
|
|
total_required_capacity: totalRequiredCapacity,
|
|
|
|
|
date,
|
|
|
|
|
time,
|
|
|
|
|
requested_suppliers, // ✅ already contains supplierId, quoted_amount, custom_field
|
|
|
|
|
status: "pending"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await requestedBooking.save();
|
|
|
|
|
|
|
|
|
|
reply.send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
message: "Requested booking created successfully",
|
|
|
|
|
data: requestedBooking
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
reply.send({
|
|
|
|
|
status_code: 500,
|
|
|
|
|
message: "Something went wrong while saving",
|
|
|
|
|
error: err.message
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|