|
|
@ -1,6 +1,6 @@
|
|
|
|
//Get the data models
|
|
|
|
//Get the data models
|
|
|
|
const { Supplier, DeliveryBoy, profilePictureSupplier } = require("../models/supplier");
|
|
|
|
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 { Tanker,Tankerbooking } = require("../models/tankers");
|
|
|
|
const { ProfilePicture, User } = require("../models/User");
|
|
|
|
const { ProfilePicture, User } = require("../models/User");
|
|
|
|
const supplierController = require("../controllers/supplierController");
|
|
|
|
const supplierController = require("../controllers/supplierController");
|
|
|
@ -951,7 +951,11 @@ exports.getSuppliers = async (req, reply) => {
|
|
|
|
date,
|
|
|
|
date,
|
|
|
|
time,
|
|
|
|
time,
|
|
|
|
price_from,
|
|
|
|
price_from,
|
|
|
|
price_to
|
|
|
|
price_to,
|
|
|
|
|
|
|
|
radius_from, // even if sent we will not apply now
|
|
|
|
|
|
|
|
radius_to,
|
|
|
|
|
|
|
|
rating_from,
|
|
|
|
|
|
|
|
rating_to
|
|
|
|
} = req.body;
|
|
|
|
} = req.body;
|
|
|
|
|
|
|
|
|
|
|
|
const parseCapacity = (value) => parseFloat((value || "0").toString().replace(/,/g, ""));
|
|
|
|
const parseCapacity = (value) => parseFloat((value || "0").toString().replace(/,/g, ""));
|
|
|
@ -982,7 +986,7 @@ exports.getSuppliers = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
let tankers = await Tanker.find(tankerQuery);
|
|
|
|
let tankers = await Tanker.find(tankerQuery);
|
|
|
|
|
|
|
|
|
|
|
|
// 🟢 Apply price filter only if both price_from and price_to are given
|
|
|
|
// 🟢 Apply price filter only if both price_from and price_to are given (not empty)
|
|
|
|
if (price_from && price_to) {
|
|
|
|
if (price_from && price_to) {
|
|
|
|
tankers = tankers.filter(tanker => {
|
|
|
|
tankers = tankers.filter(tanker => {
|
|
|
|
const tankerPrice = parsePrice(tanker.price);
|
|
|
|
const tankerPrice = parsePrice(tanker.price);
|
|
|
@ -1008,16 +1012,15 @@ exports.getSuppliers = async (req, reply) => {
|
|
|
|
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
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// 🟢 Capacity filtering: apply only if both capacity & quantity are given
|
|
|
|
// 🟢 Apply capacity filtering only if both are given
|
|
|
|
if (requestedCapacity > 0 && requestedQuantity > 0) {
|
|
|
|
if (requestedCapacity > 0 && requestedQuantity > 0) {
|
|
|
|
if (totalAvailableCapacity < totalRequiredCapacity) {
|
|
|
|
if (totalAvailableCapacity < totalRequiredCapacity) {
|
|
|
|
continue; // skip this supplier
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -1063,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
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|