diff --git a/src/handlers/supplierHandler.js b/src/handlers/supplierHandler.js index f4f51811..81f362f6 100644 --- a/src/handlers/supplierHandler.js +++ b/src/handlers/supplierHandler.js @@ -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) => { diff --git a/src/models/supplier.js b/src/models/supplier.js index 4bc4e3cf..24507c1b 100644 --- a/src/models/supplier.js +++ b/src/models/supplier.js @@ -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 }, diff --git a/src/routes/supplierRoute.js b/src/routes/supplierRoute.js index 5daec94f..f522c8f9 100644 --- a/src/routes/supplierRoute.js +++ b/src/routes/supplierRoute.js @@ -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"],