Bhaskar 1 month ago
commit b893cbf9bf

@ -1080,6 +1080,46 @@ if (isValidPrice(price_from) && isValidPrice(price_to)) {
};
// GET SUPPLIERS (simple): only needs customerId; no tanker checks
exports.getSupplierswithoutbooking = async (req, reply) => {
try {
const { customerId } = req.params;
// 1) Load customer to read favorites
const customer = await User.findOne({ customerId }, { favorate_suppliers: 1, _id: 0 });
if (!customer) {
return reply.code(404).send({ status_code: 404, message: "Customer not found" });
}
const favoriteSet = new Set(customer.favorate_suppliers || []);
// 2) Load all suppliers
const suppliers = await Supplier.find({}); // add projection if you want to slim payload
// 3) Find accepted connections for this customer across ALL suppliers in one go
const supplierIds = suppliers.map(s => s.supplierId).filter(Boolean);
const acceptedReqs = await FriendRequest.find(
{ customerId, supplierId: { $in: supplierIds }, status: "accepted" },
{ supplierId: 1, _id: 0 }
);
const connectedSet = new Set(acceptedReqs.map(r => r.supplierId));
// 4) Build response
const result = suppliers.map(s => ({
supplier: s,
isFavorite: favoriteSet.has(s.supplierId),
isConnected: connectedSet.has(s.supplierId),
}));
return reply.send({ status_code: 200, suppliers: result });
} catch (err) {
console.error(err);
return reply.code(500).send({
status_code: 500,
message: "Something went wrong",
error: err.message,
});
}
};
exports.createRequestedBooking = async (req, reply) => {

@ -35,6 +35,7 @@ const supplierSchema = new mongoose.Schema(
services: { password: { bcrypt: String } },
description: {type : String, default: null},
startingPrice : { type : String, default: 0.0},
status: { type : String, default:"under_verification"},
profile: {
role: [{ type: String, default: "supplier" }],
firstName: { type: String, default: null },

@ -52,6 +52,26 @@ module.exports = function (fastify, opts, next) {
});
fastify.post("/api/getSupplierswithoutbooking/:customerId", {
schema: {
tags: ["Supplier-Data"],
description: "Get all suppliers with favorite & connection flags for a customer",
summary: "List suppliers with isFavorite & isConnected",
params: {
type: "object",
required: ["customerId"],
properties: {
customerId: { type: "string", description: "Customer ID" },
},
},
// Body is not needed; keep empty schema or remove `body` entirely
security: [{ basicAuth: [] }],
},
handler: validationHandler.getSupplierswithoutbooking, // or `exports.getSuppliers` if wired directly
});
fastify.post("/api/requestedbookings", {
schema: {
tags: ["Supplier-Data"],

Loading…
Cancel
Save