diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 549d6081..eac3af05 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -1467,7 +1467,7 @@ exports.masterConnectedSlaveList = async (req, reply) => { { hardwareId: connectedTo }, { tankLocation: 1, typeOfWater: 1, height: 1, length: 1, width: 1 } ).lean(); - + console.log("tankDetails",tankDetails) const masterTypeOfWater = tankDetails?.typeOfWater || null; // Step 3: Get slave devices @@ -1535,39 +1535,87 @@ exports.masterConnectedSlaveList = async (req, reply) => { }; // Step 7: Process slaves - const processedSlaves = await Promise.all(slaveTanks.map(async slave => { - const finalHardwareId = slave.tankhardwareId || slave.connected_to; - - let slaveTankMeta = await Tank.findOne( - { tankhardwareId: slave.tankhardwareId, customerId }, - { height: 1, length: 1, width: 1 } - ).lean(); - - if (!slaveTankMeta && slave.connected_to) { - slaveTankMeta = await Tank.findOne( - { hardwareId: slave.connected_to, customerId }, - { height: 1, length: 1, width: 1 } - ).lean(); - } + // const processedSlaves = await Promise.all(slaveTanks.map(async slave => { + // const finalHardwareId = slave.tankhardwareId || slave.connected_to; + + // let slaveTankMeta = await Tank.findOne( + // { tankhardwareId: slave.tankhardwareId, customerId }, + // { height: 1, length: 1, width: 1,tankName:1 } + // ).lean(); + // console.log(slaveTankMeta,"slaveTankMeta") + // if (!slaveTankMeta && slave.connected_to) { + // slaveTankMeta = await Tank.findOne( + // { hardwareId: slave.connected_to, customerId }, + // { height: 1, length: 1, width: 1 } + // ).lean(); + // } + + // const matchingTankData = latestIotData?.tanks?.find(t => + // t.tankHardwareId === finalHardwareId || t.connected_to === finalHardwareId + // ); + + // return { + // ...slave, + // isMaster: false, + // product_status: slave.product_status || 'pending', + // tankHeight: matchingTankData?.tankHeight ?? null, + // typeOfWater: masterTypeOfWater === 'bore' ? 'bore' : (slave.typeOfWater || null), + // height: slaveTankMeta?.height || null, + // length: slaveTankMeta?.length || null, + // width: slaveTankMeta?.width || null, + // heightInCm: slaveTankMeta?.height ? feetToCm(slaveTankMeta.height) : null, + // lengthInCm: slaveTankMeta?.length ? feetToCm(slaveTankMeta.length) : null, + // widthInCm: slaveTankMeta?.width ? feetToCm(slaveTankMeta.width) : null + // }; + // })); +// Step 7: Process slaves +const processedSlaves = await Promise.all(slaveTanks.map(async (slave) => { + const finalHardwareId = slave.tankhardwareId || slave.connected_to; + + // Fetch slave tank metadata from Tank schema using tankhardwareId or tankName + customerId + let slaveTankMeta = await Tank.findOne( + { tankhardwareId: slave.tankhardwareId, customerId }, + { height: 1, length: 1, width: 1, typeOfWater: 1, tankName: 1 } + ).lean(); + + if (!slaveTankMeta && slave.tankName) { + // fallback: use tankName + customerId + slaveTankMeta = await Tank.findOne( + { tankName: slave.tankName, customerId }, + { height: 1, length: 1, width: 1, typeOfWater: 1, tankName: 1 } + ).lean(); + } - const matchingTankData = latestIotData?.tanks?.find(t => - t.tankHardwareId === finalHardwareId || t.connected_to === finalHardwareId - ); + if (!slaveTankMeta && slave.connected_to) { + // fallback: use connected_to as hardwareId + slaveTankMeta = await Tank.findOne( + { hardwareId: slave.connected_to, customerId }, + { height: 1, length: 1, width: 1, typeOfWater: 1, tankName: 1 } + ).lean(); + } - return { - ...slave, - isMaster: false, - product_status: slave.product_status || 'pending', - tankHeight: matchingTankData?.tankHeight ?? null, - typeOfWater: masterTypeOfWater === 'bore' ? 'bore' : (slave.typeOfWater || null), - height: slaveTankMeta?.height || null, - length: slaveTankMeta?.length || null, - width: slaveTankMeta?.width || null, - heightInCm: slaveTankMeta?.height ? feetToCm(slaveTankMeta.height) : null, - lengthInCm: slaveTankMeta?.length ? feetToCm(slaveTankMeta.length) : null, - widthInCm: slaveTankMeta?.width ? feetToCm(slaveTankMeta.width) : null - }; - })); + + const matchingTankData = latestIotData?.tanks?.find(t => + t.tankHardwareId === finalHardwareId || t.connected_to === finalHardwareId + ); + + // Use slaveTankMeta.typeOfWater if Insensors.typeOfWater is null + const typeOfWater = slave.typeOfWater || slaveTankMeta?.typeOfWater || masterTypeOfWater; +console.log(matchingTankData) + return { + ...slave, + isMaster: false, + product_status: slave.product_status || 'pending', + tankHeight: matchingTankData?.tankHeight ?? null, + typeOfWater: typeOfWater, + height: slaveTankMeta?.height || null, + length: slaveTankMeta?.length || null, + width: slaveTankMeta?.width || null, + heightInCm: slaveTankMeta?.height ? feetToCm(slaveTankMeta.height) : null, + lengthInCm: slaveTankMeta?.length ? feetToCm(slaveTankMeta.length) : null, + widthInCm: slaveTankMeta?.width ? feetToCm(slaveTankMeta.width) : null + }; +})); // Step 8: Send response return reply.send({ diff --git a/src/controllers/userController.js b/src/controllers/userController.js index 4da87f69..39a9ed9c 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -1816,6 +1816,7 @@ const today = new Date(); const datePart = today.toISOString().slice(0, 10).replace(/-/g, ''); // YYYYMMDD const randomDigit = Math.floor(Math.random() * 10); // 0–9 const bookingId = `ARM${datePart}${randomDigit}`; +const amount_due = matchedSupplier.quoted_amount-matchedSupplier.advance_paid const newBooking = new Tankerbooking({ bookingid: bookingId, @@ -1830,7 +1831,9 @@ const bookingId = `ARM${datePart}${randomDigit}`; supplierName: supplier.suppliername, supplierPhone: supplier.phone, supplierAddress: customer.address, - + amount_paid: String(matchedSupplier.advance_paid), + amount_due: String(amount_due), + advance_reference_number: matchedSupplier.advance_ref_number, type_of_water: requestedBooking.type_of_water, capacity: requestedBooking.capacity, quantity: requestedBooking.quantity, @@ -2076,3 +2079,55 @@ exports.updatestatusForSupplier = async (req, reply) => { } }; + +exports.updateadvanceForSupplier = async (req, reply) => { + try { + const { _id } = req.params; + const { supplierId, advance_paid,advance_ref_number } = 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' }); + } + + + // Map short keywords to the stored values + + + // otherwise keep the original (but normalized) value + + // Atomic update using positional $ operator + const filter = { _id, 'requested_suppliers.supplierId': supplierId }; + const update = { $set: { 'requested_suppliers.$.advance_paid': advance_paid,'requested_suppliers.$.advance_ref_number': advance_ref_number } }; + + 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 + 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: `status updated for supplier ${supplierId}`, + data: updated, + }); + } catch (err) { + console.error('updatestatusForSupplier error:', err); + throw boom.boomify(err); + } +}; diff --git a/src/models/supplier.js b/src/models/supplier.js index 38a26bd4..fc007b83 100644 --- a/src/models/supplier.js +++ b/src/models/supplier.js @@ -165,6 +165,8 @@ const requestedSupplierSchema = new mongoose.Schema({ quoted_amount: Number, time: {type:String,default:null}, // ✅ New field added here status:{type:String,default: "pending" }, + advance_paid: Number, + advance_ref_number: {type:String,default:null}, }, { _id: false }); const requestedBookingSchema = new mongoose.Schema({ diff --git a/src/models/tankers.js b/src/models/tankers.js index 3083d03b..8917d4a0 100644 --- a/src/models/tankers.js +++ b/src/models/tankers.js @@ -58,6 +58,7 @@ const tankersbookingSchema = new mongoose.Schema({ stop_time:{ type: String, default: "null" }, quantityDelivered: { type: String, default: null}, amount_paid: { type: String, default: null }, + advance_reference_number:{ type: String, default: null }, amount_due: { type: String, default: null }, distrubance_price: { type: String, default: "none" }, amount_difference: { type: String, default: "none" }, diff --git a/src/routes/usersRoute.js b/src/routes/usersRoute.js index ea6165d1..d5cbfcfe 100644 --- a/src/routes/usersRoute.js +++ b/src/routes/usersRoute.js @@ -1452,5 +1452,47 @@ fastify.route({ handler: userController.updatestatusForSupplier, }); + + 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, + }); + next(); };