diff --git a/src/controllers/userController.js b/src/controllers/userController.js index 0af39307..18c9422f 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -1937,3 +1937,63 @@ exports.updatePaymentForBooking = async (req, reply) => { throw boom.boomify(err); } }; + + +// controllers/recurringRequestedBookingController.js + +exports.updateQuotedAmountForSupplier = async (req, reply) => { + try { + const { _id } = req.params; + const { supplierId, amount } = req.body; + + if (!_id) { + return reply.code(400).send({ status_code: 400, message: '_id (param) is required' }); + } + if (!supplierId) { + return reply.code(400).send({ status_code: 400, message: 'supplierId (body) is required' }); + } + if (typeof amount === 'undefined' || amount === null || amount === '') { + return reply.code(400).send({ status_code: 400, message: 'amount (body) is required' }); + } + + // convert amount to number if possible + const numericAmount = Number(amount); + if (Number.isNaN(numericAmount)) { + return reply.code(400).send({ status_code: 400, message: 'amount must be a valid number' }); + } + + // Atomic update using positional $ operator + const filter = { _id, 'requested_suppliers.supplierId': supplierId }; + const update = { $set: { 'requested_suppliers.$.quoted_amount': numericAmount } }; + + const updated = await RequestedBooking.findOneAndUpdate(filter, update, { + new: true, + runValidators: true, + }).lean(); + + if (!updated) { + // either booking _id not found OR supplierId not found inside requested_suppliers + // let's check which one + const bookingExists = await RequestedBooking.findById(_id).lean(); + if (!bookingExists) { + return reply.code(404).send({ status_code: 404, message: `Booking with _id ${_id} not found` }); + } + + // booking exists but supplier entry missing + return reply.code(404).send({ + status_code: 404, + message: `Supplier ${supplierId} not found in requested_suppliers for booking ${_id}`, + }); + } + + return reply.code(200).send({ + status_code: 200, + message: `quoted_amount updated for supplier ${supplierId}`, + data: updated, + }); + } catch (err) { + console.error('updateQuotedAmountForSupplier error:', err); + throw boom.boomify(err); + } +}; + diff --git a/src/routes/usersRoute.js b/src/routes/usersRoute.js index a4cfbd62..84039bb8 100644 --- a/src/routes/usersRoute.js +++ b/src/routes/usersRoute.js @@ -1371,6 +1371,46 @@ fastify.route({ handler: userController.updatePaymentForBooking }); + fastify.route({ + method: 'PUT', + url: '/api/request-booking/:_id/supplier/quote', + schema: { + description: + 'Update quoted_amount for a supplier inside requested_suppliers for a requested booking', + tags: ['User'], + summary: 'Update supplier quoted amount by User', + params: { + type: 'object', + properties: { + _id: { type: 'string', description: 'Booking _id' }, + }, + required: ['_id'], + }, + body: { + type: 'object', + properties: { + supplierId: { type: 'string', description: 'Supplier ID' }, + amount: { type: ['number', 'string'], description: 'Quoted amount (number)' }, + }, + required: ['supplierId', 'amount'], + additionalProperties: false, + }, + response: { + 200: { + type: 'object', + }, + 400: { type: 'object' }, + 404: { type: 'object' }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + // preHandler: fastify.auth([fastify.authenticate]), // enable if needed + handler: userController.updateQuotedAmountForSupplier, + }); next(); };