parent
8848f2ab42
commit
1378b3bcb1
@ -0,0 +1,144 @@
|
|||||||
|
|
||||||
|
const Tanker = require("../models/tankers");
|
||||||
|
const Tankerbooking = require("../models/tankers");
|
||||||
|
|
||||||
|
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);
|
||||||
|
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);
|
||||||
|
tankersData = {
|
||||||
|
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})
|
||||||
|
if(i_tank){
|
||||||
|
throw new Error('tankname already exists');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
var tankers = new Tanker(tankersData);
|
||||||
|
|
||||||
|
checkFormEncoding = isUserFormUrlEncoded(req);
|
||||||
|
if (checkFormEncoding.isUserFormUrlEncoded) {
|
||||||
|
usertobeInserted = checkFormEncoding.tank;
|
||||||
|
console.log("thsi true url string");
|
||||||
|
tankers.tankerName = usertobeInserted.tankerName;
|
||||||
|
tankers.phoneNumber = usertobeInserted.phoneNumber;
|
||||||
|
tankers.capacity = usertobeInserted.capacity;
|
||||||
|
tankers.typeofwater = usertobeInserted.typeofwater;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const insertedTank = await tankers.save();
|
||||||
|
|
||||||
|
return insertedTank;
|
||||||
|
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
throw boom.boomify(err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//update selected tanker
|
||||||
|
exports.updateTankersInfo = async (req, reply) => {
|
||||||
|
try {
|
||||||
|
//const username = loginObject.user.username;
|
||||||
|
const tankerName = req.params.tankerName;
|
||||||
|
|
||||||
|
var tank_name = req.body.tankerName
|
||||||
|
var i_tank = await Tanker.findOne({ tankerName: tank_name})
|
||||||
|
const tanker = req.body;
|
||||||
|
const { ...updateData } = tanker;
|
||||||
|
const update = await Tanker.findOneAndUpdate({ tankerName: tankerName }, updateData, { new: true });
|
||||||
|
|
||||||
|
if(i_tank){
|
||||||
|
throw new Error('tankname already exists');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//return update;
|
||||||
|
reply.send({ status_code: 200, data: update });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
throw boom.boomify(err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//delete selected tank
|
||||||
|
exports.deleteTankerInfo = async (req, reply) => {
|
||||||
|
try {
|
||||||
|
const tankerName = req.params.tankerName;
|
||||||
|
const tanker = await Tanker.findOneAndDelete({ tankerName: tankerName });
|
||||||
|
reply.send({ status_code: 200, data: tanker});
|
||||||
|
// return tanker;
|
||||||
|
} catch (err) {
|
||||||
|
throw boom.boomify(err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
exports.tankerBooking = async (req, reply) => {
|
||||||
|
try {
|
||||||
|
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 count = bookingCount.toString().padStart(2, '0')
|
||||||
|
const bookingId = `booking${year}${month}${day}${count}`
|
||||||
|
//bookingCount = (bookingCount + 1) % 100;
|
||||||
|
|
||||||
|
|
||||||
|
bookingsData = {
|
||||||
|
|
||||||
|
typeofwater: req.body.typeofwater,
|
||||||
|
capacity: req.body.capacity,
|
||||||
|
address: req.body.address,
|
||||||
|
dateTime: req.body.dateTime,
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
var tankersBookingData = new Tankerbooking(bookingsData);
|
||||||
|
|
||||||
|
checkFormEncoding = isUserFormUrlEncoded(req);
|
||||||
|
if (checkFormEncoding.isUserFormUrlEncoded) {
|
||||||
|
usertobeInserted = checkFormEncoding.tankersBookingData;
|
||||||
|
console.log("thsi true url string");
|
||||||
|
tankersBookingData.bookingid = bookingId;
|
||||||
|
tankersBookingData.capacity = usertobeInserted.capacity;
|
||||||
|
tankersBookingData.typeofwater = usertobeInserted.typeofwater;
|
||||||
|
tankersBookingData.address = usertobeInserted.address;
|
||||||
|
tankersBookingData.dateTime = usertobeInserted.dateTime;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
const booking_data = await tankersBookingData.save();
|
||||||
|
|
||||||
|
return booking_data;
|
||||||
|
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
throw boom.boomify(err);
|
||||||
|
}
|
||||||
|
};
|
@ -0,0 +1,153 @@
|
|||||||
|
const fastify = require("fastify");
|
||||||
|
const tankersController = require("../controllers/tankersController.js");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = function (fastify, opts, next) {
|
||||||
|
|
||||||
|
fastify.route({
|
||||||
|
method: "POST",
|
||||||
|
url: "/api/addTankers",
|
||||||
|
schema: {
|
||||||
|
tags: ["Tanker"],
|
||||||
|
description: "This is for cretae New Tanker",
|
||||||
|
summary: "This is for Create New Tanker.",
|
||||||
|
body: {
|
||||||
|
type: "object",
|
||||||
|
properties: {
|
||||||
|
tankerName: { type: "string" },
|
||||||
|
phoneNumber: { type: "string"},
|
||||||
|
typeofwater: { type: "string" },
|
||||||
|
capacity: { type: "string" },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
security: [
|
||||||
|
{
|
||||||
|
basicAuth: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
|
||||||
|
},
|
||||||
|
preHandler: fastify.auth([fastify.authenticate]),
|
||||||
|
handler: tankersController.addTankers,
|
||||||
|
// onResponse: (request, reply) => {
|
||||||
|
// validationHandler.sendPhoneVerificationCode(request, reply);
|
||||||
|
// },
|
||||||
|
//onResponse: validationHandler.sendPhoneVerificationCode,
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
//update tankers
|
||||||
|
fastify.route({
|
||||||
|
method: "PUT",
|
||||||
|
url: "/api/updateTankers/:tankerName",
|
||||||
|
schema: {
|
||||||
|
tags: ["Tanker"],
|
||||||
|
summary: "This is for update tanker",
|
||||||
|
params: {
|
||||||
|
required: ["tankerName"],
|
||||||
|
type: "object",
|
||||||
|
properties: {
|
||||||
|
tankerName: {
|
||||||
|
type: "string",
|
||||||
|
description: "tankerName",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
body: {
|
||||||
|
type: "object",
|
||||||
|
// required: ['phone'],
|
||||||
|
properties: {
|
||||||
|
tankerName: { type: "string", default: null },
|
||||||
|
phoneNumber: { type: "string", default: null },
|
||||||
|
capacity: { type: "number" },
|
||||||
|
typeofwater: { type: "string", default: null },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
security: [
|
||||||
|
{
|
||||||
|
basicAuth: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
// preHandler: [
|
||||||
|
// fastify.auth([fastify.operatorAuthenticate]),
|
||||||
|
// validationHandler.validatePhoneFormat,
|
||||||
|
// ],
|
||||||
|
preHandler: fastify.auth([fastify.authenticate]),
|
||||||
|
handler: tankersController.updateTankersInfo,
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
fastify.route({
|
||||||
|
method: "PUT",
|
||||||
|
url: "/api/deleteTanker/:tankerName",
|
||||||
|
schema: {
|
||||||
|
tags: ["Tanker"],
|
||||||
|
summary: "This is for delete tanker",
|
||||||
|
params: {
|
||||||
|
required: ["tankerName"],
|
||||||
|
type: "object",
|
||||||
|
properties: {
|
||||||
|
tankerName: {
|
||||||
|
type: "string",
|
||||||
|
description: "tankerName",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
security: [
|
||||||
|
{
|
||||||
|
basicAuth: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
preHandler: fastify.auth([fastify.authenticate]),
|
||||||
|
handler: tankersController.deleteTankerInfo,
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
fastify.route({
|
||||||
|
method: "POST",
|
||||||
|
url: "/api/bookingData",
|
||||||
|
schema: {
|
||||||
|
tags: ["Tanker"],
|
||||||
|
description: "This is for store booking data of a Tanker",
|
||||||
|
summary: "This is for store booking data of a Tanker",
|
||||||
|
body: {
|
||||||
|
type: "object",
|
||||||
|
properties: {
|
||||||
|
|
||||||
|
typeofwater: { type: "string" },
|
||||||
|
capacity: { type: "string" },
|
||||||
|
address: { type: "string" },
|
||||||
|
dateTime: { type: "string"},
|
||||||
|
|
||||||
|
},
|
||||||
|
},
|
||||||
|
security: [
|
||||||
|
{
|
||||||
|
basicAuth: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
|
||||||
|
},
|
||||||
|
preHandler: fastify.auth([fastify.authenticate]),
|
||||||
|
handler: tankersController.tankerBooking,
|
||||||
|
// onResponse: (request, reply) => {
|
||||||
|
// validationHandler.sendPhoneVerificationCode(request, reply);
|
||||||
|
// },
|
||||||
|
//onResponse: validationHandler.sendPhoneVerificationCode,
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in new issue