|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
//Get the data models
|
|
|
|
|
const { Supplier, DeliveryBoy, profilePictureSupplier } = require("../models/supplier");
|
|
|
|
|
const { FriendRequest } = require("../models/supplier");
|
|
|
|
|
const { Tanker,Tankerbooking } = require("../models/tankers");
|
|
|
|
|
const { ProfilePicture, User } = require("../models/User");
|
|
|
|
|
const supplierController = require("../controllers/supplierController");
|
|
|
|
|
const customJwtAuth = require("../customAuthJwt");
|
|
|
|
@ -938,44 +939,104 @@ exports.getCurrentSupplier = async (req, reply) => {
|
|
|
|
|
// }
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
exports.getSuppliers = async (req, reply) => {
|
|
|
|
|
const customerId = req.params.customerId;
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
type_of_water,
|
|
|
|
|
capacity: requestedCapacityStr,
|
|
|
|
|
quantity: requestedQuantityStr,
|
|
|
|
|
date
|
|
|
|
|
} = req.body;
|
|
|
|
|
|
|
|
|
|
const parseCapacity = (value) => parseFloat((value || "0").toString().replace(/,/g, ""));
|
|
|
|
|
|
|
|
|
|
const requestedCapacity = parseCapacity(requestedCapacityStr);
|
|
|
|
|
const requestedQuantity = parseInt(requestedQuantityStr || "0");
|
|
|
|
|
const totalRequiredCapacity = requestedCapacity * requestedQuantity;
|
|
|
|
|
|
|
|
|
|
exports.getSuppliers = async (req, reply) => {
|
|
|
|
|
const limit = parseInt(req.query.limit) || 100;
|
|
|
|
|
const page = parseInt(req.query.page) || 1;
|
|
|
|
|
const startindex = (page - 1) * limit;
|
|
|
|
|
const customerId = req.params.customerId; // Assuming you have already authenticated the user and stored their ID in the request object
|
|
|
|
|
try {
|
|
|
|
|
const friendRequests = await FriendRequest.find({ customerId });
|
|
|
|
|
const supplierIdsToExclude = friendRequests.map(
|
|
|
|
|
(request) => request.supplierId
|
|
|
|
|
const tankerBookings = await Tankerbooking.find({ date });
|
|
|
|
|
const bookedTankerSet = new Set(
|
|
|
|
|
tankerBookings.map(booking => `${booking.supplierId}_${booking.tankerName}`)
|
|
|
|
|
);
|
|
|
|
|
const suppliers = await Supplier.find({ supplierId: { $nin: supplierIdsToExclude } })
|
|
|
|
|
.limit(limit)
|
|
|
|
|
.skip(startindex)
|
|
|
|
|
.exec();
|
|
|
|
|
|
|
|
|
|
const supplierIds = suppliers.map((supplier) => supplier.supplierId);
|
|
|
|
|
const profilePictures = await profilePictureSupplier.find({
|
|
|
|
|
supplierId: { $in: supplierIds },
|
|
|
|
|
}).exec();
|
|
|
|
|
// 1️⃣ Only filter typeofwater first
|
|
|
|
|
let tankers = await Tanker.find({ typeofwater: type_of_water });
|
|
|
|
|
|
|
|
|
|
const data = suppliers.map((supplier) => {
|
|
|
|
|
const profilePicture = profilePictures.find(
|
|
|
|
|
(picture) => picture.supplierId === supplier.supplierId
|
|
|
|
|
// 2️⃣ Exclude booked tankers
|
|
|
|
|
tankers = tankers.filter(tanker => {
|
|
|
|
|
const key = `${tanker.supplierId}_${tanker.tankerName}`;
|
|
|
|
|
return !bookedTankerSet.has(key);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 3️⃣ Group by supplierId
|
|
|
|
|
const supplierTankerMap = {};
|
|
|
|
|
for (let tanker of tankers) {
|
|
|
|
|
if (!supplierTankerMap[tanker.supplierId]) {
|
|
|
|
|
supplierTankerMap[tanker.supplierId] = [];
|
|
|
|
|
}
|
|
|
|
|
supplierTankerMap[tanker.supplierId].push(tanker);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4️⃣ Aggregate supplier capacities
|
|
|
|
|
const qualifiedSuppliers = [];
|
|
|
|
|
for (let [supplierId, supplierTankers] of Object.entries(supplierTankerMap)) {
|
|
|
|
|
const totalAvailableCapacity = supplierTankers.reduce(
|
|
|
|
|
(sum, t) => sum + parseCapacity(t.capacity),
|
|
|
|
|
0
|
|
|
|
|
);
|
|
|
|
|
return {
|
|
|
|
|
...supplier.toObject(),
|
|
|
|
|
picture: profilePicture ? profilePicture.picture : null,
|
|
|
|
|
|
|
|
|
|
if (totalAvailableCapacity >= totalRequiredCapacity) {
|
|
|
|
|
qualifiedSuppliers.push({ supplierId, tankers: supplierTankers });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const connected_suppliers = [];
|
|
|
|
|
const non_connected_suppliers = [];
|
|
|
|
|
|
|
|
|
|
for (let supplierObj of qualifiedSuppliers) {
|
|
|
|
|
const supplierData = await Supplier.findOne({ supplierId: supplierObj.supplierId });
|
|
|
|
|
|
|
|
|
|
const responseData = {
|
|
|
|
|
supplier: supplierData,
|
|
|
|
|
tankers: supplierObj.tankers
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 🔥 The correct friend request check:
|
|
|
|
|
const friendRequest = await FriendRequest.findOne({
|
|
|
|
|
customerId: customerId,
|
|
|
|
|
supplierId: supplierObj.supplierId
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (friendRequest && friendRequest.status === "accepted") {
|
|
|
|
|
connected_suppliers.push(responseData);
|
|
|
|
|
} else {
|
|
|
|
|
non_connected_suppliers.push(responseData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reply.send({
|
|
|
|
|
status_code: 200,
|
|
|
|
|
connected_suppliers,
|
|
|
|
|
non_connected_suppliers
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
reply.send({ status_code: 200, data, count: data.length });
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw boom.boomify(err);
|
|
|
|
|
console.error(err);
|
|
|
|
|
reply.send({
|
|
|
|
|
status_code: 500,
|
|
|
|
|
message: "Something went wrong",
|
|
|
|
|
error: err.message
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Get single user by ID
|
|
|
|
|
exports.getSingleSupplier = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|