changes in get suppliers

master^2
Varun 4 months ago
parent 6082b83af4
commit 8974dc6aa7

@ -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, ""));
@ -982,7 +986,7 @@ exports.getSuppliers = async (req, reply) => {
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) {
tankers = tankers.filter(tanker => {
const tankerPrice = parsePrice(tanker.price);
@ -1008,16 +1012,15 @@ exports.getSuppliers = async (req, reply) => {
const qualifiedSuppliers = [];
for (let [supplierId, supplierTankers] of Object.entries(supplierTankerMap)) {
const totalAvailableCapacity = supplierTankers.reduce(
(sum, t) => sum + parseCapacity(t.capacity),
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 (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
});
}
}

@ -155,12 +155,34 @@ const supplierSchema = new mongoose.Schema(
});
const requestedSupplierSchema = new mongoose.Schema({
supplierId: String,
quoted_amount: Number,
custom_field: String // ✅ New field added here
}, { _id: false });
const requestedBookingSchema = new mongoose.Schema({
customerId: { type: String, required: true },
type_of_water: String,
capacity: String,
quantity: String,
total_required_capacity: Number,
date: String,
time: String,
requested_suppliers: [requestedSupplierSchema],
status: { type: String, default: "pending" },
}, { timestamps: true });
const RequestedBooking = mongoose.model('RequestedBooking', requestedBookingSchema);
const Supplier = mongoose.model("Supplier", supplierSchema);
//const DeliveryAgent = mongoose.model("DeliveryAgent", deliveryAgent);
const FriendRequest = mongoose.model('FriendRequest', friendRequestSchema);
const DeliveryBoy = mongoose.model('DeliveryBoy', deliveryBoySchema);
const profilePictureSupplier = mongoose.model('ProfilePictureSupplier', profilePictureSupplierSchema);
module.exports = { Supplier, generateSupplierId, FriendRequest,DeliveryBoy, profilePictureSupplier}
module.exports = { Supplier, generateSupplierId, FriendRequest,DeliveryBoy, profilePictureSupplier,RequestedBooking}

@ -52,6 +52,39 @@ module.exports = function (fastify, opts, next) {
});
fastify.post("/api/requestedbookings", {
schema: {
tags: ["Supplier-Data"],
description: "API to create requested bookings and send to suppliers",
summary: "Create requested booking",
body: {
type: "object",
required: ["customerId", "type_of_water", "capacity", "quantity", "date", "time", "requested_suppliers"],
properties: {
customerId: { type: "string" },
type_of_water: { type: "string" },
capacity: { type: "string" },
quantity: { type: "string" },
date: { type: "string" },
time: { type: "string" },
requested_suppliers: {
type: "array",
items: {
type: "object",
required: ["supplierId", "quoted_amount", "custom_field"],
properties: {
supplierId: { type: "string" },
quoted_amount: { type: "number" },
custom_field: { type: "string" } // ✅ New field
}
}
}
}
},
security: [{ basicAuth: [] }]
},
handler: validationHandler.createRequestedBooking
});

Loading…
Cancel
Save