|
|
|
|
|
|
|
//const Supplier = require("../models/supplier");
|
|
|
|
const { Supplier, generateSupplierId, FriendRequest,DeliveryBoy} = require("../models/supplier")
|
|
|
|
const { Tank } = require('../models/tanks')
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
suppliername: req.body.suppliername,
|
|
|
|
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,
|
|
|
|
status:req.body.status,
|
|
|
|
longitude: req.body.longitude,
|
|
|
|
latitude:req.body.latitude,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
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.suppliername = usertobeInserted.suppliername
|
|
|
|
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
|
|
|
|
agent.status = usertobeInserted.status
|
|
|
|
agent.longitude = usertobeInserted.longitude
|
|
|
|
agent.latitude = usertobeInserted.latitude
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const insertedagent = await agent.save();
|
|
|
|
|
|
|
|
console.log("inster...", insertedagent)
|
|
|
|
|
|
|
|
return insertedagent;
|
|
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
throw boom.boomify(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
exports.getbookingsofsupplier = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
console.log(req.params.supplierId)
|
|
|
|
await Tankerbooking.find({supplierId: req.params.supplierId})
|
|
|
|
.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.getbookingsofdeliveryboy = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
console.log(req.params.supplierId)
|
|
|
|
await Tankerbooking.find({delivery_agent_mobile: req.params.agent_mobile})
|
|
|
|
.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.getalldeliveryboysofsupplier = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
console.log(req.params.supplierId)
|
|
|
|
await DeliveryBoy.find({supplierId: req.params.supplierId})
|
|
|
|
.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.getactivedeliveryboysofsupplier = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
console.log(req.params.supplierId)
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const intervals = {};
|
|
|
|
exports.deliveryboystartandstop = async (req, reply) => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
//let start_time,stop_time
|
|
|
|
const bookingId = req.params.bookingId
|
|
|
|
const bookingdata = await Tankerbooking.findOne({bookingid: bookingId})
|
|
|
|
// console.log(bookingdata,"0")
|
|
|
|
// console.log(bookingdata.supplierId,"6")
|
|
|
|
const customerId = bookingdata.customerId;
|
|
|
|
const action = req.body.action
|
|
|
|
const receiver_tank = bookingdata.tankName
|
|
|
|
// console.log(receiver_tank,"5")
|
|
|
|
const receiver_tank_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:"sump"});
|
|
|
|
// console.log(receiver_tank_info,"1")
|
|
|
|
|
|
|
|
const receiver_capacity = parseInt((receiver_tank_info.capacity).replace(/,/g, ''), 10)
|
|
|
|
// console.log(receiver_capacity,"2")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(action === "start"){
|
|
|
|
// console.log("started")
|
|
|
|
start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'})
|
|
|
|
const initial_receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10)
|
|
|
|
|
|
|
|
await Tankerbooking.findOneAndUpdate({bookingid:bookingId}, { $set: { initial_water_level: initial_receiver_waterlevel ,start_time:start_time} });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10)
|
|
|
|
// console.log(receiver_waterlevel,"1")
|
|
|
|
const intervalId = setInterval(async function () {
|
|
|
|
// Calculate new water levels
|
|
|
|
|
|
|
|
const newWaterLevel = receiver_waterlevel+450;
|
|
|
|
//console.log(newWaterLevel,"2",receiver_tank_info.tankName)
|
|
|
|
// Check if updating should stop
|
|
|
|
if ((newWaterLevel/receiver_capacity)*100 >= 97 ) {
|
|
|
|
|
|
|
|
|
|
|
|
clearInterval(intervals[receiver_tank]); // Clear the interval for this tank
|
|
|
|
|
|
|
|
delete intervals[receiver_tank];
|
|
|
|
stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'})
|
|
|
|
|
|
|
|
const final_tank_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:"sump"});
|
|
|
|
const final_receiver_waterlevel = parseInt(final_tank_info.waterlevel.replace(/,/g, ''), 10)
|
|
|
|
// console.log(final_receiver_waterlevel)
|
|
|
|
// clearInterval(intervalId);
|
|
|
|
await Tankerbooking.findOneAndUpdate({bookingid:bookingId}, { $set: { final_water_level: final_receiver_waterlevel ,stop_time:stop_time} });
|
|
|
|
// console.log("end for" + receiver_tank);
|
|
|
|
} else {
|
|
|
|
// Update water levels in database
|
|
|
|
|
|
|
|
receiver_waterlevel = newWaterLevel;
|
|
|
|
// console.log(receiver_tank+""+newWaterLevel+""+"bore to sump")
|
|
|
|
//console.log((newWaterLevel/receiver_capacity)*100,"4",receiver_tank_info.tankName)
|
|
|
|
await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:"sump"}, { $set: { waterlevel: newWaterLevel } })
|
|
|
|
// console.log(intervals,"check1")
|
|
|
|
}
|
|
|
|
}, 2000);
|
|
|
|
intervals[receiver_tank] = intervalId;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// console.log(customerId,req.body.from,req.body.from_type,receiver_tank,req.body.to_type,)
|
|
|
|
|
|
|
|
|
|
|
|
reply.send({ status_code: 200, "start time": start_time,});
|
|
|
|
console.log(start_time)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action === "stop") {
|
|
|
|
const booking_data = await Tankerbooking.findOne({bookingid: bookingId})
|
|
|
|
console.log(intervals,"hii3")
|
|
|
|
try {
|
|
|
|
clearInterval(intervals[receiver_tank]); // Clear the interval for this tank
|
|
|
|
delete intervals[receiver_tank]; // Delete the interval ID for this tank from the intervals object
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'})
|
|
|
|
const final_tank_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:"sump"});
|
|
|
|
// console.log(final_tank_info,"hii")
|
|
|
|
const final_receiver_waterlevel = parseInt(final_tank_info.waterlevel.replace(/,/g, ''), 10)
|
|
|
|
|
|
|
|
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} });
|
|
|
|
reply.send({ status_code: 200, "stop time": stop_time});
|
|
|
|
} else {
|
|
|
|
throw new Error("Invalid action");
|
|
|
|
}
|
|
|
|
|
|
|
|
return { message: 'Water level updates started' };
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error(`Failed to start/stop water level updates: ${err.message}`);
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|