added get orderts for customers and given api for supplier accepting the orders

master^2
Varun 3 months ago
parent 3ea20ea044
commit 7cb64748c1

@ -10,6 +10,7 @@ const fastify = require("fastify")({
return uuidv4();
},
});
const { Tankerbooking} = require("../models/tankers")
const {Repairorder,SensorStock,Order,EstimationOrder,Iotprice, Install, ProfilePictureInstall, SensorQuotation,generateinstallationId,Store,WaterLeverSensor,MotorSwitchSenso,Insensors,generatequatationId, HardwareCart, ServiceCart, Sales} = require("../models/store");
const { User,Counter, generateBookingId,resetCounter,generateCustomerId,ProfilePicture} = require('../models/User')
@ -2824,4 +2825,8 @@ exports.replaceAndRepair = async (req, reply) => {
console.error("❌ Error during replacement:", err);
return reply.code(500).send({ error: "Internal Server Error" });
}
};
};

@ -10,9 +10,12 @@ const saltRounds = 10;
//Get the data models
const { Supplier ,ProfilePicture, generateSupplierId, DeliveryBoy} = require('../models/supplier');
const { RequestedBooking,Supplier , generateSupplierId, DeliveryBoy} = require('../models/supplier');
const { Tankerbooking} = require("../models/tankers")
// Get Data Models
const { User,Counter, generateBookingId,resetCounter,generateCustomerId,ProfilePicture, AddTeamMembers,Cart} = require('../models/User')
async function bcryptPassword(password) {
encryptedPwd = bcrypt.hash(password, saltRounds);
return encryptedPwd;
@ -244,4 +247,79 @@ exports.loginDeliveryBoy = async (req) => {
throw boom.boomify(err);
}
};
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." });
}
try {
const requestedBooking = await RequestedBooking.findOne({
_id,
'requested_suppliers.supplierId': supplierId,
status: 'pending'
});
if (!requestedBooking) {
return reply.code(404).send({ message: "No matching pending request for given ID and supplier" });
}
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" });
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" });
}
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 accept booking", err);
}
};

@ -9,9 +9,10 @@ const libphonenumberjs = require("libphonenumber-js");
// External Dependancies
// offers http-friendly error objects.
const boom = require("boom");
const { Tankerbooking} = require("../models/tankers")
// Get Data Models
const { Supplier, generateSupplierId, FriendRequest,DeliveryBoy} = require("../models/supplier")
const { RequestedBooking,Supplier, generateSupplierId, FriendRequest,DeliveryBoy} = require("../models/supplier")
const { User,Counter, generateBookingId,resetCounter,generateCustomerId,ProfilePicture, AddTeamMembers,Cart} = require('../models/User')
//const User = require("../models/User");
@ -1272,3 +1273,27 @@ exports.clearCart = async (req, reply) => {
reply.status(500).send({ error: "Internal server error" });
}
};
exports.getuserOrders = async (req, reply) => {
try {
const { customerId } = req.params;
const orders = await Tankerbooking.find({ customerId }).sort({ createdAt: -1 }).lean();
return reply.send({
status_code: 200,
message: `Orders for customer ${customerId} fetched successfully`,
data: orders
});
} catch (err) {
throw boom.boomify(err);
}
};

@ -2082,5 +2082,11 @@ fastify.post("/api/repair/replace/:customerId", {
});
next();
};

@ -597,7 +597,37 @@ fastify.post("/api/requestedbookings", {
handler: supplierController.editCuurentSupplierInfo,
});
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"], description: "Action to perform" }
},
required: ["_id", "action"]
},
security: [
{
basicAuth: [],
},
],
},
//preHandler: fastify.auth([fastify.authenticate]),
handler: supplierController.acceptRequestedBooking
});
next();
}

@ -1123,5 +1123,32 @@ fastify.get("/api/cart/:customerId", {
fastify.route({
method: "POST",
url: "/api/getuserOrders/:customerId",
schema: {
description: "To Get orders of customer",
tags: ["User"],
summary: "This is for Geting orders of customer",
params: {
type: "object",
properties: {
customerId: {
type: "string",
description: "customerId",
},
},
},
security: [
{
basicAuth: [],
},
],
},
//preHandler: fastify.auth([fastify.authenticate]),
handler: userController.getuserOrders,
});
next();
};

Loading…
Cancel
Save