From 333cc147f32cc57266cc6986cdee0f19ae110fde Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 24 Sep 2025 15:31:29 +0530 Subject: [PATCH 1/2] added split for supplier in request bookings --- src/controllers/userController.js | 64 +++++++++++++++++++++++ src/routes/usersRoute.js | 84 +++++++++++++++++++++++++++++-- 2 files changed, 145 insertions(+), 3 deletions(-) diff --git a/src/controllers/userController.js b/src/controllers/userController.js index 39a9ed9c..199b094c 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -2131,3 +2131,67 @@ exports.updateadvanceForSupplier = async (req, reply) => { throw boom.boomify(err); } }; + + +exports.splitBookingForSupplier = async (req, reply) => { + try { + const { id } = req.params; + const { capacity, quantity, date, time, quoted_amount } = req.body; + + // 1) Find existing booking + const existing = await RequestedBooking.findById(id); + if (!existing) { + return reply.code(404).send({ status_code: 404, message: "Booking not found" }); + } + + // 2) Update the original booking + existing.capacity = `${capacity} L`; + existing.quantity = "1"; + existing.total_required_capacity = capacity; + if (date) existing.date = date; + if (time) existing.time = time; + if (quoted_amount) { + existing.requested_suppliers[0].quoted_amount = quoted_amount; + } + await existing.save(); + + // 3) Create new bookings for the rest + const newBookings = []; + for (let i = 1; i < quantity; i++) { + const newBooking = new RequestedBooking({ + status: "pending", + customerId: existing.customerId, + type_of_water: existing.type_of_water, + capacity: `${capacity} L`, + quantity: "1", + total_required_capacity: capacity, + date: date || existing.date, + time: time || existing.time, + requested_suppliers: [ + { + time: new Date().toISOString().slice(0, 16).replace("T", " "), + status: "pending", + supplierId: existing.requested_suppliers[0].supplierId, + quoted_amount: quoted_amount || existing.requested_suppliers[0].quoted_amount, + }, + ], + }); + newBookings.push(newBooking); + } + + if (newBookings.length > 0) { + await RequestedBooking.insertMany(newBookings); + } + + return reply.code(200).send({ + status_code: 200, + message: `${quantity} booking(s) created/updated successfully`, + updated: existing, + newEntries: newBookings, + }); + } catch (err) { + console.error("splitBookingForSupplier error:", err); + return reply.code(500).send({ status_code: 500, message: "Internal Server Error" }); + } +}; + diff --git a/src/routes/usersRoute.js b/src/routes/usersRoute.js index d5cbfcfe..99521df2 100644 --- a/src/routes/usersRoute.js +++ b/src/routes/usersRoute.js @@ -1412,7 +1412,7 @@ fastify.route({ handler: userController.updateQuotedAmountForSupplier, }); - fastify.route({ + fastify.route({ method: 'PUT', url: '/api/request-booking-status/:_id/supplier/quotestatus', schema: { @@ -1453,7 +1453,7 @@ fastify.route({ }); - fastify.route({ + fastify.route({ method: 'PUT', url: '/api/request-advance-amount/:_id/supplier/advance', schema: { @@ -1473,7 +1473,7 @@ fastify.route({ properties: { supplierId: { type: 'string' }, advance_paid: { type: 'number' }, // ✅ fixed - advance_ref_number: { type: 'string' } + advance_ref_number: { type: 'string' } }, }, @@ -1494,5 +1494,83 @@ fastify.route({ handler: userController.updateadvanceForSupplier, }); + fastify.route({ + method: 'PUT', + url: '/api/request-advance-amount/:_id/supplier/advance', + schema: { + description: + 'Update adavance with reference number for particular supplier by user', + tags: ['User'], + summary: 'Update adavance with reference number for particular supplier by user', + params: { + type: 'object', + properties: { + _id: { type: 'string', description: 'Booking _id' }, + }, + required: ['_id'], + }, + body: { + type: 'object', + properties: { + supplierId: { type: 'string' }, + advance_paid: { type: 'number' }, // ✅ fixed + advance_ref_number: { type: 'string' } + }, + + }, + response: { + 200: { + type: 'object', + }, + 400: { type: 'object' }, + 404: { type: 'object' }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + // preHandler: fastify.auth([fastify.authenticate]), // enable if needed + handler: userController.updateadvanceForSupplier, + }); + + + fastify.route({ + method: 'POST', + url: '/api/request-advance-amount/:id/supplier/split', + schema: { + description: 'Split a booking into multiple entries based on supplier-edited capacity & quantity', + tags: ['User'], + summary: 'Split booking by capacity and quantity', + params: { + type: 'object', + properties: { + id: { type: 'string', description: 'Booking _id' }, + }, + required: ['id'], + }, + body: { + type: 'object', + properties: { + capacity: { type: 'number' }, // supplier wants capacity per booking + quantity: { type: 'number' }, // number of bookings + date: { type: 'string' }, // optional new date + time: { type: 'string' }, // optional new time + quoted_amount: { type: 'number' } // optional new quoted amount + }, + required: ['capacity', 'quantity'], + }, + response: { + 200: { type: 'object' }, + 400: { type: 'object' }, + 404: { type: 'object' }, + }, + }, + handler: userController.splitBookingForSupplier, +}); + + + next(); }; From 17fc8307b014792cfc087f19ed30a4f647706a48 Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 24 Sep 2025 15:43:14 +0530 Subject: [PATCH 2/2] changes in splits --- src/controllers/userController.js | 41 ++++++++++++++++++------------- src/routes/usersRoute.js | 27 +++++++++++++------- 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/src/controllers/userController.js b/src/controllers/userController.js index 199b094c..69a01639 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -2136,43 +2136,49 @@ exports.updateadvanceForSupplier = async (req, reply) => { exports.splitBookingForSupplier = async (req, reply) => { try { const { id } = req.params; - const { capacity, quantity, date, time, quoted_amount } = req.body; + const { splits } = req.body; - // 1) Find existing booking + if (!Array.isArray(splits) || splits.length === 0) { + return reply.code(400).send({ status_code: 400, message: "splits array is required" }); + } + + // 1) Find the existing booking const existing = await RequestedBooking.findById(id); if (!existing) { return reply.code(404).send({ status_code: 404, message: "Booking not found" }); } - // 2) Update the original booking - existing.capacity = `${capacity} L`; + // 2) Update the original booking with the first split + const first = splits[0]; + existing.capacity = `${first.capacity} L`; existing.quantity = "1"; - existing.total_required_capacity = capacity; - if (date) existing.date = date; - if (time) existing.time = time; - if (quoted_amount) { - existing.requested_suppliers[0].quoted_amount = quoted_amount; + existing.total_required_capacity = first.capacity; + if (first.date) existing.date = first.date; + if (first.time) existing.time = first.time; + if (first.quoted_amount) { + existing.requested_suppliers[0].quoted_amount = first.quoted_amount; } await existing.save(); - // 3) Create new bookings for the rest + // 3) Create new bookings for remaining splits const newBookings = []; - for (let i = 1; i < quantity; i++) { + for (let i = 1; i < splits.length; i++) { + const s = splits[i]; const newBooking = new RequestedBooking({ status: "pending", customerId: existing.customerId, type_of_water: existing.type_of_water, - capacity: `${capacity} L`, + capacity: `${s.capacity} L`, quantity: "1", - total_required_capacity: capacity, - date: date || existing.date, - time: time || existing.time, + total_required_capacity: s.capacity, + date: s.date || existing.date, + time: s.time || existing.time, requested_suppliers: [ { time: new Date().toISOString().slice(0, 16).replace("T", " "), status: "pending", supplierId: existing.requested_suppliers[0].supplierId, - quoted_amount: quoted_amount || existing.requested_suppliers[0].quoted_amount, + quoted_amount: s.quoted_amount || existing.requested_suppliers[0].quoted_amount, }, ], }); @@ -2185,7 +2191,7 @@ exports.splitBookingForSupplier = async (req, reply) => { return reply.code(200).send({ status_code: 200, - message: `${quantity} booking(s) created/updated successfully`, + message: `${splits.length} booking(s) created/updated successfully`, updated: existing, newEntries: newBookings, }); @@ -2195,3 +2201,4 @@ exports.splitBookingForSupplier = async (req, reply) => { } }; + diff --git a/src/routes/usersRoute.js b/src/routes/usersRoute.js index 99521df2..b4dc9540 100644 --- a/src/routes/usersRoute.js +++ b/src/routes/usersRoute.js @@ -1536,13 +1536,13 @@ fastify.route({ }); - fastify.route({ +fastify.route({ method: 'POST', url: '/api/request-advance-amount/:id/supplier/split', schema: { - description: 'Split a booking into multiple entries based on supplier-edited capacity & quantity', + description: 'Split a booking into multiple entries with individual capacity, date, time, and quoted_amount', tags: ['User'], - summary: 'Split booking by capacity and quantity', + summary: 'Split booking into multiple entries', params: { type: 'object', properties: { @@ -1553,13 +1553,21 @@ fastify.route({ body: { type: 'object', properties: { - capacity: { type: 'number' }, // supplier wants capacity per booking - quantity: { type: 'number' }, // number of bookings - date: { type: 'string' }, // optional new date - time: { type: 'string' }, // optional new time - quoted_amount: { type: 'number' } // optional new quoted amount + splits: { + type: 'array', + items: { + type: 'object', + properties: { + capacity: { type: 'number' }, + date: { type: 'string' }, + time: { type: 'string' }, + quoted_amount: { type: 'number' } + }, + required: ['capacity'] + } + } }, - required: ['capacity', 'quantity'], + required: ['splits'], }, response: { 200: { type: 'object' }, @@ -1570,6 +1578,7 @@ fastify.route({ handler: userController.splitBookingForSupplier, }); + next();