You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

376 lines
9.4 KiB

const { Tanker, Tankerbooking,Bore,GovtPipeLine } = require('../models/tankers')
const { User,Counter, generateBookingId,resetCounter,generateCustomerId } = require('../models/User')
//const User = require("../models/User");
const boom = require("boom");
const fastify = require("fastify")({
logger: true,
});
exports.addTankers = async (req, reply) => {
try {
//const username = req.params.username;
console.log(req.params);
var customerId = req.params.customerId;
//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);
tankersData = {
customerId:customerId,
tankerName: req.body.tankerName,
phoneNumber: req.body.phoneNumber,
typeofwater: req.body.typeofwater,
capacity: req.body.capacity,
};
var tanker_Name = req.body.tankerName
var i_tank = await Tanker.findOne({ tankerName: tanker_Name,customerId:customerId})
if(i_tank){
throw new Error('tankername already exists');
}
else {
var tankers = new Tanker(tankersData);
checkFormEncoding = isUserFormUrlEncoded(req);
if (checkFormEncoding.isUserFormUrlEncoded) {
usertobeInserted = checkFormEncoding.tankers;
console.log("thsi true url string");
tankers.tankerName = usertobeInserted.tankerName;
tankers.phoneNumber = usertobeInserted.phoneNumber;
tankers.capacity = usertobeInserted.capacity;
tankers.typeofwater = usertobeInserted.typeofwater;
tankers.customerId = usertobeInserted.customerId;
}
}
const insertedTank = await tankers.save();
return insertedTank;
} catch (err) {
throw boom.boomify(err);
}
};
//update selected tanker
exports.updateTankersInfo = async (req, reply) => {
try {
var customerId = req.params.customerId;
var tankerName = req.query.tankerName;
var tanker = req.body;
const { ...updateData } = tanker;
const update = await Tanker.findOneAndUpdate({ customerId:customerId,tankerName: tankerName, }, updateData, { new: true });
//console.log(update.username)
//return update;
reply.send({ status_code: 200, data: update });
}
catch (err) {
throw boom.boomify(err);
}
};
3 years ago
//delete selected tanker
exports.deleteTankerInfo = async (req, reply) => {
try {
var customerId = req.params.customerId;
var tankerName = req.query.tankerName;
const tanker = await Tanker.findOneAndDelete({ customerId:customerId,tankerName: tankerName, });
reply.send({ status_code: 200, data: tanker});
// return tanker;
} catch (err) {
throw boom.boomify(err);
}
};
exports.tankerBooking = async (req, reply) => {
try {
var customerId = req.params.customerId;
var b_id = await generateBookingId()
const date = new Date()
const year = date.getFullYear()
const month = (date.getMonth() + 1).toString().padStart(2, '0')
const day = date.getDate().toString().padStart(2, '0')
const bookingId = `ARM${year}${month}${day}${b_id}`
var booking_id = await Tankerbooking.findOne({ bookingid: bookingId})
if(booking_id){
await generateCustomerId()
}
else {
bookingsData = {
customerId:customerId,
bookingid : bookingId,
typeofwater: req.body.typeofwater,
capacity: req.body.capacity,
address: req.body.address,
dateOfOrder: req.body.dateOfOrder,
};
var tankersBookingData = new Tankerbooking(bookingsData);
checkFormEncoding = isUserFormUrlEncoded(req);
if (checkFormEncoding.isUserFormUrlEncoded) {
usertobeInserted = checkFormEncoding.tankersBookingData;
console.log("thsi true url string");
tankersBookingData.customerId = customerId;
tankersBookingData.bookingid = bookingId;
tankersBookingData.capacity = usertobeInserted.capacity;
tankersBookingData.typeofwater = usertobeInserted.typeofwater;
tankersBookingData.address = usertobeInserted.address;
tankersBookingData.dateOfOrder = usertobeInserted.dateOfOrder;
}
}
const booking_data = await tankersBookingData.save();
return booking_data;
} catch (err) {
throw boom.boomify(err);
}
};
exports.getTanker = async (req, reply) => {
try {
await Tanker.find({customerId: req.query.customerId})
.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.addBores = async (req, reply) => {
try {
var customerId = req.params.customerId;
//console.log(customerId);
//const username = loginObject.user.username;
boresData = {
customerId:customerId,
boreName: req.body.boreName,
capacity: req.body.capacity,
typeofwater: req.body.typeofwater,
description: req.body.description,
};
var bore_Name = req.body.boreName
var i_bore = await Bore.findOne({ boreName: bore_Name,customerId:customerId})
if(i_bore){
throw new Error('Bore already exists');
}
else {
var bores = new Bore(boresData);
checkFormEncoding = isUserFormUrlEncoded(req);
if (checkFormEncoding.isUserFormUrlEncoded) {
usertobeInserted = checkFormEncoding.bores;
console.log("thsi true url string");
bores.customerId = usertobeInserted.customerId
bores.boreName = usertobeInserted.boreName;
bores.capacity = usertobeInserted.capacity;
bores.typeofwater = usertobeInserted.typeofwater;
bores.description = usertobeInserted.description;
}
}
const insertedBore = await bores.save();
return insertedBore;
} catch (err) {
throw boom.boomify(err);
}
};
exports.getBores = async (req, reply) => {
try {
await Bore.find({customerId: req.query.customerId})
.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);
}
};
3 years ago
exports.deleteBoresInfo = async (req, reply) => {
try {
3 years ago
await Bore.findOneAndDelete({customerId: req.params.customerId,boreName: req.query.boreName});
//console.log(Bore)
3 years ago
reply.send({ status_code: 200},);
3 years ago
// return Bore;
} catch (err) {
throw boom.boomify(err);
}
3 years ago
};
exports.updateBoresInfo = async (req, reply) => {
try {
var customerId = req.params.customerId;
var boreName = req.query.boreName;
const bore = req.body;
const { ...updateData } = bore;
const update = await Bore.findOneAndUpdate({ customerId:customerId,boreName: boreName, }, updateData, { new: true });
//console.log(update.username)
//return update;
reply.send({ status_code: 200, data: update });
}
catch (err) {
throw boom.boomify(err);
}
};
exports.addGovtPIpeline = async (req, reply) => {
try {
var customerId = req.params.customerId;
//console.log(customerId);
//const username = loginObject.user.username;
pipelineData = {
customerId:customerId,
Name: req.body.Name,
capacity: req.body.capacity,
typeofwater: req.body.typeofwater,
description: req.body.description,
};
var pipeline_Name = req.body.Name
var i_pipe = await GovtPipeLine.findOne({ Name: pipeline_Name,customerId:customerId})
if(i_pipe){
throw new Error('Pipeline already exists');
}
else {
var pipe_line = new GovtPipeLine(pipelineData);
checkFormEncoding = isUserFormUrlEncoded(req);
if (checkFormEncoding.isUserFormUrlEncoded) {
usertobeInserted = checkFormEncoding.pipe_line;
console.log("thsi true url string");
pipe_line.customerId = usertobeInserted.customerId
pipe_line.Name = usertobeInserted.Name;
pipe_line.capacity = usertobeInserted.capacity;
pipe_line.typeofwater = usertobeInserted.typeofwater;
pipe_line.description = usertobeInserted.description;
}
}
const insertedpipeline = await pipe_line.save();
return insertedpipeline;
} catch (err) {
throw boom.boomify(err);
}
};
exports.getPipeline = async (req, reply) => {
try {
await GovtPipeLine.find({customerId: req.query.customerId})
.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.deletePipelineInfo = async (req, reply) => {
try {
await GovtPipeLine.findOneAndDelete({customerId: req.params.customerId,Name: req.query.Name});
//console.log(Bore)
reply.send({ status_code: 200});
// return Bore;
} catch (err) {
throw boom.boomify(err);
}
}
exports.updatePipelineInfo = async (req, reply) => {
try {
var customerId = req.params.customerId;
var Name = req.query.Name;
const pipeline = req.body;
const { ...updateData } = pipeline;
const update = await GovtPipeLine.findOneAndUpdate({ customerId:customerId,Name: Name, }, updateData, { new: true });
//console.log(update.username)
//return update;
reply.send({ status_code: 200, data: update });
}
catch (err) {
throw boom.boomify(err);
}
};