crud operations in tankers with customer id

master
varun 3 years ago
parent 5bc965d69f
commit bd9f8531ac

@ -16,7 +16,7 @@ exports.addTankers = async (req, reply) => {
//const username = req.params.username; //const username = req.params.username;
console.log(req.params); console.log(req.params);
const username = loginObject.user.username; var customerId = req.params.customerId;
//console.log(loginObject.user.username) //console.log(loginObject.user.username)
// const userInfo = await User.findOne({ username: username.toString() }); // const userInfo = await User.findOne({ username: username.toString() });
// const updateData = req.body; // const updateData = req.body;
@ -25,6 +25,7 @@ console.log(req.params);
// console.log("This is the reply in the handler after the validations", reply); // console.log("This is the reply in the handler after the validations", reply);
tankersData = { tankersData = {
customerId:customerId,
tankerName: req.body.tankerName, tankerName: req.body.tankerName,
phoneNumber: req.body.phoneNumber, phoneNumber: req.body.phoneNumber,
typeofwater: req.body.typeofwater, typeofwater: req.body.typeofwater,
@ -33,9 +34,9 @@ console.log(req.params);
var tanker_Name = req.body.tankerName var tanker_Name = req.body.tankerName
var i_tank = await Tanker.findOne({ tankerName: tanker_Name}) var i_tank = await Tanker.findOne({ tankerName: tanker_Name,customerId:customerId})
if(i_tank){ if(i_tank){
throw new Error('tankname already exists'); throw new Error('tankername already exists');
} }
else { else {
@ -49,6 +50,7 @@ console.log(req.params);
tankers.phoneNumber = usertobeInserted.phoneNumber; tankers.phoneNumber = usertobeInserted.phoneNumber;
tankers.capacity = usertobeInserted.capacity; tankers.capacity = usertobeInserted.capacity;
tankers.typeofwater = usertobeInserted.typeofwater; tankers.typeofwater = usertobeInserted.typeofwater;
tankers.customerId = usertobeInserted.customerId;
} }
} }
const insertedTank = await tankers.save(); const insertedTank = await tankers.save();
@ -62,25 +64,20 @@ console.log(req.params);
}; };
//update selected tanker //update selected tanker
exports.updateTankersInfo = async (req, reply) => { exports.updateTankersInfo = async (req, reply) => {
try {
//const username = loginObject.user.username;
const tankerName = req.params.tankerName;
var tank_name = req.body.tankerName try {
var i_tank = await Tanker.findOne({ tankerName: tank_name}) var customerId = req.params.customerId;
const tanker = req.body; var tankerName = req.query.tankerName;
var tanker = req.body;
const { ...updateData } = tanker; const { ...updateData } = tanker;
const update = await Tanker.findOneAndUpdate({ tankerName: tankerName }, updateData, { new: true }); 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 });
if(i_tank){
throw new Error('tankname already exists');
}
else
{
//return update;
reply.send({ status_code: 200, data: update });
}
} }
catch (err) { catch (err) {
throw boom.boomify(err); throw boom.boomify(err);
@ -90,8 +87,9 @@ exports.updateTankersInfo = async (req, reply) => {
//delete selected tanker //delete selected tanker
exports.deleteTankerInfo = async (req, reply) => { exports.deleteTankerInfo = async (req, reply) => {
try { try {
const tankerName = req.params.tankerName; var customerId = req.params.customerId;
const tanker = await Tanker.findOneAndDelete({ tankerName: tankerName }); var tankerName = req.query.tankerName;
const tanker = await Tanker.findOneAndDelete({ customerId:customerId,tankerName: tankerName, });
reply.send({ status_code: 200, data: tanker}); reply.send({ status_code: 200, data: tanker});
// return tanker; // return tanker;
} catch (err) { } catch (err) {
@ -139,7 +137,6 @@ exports.tankerBooking = async (req, reply) => {
tankersBookingData.address = usertobeInserted.address; tankersBookingData.address = usertobeInserted.address;
tankersBookingData.dateOfOrder = usertobeInserted.dateOfOrder; tankersBookingData.dateOfOrder = usertobeInserted.dateOfOrder;
} }
} }
@ -156,7 +153,7 @@ exports.tankerBooking = async (req, reply) => {
exports.getTanker = async (req, reply) => { exports.getTanker = async (req, reply) => {
try { try {
await Tanker.find({tankerName: req.query.tankerName}) await Tanker.find({customerId: req.query.customerId})
.exec() .exec()
.then((docs) => { .then((docs) => {
reply.send({ status_code: 200, data: docs, count: docs.length }); reply.send({ status_code: 200, data: docs, count: docs.length });
@ -173,17 +170,11 @@ exports.getTanker = async (req, reply) => {
exports.addBores = async (req, reply) => { exports.addBores = async (req, reply) => {
try { try {
//const username = req.params.username;
var customerId = req.params.customerId; var customerId = req.params.customerId;
console.log(customerId); //console.log(customerId);
//const username = loginObject.user.username; //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);
boresData = { boresData = {
customerId:customerId, customerId:customerId,
boreName: req.body.boreName, boreName: req.body.boreName,
@ -253,3 +244,23 @@ exports.deleteBoresInfo = async (req, reply) => {
}; };
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);
}
};

@ -8,11 +8,21 @@ module.exports = function (fastify, opts, next) {
fastify.route({ fastify.route({
method: "POST", method: "POST",
url: "/api/addTankers", url: "/api/addTankers/:customerId",
schema: { schema: {
tags: ["Supplier"], tags: ["Supplier"],
description: "This is to cretae New Tanker", description: "This is to cretae New Tanker",
summary: "This is to Create New Tanker.", summary: "This is to Create New Tanker.",
params: {
required: ["customerId"],
type: "object",
properties: {
customerId: {
type: "string",
description: "customerId",
},
},
},
body: { body: {
type: "object", type: "object",
properties: { properties: {
@ -41,20 +51,23 @@ module.exports = function (fastify, opts, next) {
//update tankers //update tankers
fastify.route({ fastify.route({
method: "PUT", method: "PUT",
url: "/api/updateTankers/:tankerName", url: "/api/updateTankers/:customerId",
schema: { schema: {
tags: ["Supplier"], tags: ["Supplier"],
summary: "This is to update tanker", summary: "This is to update tanker",
params: { params: {
required: ["tankerName"], required: ["customerId"],
type: "object", type: "object",
properties: { properties: {
tankerName: { customerId: {
type: "string", type: "string",
description: "tankerName", description: "customerId",
}, },
}, },
}, },
querystring: {
tankerName: {type: 'string'}
},
body: { body: {
type: "object", type: "object",
// required: ['phone'], // required: ['phone'],
@ -82,20 +95,23 @@ module.exports = function (fastify, opts, next) {
fastify.route({ fastify.route({
method: "PUT", method: "PUT",
url: "/api/deleteTanker/:tankerName", url: "/api/deleteTanker/:customerId",
schema: { schema: {
tags: ["Supplier"], tags: ["Supplier"],
summary: "This is to delete tanker", summary: "This is to delete tanker",
params: { params: {
required: ["tankerName"], required: ["customerId"],
type: "object", type: "object",
properties: { properties: {
tankerName: { customerId: {
type: "string", type: "string",
description: "tankerName", description: "customerId",
}, },
}, },
}, },
querystring: {
tankerName: {type: 'string'}
},
security: [ security: [
{ {
basicAuth: [], basicAuth: [],
@ -149,7 +165,7 @@ module.exports = function (fastify, opts, next) {
description: "This is for Get Tanker Data", description: "This is for Get Tanker Data",
summary: "This is for to Get Tanker Data", summary: "This is for to Get Tanker Data",
querystring: { querystring: {
tankerName: {type: 'string'} customerId: {type: 'string'}
}, },
security: [ security: [
{ {
@ -244,10 +260,57 @@ module.exports = function (fastify, opts, next) {
}, },
], ],
}, },
//preHandler: fastify.auth([fastify.authenticate]), //preHandler: fastify.auth([fastify.authenticate]),
handler: tankersController.deleteBoresInfo, handler: tankersController.deleteBoresInfo,
}); });
fastify.route({
method: "PUT",
url: "/api/updateBores/:customerId",
schema: {
tags: ["Supplier"],
summary: "This is for update bore",
params: {
required: ["customerId"],
type: "object",
properties: {
customerId: {
type: "string",
description: "customerId",
},
},
},
querystring: {
boreName: {type: 'string'}
},
body: {
type: "object",
// required: ['phone'],
properties: {
boreName: { type: "string" },
typeofwater: { type: "string" },
description: { type: "string" },
},
},
security: [
{
basicAuth: [],
},
],
},
// preHandler: [
// fastify.auth([fastify.operatorAuthenticate]),
// validationHandler.validatePhoneFormat,
// ],
preHandler: fastify.auth([fastify.authenticate]),
handler: tankersController.updateBoresInfo,
});

Loading…
Cancel
Save