diff --git a/src/controllers/supplierOrderController.js b/src/controllers/supplierOrderController.js index d5cab14d..5aae078e 100644 --- a/src/controllers/supplierOrderController.js +++ b/src/controllers/supplierOrderController.js @@ -3,6 +3,7 @@ const { Supplier, generateSupplierId, FriendRequest,DeliveryBoy} = require("../models/supplier") const { Tank } = require('../models/tanks') +const http = require('https'); const boom = require("boom"); const fastify = require("fastify")({ @@ -10,6 +11,53 @@ const fastify = require("fastify")({ }); const { Tanker, Tankerbooking,Bore,GovtPipeLine } = require('../models/tankers') +exports.orderStartedSms = async (request, reply) => { +//console.log(bookingId,supplierContact,deliveryAgentContact,mobile) + const bookingId = request.params.bookingId; + const booking_data = await Tankerbooking.findOne({ bookingid: bookingId}) + console.log(booking_data) + const supplierContact = booking_data.supplierPhone + const deliveryAgentContact = booking_data.delivery_agent_mobile + const user_name = booking_data.customerName + const mobile = booking_data.customerPhone + const username = 'Arminta'; + const apiKey = '2068323bea61494d315b'; + const senderId = 'ARMNTA'; + const message = `Hello ${user_name}, We are pleased to inform you that your booking with bookingId ${bookingId} has been started,you can track the current location of tanker here {#var#}. Please note that the supplier's contact number is ${supplierContact} and deliver agent contact number is ${deliveryAgentContact} Thank you for choosing our services. If you have any further questions or concerns, please do not hesitate to contact us. Best regards, Arminta Water Services`; + + const apiUrl = `https://smslogin.co/v3/api.php?username=${username}&apikey=${apiKey}&senderid=${senderId}&mobile=${mobile}&message=${encodeURIComponent(message)}`; + + const options = { + method: 'GET', + headers: { + 'Content-Type': 'application/json' + } + }; + + const req = http.request(apiUrl, options, (res) => { + let data = ''; + res.on('data', (chunk) => { + data += chunk; + }); + console.log(req) + res.on('end', () => { + reply.send(data); + }); + }); + + req.on('error', (error) => { + console.error(error); + reply.send({ error: 'Failed to send SMS' }); + }); + + req.end(); +} + + + + + + exports.orderNow = async (req, reply) => { @@ -18,6 +66,7 @@ exports.orderNow = async (req, reply) => { const bookingId = req.params.bookingId; const tankerName = req.body.tankerName; const booking_info = await Tankerbooking.findOne({ bookingid: bookingId}) + const action = req.body.action const typeofwater = req.body.typeofwater const expectedDateOfDelivery = req.body.expectedDateOfDelivery; @@ -27,6 +76,11 @@ exports.orderNow = async (req, reply) => { const delivery_agent = req.body.delivery_agent const agent_mobile = req.body.agent_mobile const agent_alternative_mobile = req.body.agent_alternative_mobile + const booking_data = await Tankerbooking.findOne({ bookingid: bookingId}) + const supplierContact = booking_data.supplierPhone + const deliveryAgentContact = agent_mobile + const mobile = booking_data.customerPhone + // hash = await orderStartedSms(bookingId,supplierContact,deliveryAgentContact,mobile); booking_info.orderStatus = "accepted" booking_info.delivery_agent = delivery_agent booking_info.delivery_agent_mobile = agent_mobile @@ -203,6 +257,9 @@ exports.deliveryboystartandstop = async (req, reply) => { //let start_time,stop_time const bookingId = req.params.bookingId const bookingdata = await Tankerbooking.findOne({bookingid: bookingId}) + const ordered_quantity = parseInt((bookingdata.capacity).replace(/,/g, ''), 10) + const price = parseInt((bookingdata.price).replace(/,/g, ''), 10) + const price_per_liter = ordered_quantity/price // console.log(bookingdata.supplierId,"6") @@ -297,8 +354,9 @@ exports.deliveryboystartandstop = async (req, reply) => { const start_water_level = parseInt(booking_data.initial_water_level.replace(/,/g, ''), 10) const quantity_delivered = final_receiver_waterlevel-start_water_level - - await Tankerbooking.findOneAndUpdate({bookingid:bookingId}, { $set: { final_water_level: final_receiver_waterlevel ,stop_time:stop_time,quantityDelivered:quantity_delivered,tankerRunningStatus:"0"} }); + const distrubance_price = parseInt(quantity_delivered.replace(/,/g, ''), 10)*price_per_liter + const amount_difference = price - distrubance_price + await Tankerbooking.findOneAndUpdate({bookingid:bookingId}, { $set: { final_water_level: final_receiver_waterlevel ,stop_time:stop_time,quantityDelivered:quantity_delivered,distrubance_price:distrubance_price,amount_difference:amount_difference,tankerRunningStatus:"0"} }); reply.send({ status_code: 200, "stop time": stop_time,"final water level":final_receiver_waterlevel}); } else { throw new Error("Invalid action"); diff --git a/src/controllers/tankersController.js b/src/controllers/tankersController.js index 0430d101..a6bfb87e 100644 --- a/src/controllers/tankersController.js +++ b/src/controllers/tankersController.js @@ -681,11 +681,11 @@ exports.distrubanceStatus = async (req, reply) => { const action = req.body.action const booking = await Tankerbooking.findOne({ bookingid:bookingid }); const distrubance_price = parseInt(booking.distrubance_price .replace(/,/g, ''), 10) - const actual_price = parseInt(booking.actual_price.replace(/,/g, ''), 10) + const price = parseInt(booking.price.replace(/,/g, ''), 10) const amount_due = parseInt(booking.amount_due .replace(/,/g, ''), 10) - const price_variation = actual_price-distrubance_price + const price_variation = price-distrubance_price if (action === "accept") { - await Tankerbooking.findOneAndUpdate({ bookingid:bookingid }, { $set: { amount_due: actual_price,amount_difference:"0" } }); + await Tankerbooking.findOneAndUpdate({ bookingid:bookingid }, { $set: { amount_due: price,amount_difference:"0" } }); } if (action === "reject") { diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 91634152..a150267a 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -474,7 +474,7 @@ exports.calculateCapacity = async (req, reply) => { return { message: 'success' }; } - if(shape==="horizontalcylinder"){ + if(shape==="cylinder"){ console.log("hii1") const { length,diameter } = req.body @@ -495,28 +495,8 @@ exports.calculateCapacity = async (req, reply) => { return { message: 'success' }; } - if(shape==="verticalcylinder"){ - console.log("hii2") - const { height,diameter } = req.body - - // Ensure all parameters are valid numbers - if (isNaN(height) || isNaN(diameter)) { - reply.code(400).send('Invalid input parameters') - return - } - - // Calculate the capacity of the water tank in liters - const radius = diameter / 2 - const volume = Math.PI * Math.pow(radius, 2) * height - const capacity = volume * 1000 - - reply.send({ status_code: 200, capacity: capacity}); - - - return { message: 'success' }; - -} - if(shape==="horizontaloval"){ + + if(shape==="oval"){ console.log("hii3") const { length, width, height } = req.body @@ -543,32 +523,6 @@ exports.calculateCapacity = async (req, reply) => { } -if(shape=== "verticaloval"){ - console.log("hii4") - const { length, width, height } = req.body - - // Ensure all parameters are valid numbers - if (isNaN(length) || isNaN(width) || isNaN(height)) { - reply.code(400).send('Invalid input parameters') - return - } - - // Calculate the capacity of the water tank in liters - const radius = width / 2 - const a = height - width - const area = Math.PI * radius * radius + 2 * radius * a - const volume = area * length - const capacity = volume * 1000 - - // Format the result with two decimal places and comma-separated thousands - const formattedCapacity = capacity.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,') - - reply.send({ status_code: 200, capacity: formattedCapacity}); - - - return { message: 'success' }; - -} if(shape==="horizontalellipse"){ const { length, width, height } = req.body diff --git a/src/models/tankers.js b/src/models/tankers.js index e941e5e5..2aefd1f2 100644 --- a/src/models/tankers.js +++ b/src/models/tankers.js @@ -44,7 +44,7 @@ const tankersbookingSchema = new mongoose.Schema({ typeofwater: { type: String, default: null }, capacity: { type: String, default: null }, address: { type: String, default: null }, - actual_price: { type: String, default: "750" }, + price: { type: String, default: null }, payment_status : { type: String, default: "due" }, orderStatus: { type: String, default: "pending" }, delivery_agent : { type: String, default: "null" }, diff --git a/src/routes/supplierOrdersRoutes.js b/src/routes/supplierOrdersRoutes.js index eb894f72..6db65960 100644 --- a/src/routes/supplierOrdersRoutes.js +++ b/src/routes/supplierOrdersRoutes.js @@ -57,6 +57,45 @@ module.exports = function (fastify, opts, next) { //onResponse: validationHandler.sendPhoneVerificationCode, }); + + fastify.route({ + method: "POST", + url: "/api/orderStartedSms/:bookingId", + schema: { + tags: ["Supplier-Order"], + description:"This is for giving booking data to user through sms", + summary: "This is for giving booking data to user through sms", + params: { + required: ["bookingId"], + type: "object", + properties: { + bookingId: { + type: "string", + description: "bookingId", + }, + }, + }, + + security: [ + { + basicAuth: [], + }, + ], + + }, + // preHandler: fastify.auth([fastify.authenticate]), + handler:supplierOrderController.orderStartedSms, + // onResponse: (request, reply) => { + // validationHandler.sendPhoneVerificationCode(request, reply); + // }, + //onResponse: validationHandler.sendPhoneVerificationCode, + }); + + + + + + fastify.route({ method: "POST", url: "/api/addDeliveryboys/:supplierId",