added accepted and rejected request bookings fror user and get and respoond request bookings for supplier

master^2
Varun 3 months ago
parent 109bab2e2c
commit 6a7d43a31b

@ -249,77 +249,50 @@ exports.loginDeliveryBoy = async (req) => {
};
exports.acceptRequestedBooking = async (req, reply) => {
const { supplierId } = req.params;
const { action, _id } = req.body;
if (action !== 'accept') {
return reply.code(400).send({ message: "Invalid action. Only 'accept' is allowed." });
}
const mongoose = require('mongoose');
exports.respondToRequestedBooking = async (req, reply) => {
const { _id } = req.params;
const { action, supplierId } = req.body;
try {
const requestedBooking = await RequestedBooking.findOne({
_id,
'requested_suppliers.supplierId': supplierId,
status: 'pending'
});
if (!mongoose.Types.ObjectId.isValid(_id)) {
return reply.code(400).send({ message: "Invalid requested booking ID" });
}
if (!requestedBooking) {
return reply.code(404).send({ message: "No matching pending request for given ID and supplier" });
if (!["accept", "reject"].includes(action)) {
return reply.code(400).send({ message: "Action must be 'accept' or 'reject'" });
}
requestedBooking.status = 'accepted';
await requestedBooking.save();
try {
const booking = await RequestedBooking.findById(_id);
const customer = await User.findOne({ customerId: requestedBooking.customerId }).lean();
if (!customer) return reply.code(404).send({ message: "Customer not found" });
if (!booking) {
return reply.code(404).send({ message: "Requested booking not found" });
}
const supplier = await Supplier.findOne({ supplierId }).lean();
if (!supplier) return reply.code(404).send({ message: "Supplier not found" });
const supplierEntry = booking.requested_suppliers.find(s => s.supplierId === supplierId);
const matchedSupplier = requestedBooking.requested_suppliers.find(s => s.supplierId === supplierId);
if (!matchedSupplier || !matchedSupplier.quoted_amount) {
return reply.code(400).send({ message: "Quoted amount missing for this supplier" });
if (!supplierEntry) {
return reply.code(404).send({ message: "Supplier not found in this booking" });
}
const newBooking = new Tankerbooking({
customerId: customer.customerId,
customerName: customer.profile.firstName,
customerPhone: customer.phone,
address: customer.address1,
latitude: customer.latitude,
longitude: customer.longitude,
supplierId: supplier.supplierId,
supplierName: supplier.suppliername,
supplierPhone: supplier.phone,
supplierAddress: customer.address,
type_of_water: requestedBooking.type_of_water,
capacity: requestedBooking.capacity,
quantity: requestedBooking.quantity,
total_required_capacity: requestedBooking.total_required_capacity,
expectedDateOfDelivery: requestedBooking.date,
time: requestedBooking.time,
price: matchedSupplier.quoted_amount,
status: 'pending'
});
// Update custom_field (status) for that supplier
supplierEntry.status = action === "accept" ? "accepted_by_supplier" : "rejected_by_supplier";
await newBooking.save();
await booking.save();
reply.code(200).send({
return reply.code(200).send({
status_code: 200,
message: "Booking accepted and moved to tanker bookings",
data: newBooking
message: `Booking ${action}ed by supplier successfully`,
data: booking
});
} catch (err) {
console.error(err);
throw boom.internal("Failed to accept booking", err);
throw boom.internal("Failed to update supplier response", err);
}
};
};

@ -1313,3 +1313,92 @@ exports.getuserRequestbookings = async (req, reply) => {
throw boom.boomify(err);
}
};
exports.acceptRequestedBooking = async (req, reply) => {
const { supplierId } = req.params;
const { action, _id } = req.body;
if (!["accept", "reject"].includes(action)) {
return reply.code(400).send({ message: "Invalid action. Must be 'accept' or 'reject'." });
}
try {
const requestedBooking = await RequestedBooking.findOne({
_id: new mongoose.Types.ObjectId(_id),
'requested_suppliers.supplierId': supplierId
});
if (!requestedBooking) {
return reply.code(404).send({ message: "No matching request for given ID and supplier" });
}
const matchedSupplier = requestedBooking.requested_suppliers.find(s => s.supplierId === supplierId);
if (!matchedSupplier) {
return reply.code(404).send({ message: "Supplier not found in requested_suppliers array" });
}
if (action === "reject") {
matchedSupplier.status = "rejected_by_user";
await requestedBooking.save();
return reply.code(200).send({
status_code: 200,
message: "Supplier request rejected by user",
data: requestedBooking
});
}
// Accept path
requestedBooking.status = 'accepted';
await requestedBooking.save();
const customer = await User.findOne({ customerId: requestedBooking.customerId }).lean();
if (!customer) return reply.code(404).send({ message: "Customer not found" });
const supplier = await Supplier.findOne({ supplierId }).lean();
if (!supplier) return reply.code(404).send({ message: "Supplier not found" });
if (!matchedSupplier.quoted_amount) {
return reply.code(400).send({ message: "Quoted amount missing for this supplier" });
}
const newBooking = new Tankerbooking({
customerId: customer.customerId,
customerName: customer.profile.firstName,
customerPhone: customer.phone,
address: customer.address1,
latitude: customer.latitude,
longitude: customer.longitude,
supplierId: supplier.supplierId,
supplierName: supplier.suppliername,
supplierPhone: supplier.phone,
supplierAddress: customer.address,
type_of_water: requestedBooking.type_of_water,
capacity: requestedBooking.capacity,
quantity: requestedBooking.quantity,
total_required_capacity: requestedBooking.total_required_capacity,
expectedDateOfDelivery: requestedBooking.date,
time: requestedBooking.time,
price: matchedSupplier.quoted_amount,
status: 'pending'
});
await newBooking.save();
reply.code(200).send({
status_code: 200,
message: "Booking accepted and moved to tanker bookings",
data: newBooking
});
} catch (err) {
console.error(err);
throw boom.internal("Failed to handle booking action", err);
}
};

@ -159,7 +159,8 @@ const supplierSchema = new mongoose.Schema(
const requestedSupplierSchema = new mongoose.Schema({
supplierId: String,
quoted_amount: Number,
custom_field: String // ✅ New field added here
time: {type:String,default:null}, // ✅ New field added here
status:{type:String,default: "pending" },
}, { _id: false });
const requestedBookingSchema = new mongoose.Schema({

@ -597,27 +597,33 @@ fastify.post("/api/requestedbookings", {
handler: supplierController.editCuurentSupplierInfo,
});
fastify.route({
method: "POST",
url: "/api/booking/accept/:supplierId",
url: "/api/supplier/booking/respond/:_id",
schema: {
description: "Accept a requested booking by supplier",
description: "Supplier accepts or rejects a requested booking",
tags: ["Supplier-Data"],
summary: "Accept booking and move to tanker bookings",
summary: "Supplier action on requested booking",
params: {
type: "object",
properties: {
supplierId: { type: "string", description: "Supplier ID" }
_id: { type: "string", description: "Requested Booking ID" }
},
required: ["supplierId"]
required: ["_id"]
},
body: {
type: "object",
properties: {
_id: { type: "string", description: "Requested booking ID" },
action: { type: "string", enum: ["accept"], description: "Action to perform" }
supplierId: { type: "string", description: "Supplier ID" },
action: {
type: "string",
enum: ["accept", "reject"],
description: "Action to perform by supplier"
}
},
required: ["_id", "action"]
required: ["supplierId", "action"]
},
security: [
{
@ -625,9 +631,12 @@ fastify.route({
},
],
},
//preHandler: fastify.auth([fastify.authenticate]),
handler: supplierController.acceptRequestedBooking
// preHandler: fastify.auth([fastify.authenticate]), // Uncomment if auth is needed
handler: supplierController.respondToRequestedBooking
});
next();
}

@ -1179,5 +1179,39 @@ fastify.route({
handler: userController.getuserRequestbookings,
});
fastify.route({
method: "POST",
url: "/api/booking/accept/:supplierId",
schema: {
description: "Accept a requested booking by supplier",
tags: ["Supplier-Data"],
summary: "Accept booking and move to tanker bookings",
params: {
type: "object",
properties: {
supplierId: { type: "string", description: "Supplier ID" }
},
required: ["supplierId"]
},
body: {
type: "object",
properties: {
_id: { type: "string", description: "Requested booking ID" },
action: { type: "string", enum: ["accept","reject"], description: "Action to perform" }
},
required: ["_id", "action"]
},
security: [
{
basicAuth: [],
},
],
},
//preHandler: fastify.auth([fastify.authenticate]),
handler: userController.acceptRequestedBooking
});
next();
};

Loading…
Cancel
Save