//const Supplier = require("../models/supplier"); const { Supplier, generateSupplierId, FriendRequest,DeliveryBoy} = require("../models/supplier") const boom = require("boom"); const fastify = require("fastify")({ logger: true, }); const { Tanker, Tankerbooking,Bore,GovtPipeLine } = require('../models/tankers') exports.orderNow = async (req, reply) => { try { const customerId = req.body.customerId; 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 if(action === "accept"){ const price = req.body.price const delivery_agent = req.body.delivery_agent const agent_mobile = req.body.agent_mobile const agent_alternative_mobile = req.body.agent_alternative_mobile booking_info.orderStatus = "accepted" booking_info.delivery_agent = delivery_agent booking_info.delivery_agent_mobile = agent_mobile booking_info.delivery_agent_alternative_mobile = agent_alternative_mobile booking_info.price = price } else { booking_info.orderStatus = "rejected" } const booking = await booking_info.save(); return booking; } catch (err) { throw boom.boomify(err); } }; exports.addDeliveryboy = async (req, reply) => { try { const supplierId = req.params.supplierId //const username = req.params.username; console.log(req.params); //const {username} = loginObject.user.username; //console.log(loginObject.user.username) // const userInfo = await User.findOne({ username: username.toString() }); // const updateData = req.body; // console.log("This is the reply in the handler after the validations", reply); deliveryData = { supplierId: supplierId, name: req.body.Name, phone: req.body.phone, alternativeContactNumber: req.body.alternativeContactNumber, address: req.body.address, city:req.body.city, state:req.body.state, zip:req.body.zip, }; var agent_mobile = req.body.phone var i_agent = await DeliveryBoy.findOne({ phone: agent_mobile}) if(i_agent){ throw new Error('phone already exists'); } else { var agent = new DeliveryBoy(deliveryData); checkFormEncoding = isUserFormUrlEncoded(req); if (checkFormEncoding.isUserFormUrlEncoded) { usertobeInserted = checkFormEncoding.agent; console.log("thsi true url string"); agent.supplierId = usertobeInserted.supplierId agent.name = usertobeInserted.name; agent.phone = usertobeInserted.phone; agent.alternativeContactNumber = usertobeInserted.alternativeContactNumber; agent.address = usertobeInserted.address; agent.city = usertobeInserted.city agent.state = usertobeInserted.state agent.zip = usertobeInserted.zip } } const insertedagent = await agent.save(); return insertedagent; } catch (err) { throw boom.boomify(err); } }; exports.getactiveDeliveryboys = async (req, reply) => { try { await DeliveryBoy.find({supplierId: req.params.supplierId,status:"active"}) .exec() .then((docs) => { reply.send({ status_code: 200, data: docs, count: docs.length }); }) .catch((err) => { console.log(err); reply.send({ error: err }); }); } catch (err) { throw boom.boomify(err); } }; exports.verifyPhone = async (req, reply) => { console.log("-------------------------------------------------"); try { phone = req.body.phone; phoneVerificationCode = req.body.phoneVerificationCode; // check if user exists in the system. If user exists , display message that // username is not available console.log( "this is the phone and verification code", phone, phoneVerificationCode ); userExists = await Supplier.findOne({ phone: phone, phoneVerified: false, phoneVerificationCode: phoneVerificationCode, }); console.log(userExists); if (userExists) { // update the phoneVerified flag to true. const filter = { phone: phone, phoneVerificationCode: phoneVerificationCode, }; const update = { phoneVerified: true }; const doc = await Supplier.findOneAndUpdate(filter, update); updatedUser = await Supplier.findOne({ phone: phone }); if (updatedUser.phoneVerified) { reply.send('{"armintatankdata":{"error":false,"verified": true}}'); } else { error = { armintatankdata: { error: true, code: 10005, message: "10005 - Verification code entered cannot be validated.", }, }; req.body.regError = error; reply.send(error); } } else { error = { armintatankdata: { error: true, code: 10005, message: "10005 - Verification code entered cannot be validated.", }, }; req.body.regError = error; reply.send(error); } } catch (err) { throw boom.boomify(err); } };