diff --git a/src/controllers/userController.js b/src/controllers/userController.js index 9bdc703f..f7172089 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -1526,6 +1526,68 @@ exports.getuserRequestbookings = async (req, reply) => { } }; +// controllers/user.controller.js (or wherever your controllers live) + +// const Supplier = require("../models/supplier.model"); // not needed here + +/** + * GET /api/getuserRequestbookingsforsupplier/:supplierId + * Returns bookings where this supplier was requested, showing only this supplier's sub-entry. + */ +exports.getuserRequestbookingsForSupplier = async (req, reply) => { + try { + const { supplierId } = req.params; + + if (!supplierId) { + return reply.code(400).send({ + status_code: 400, + message: "supplierId is required", + }); + } + + // 1) Find all bookings that include this supplier + const bookings = await RequestedBooking.find({ + "requested_suppliers.supplierId": supplierId, + }) + .sort({ createdAt: -1 }) + .lean(); + + // 2) For each booking, expose only this supplier's subdocument + const data = bookings.map((b) => { + const mySupplierEntry = (b.requested_suppliers || []).find( + (s) => s.supplierId === supplierId + ) || null; + + return { + _id: b._id, + customerId: b.customerId, + type_of_water: b.type_of_water, + capacity: b.capacity, + quantity: b.quantity, + total_required_capacity: b.total_required_capacity, + date: b.date, + time: b.time, + // booking-wide status (e.g., pending/confirmed/cancelled) + booking_status: b.status, + createdAt: b.createdAt, + updatedAt: b.updatedAt, + + // only the supplier's own requested_suppliers entry + my_supplier_entry: mySupplierEntry, // { supplierId, quoted_amount, time, status } + }; + }); + + return reply.send({ + status_code: 200, + message: `Orders for supplier ${supplierId} fetched successfully`, + data, + }); + } catch (err) { + console.error(err); + throw boom.boomify(err); + } +}; + const mongoose = require('mongoose'); diff --git a/src/routes/usersRoute.js b/src/routes/usersRoute.js index d36d8dc3..5545d45a 100644 --- a/src/routes/usersRoute.js +++ b/src/routes/usersRoute.js @@ -1179,6 +1179,28 @@ fastify.route({ handler: userController.getuserRequestbookings, }); +fastify.route({ + method: "GET", + url: "/api/getuserRequestbookingsforsupplier/:supplierId", + schema: { + description: "Get request bookings for a particular supplier", + tags: ["Supplier"], + summary: "Fetch bookings where the supplier is requested", + params: { + type: "object", + properties: { + supplierId: { type: "string", description: "Supplier ID" }, + }, + required: ["supplierId"], + }, + security: [{ basicAuth: [] }], + }, + // preHandler: fastify.auth([fastify.authenticate]), + handler: userController.getuserRequestbookingsForSupplier, +}); + + + fastify.route({ method: "POST", url: "/api/booking/accept/:supplierId",