From 8dd793c117f7017a92718e4502d1c0e8f936b728 Mon Sep 17 00:00:00 2001 From: varun Date: Thu, 9 Feb 2023 05:19:07 -0500 Subject: [PATCH] made change in booking id and linked booking data with customer id --- src/controllers/tankersController.js | 22 ++++++++++++---------- src/controllers/userController.js | 4 ++-- src/models/User.js | 14 ++++++++++++-- src/routes/tankersRoute.js | 12 +++++++++++- 4 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/controllers/tankersController.js b/src/controllers/tankersController.js index 083b4a90..6215417c 100644 --- a/src/controllers/tankersController.js +++ b/src/controllers/tankersController.js @@ -1,6 +1,8 @@ const { Tanker, Tankerbooking,Bore,GovtPipeLine } = require('../models/tankers') +const { User,Counter, generateBookingId,resetCounter,generateCustomerId } = require('../models/User') -const User = require("../models/User"); + +//const User = require("../models/User"); const boom = require("boom"); @@ -97,26 +99,25 @@ exports.deleteTankerInfo = async (req, reply) => { } }; -let bookingCount = 0 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 count = bookingCount.toString().padStart(4, '0') - const bookingId = `ARM${year}${month}${day}${count}` - bookingCount = (bookingCount + 1) % 100; - - + 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, @@ -130,7 +131,8 @@ exports.tankerBooking = async (req, reply) => { checkFormEncoding = isUserFormUrlEncoded(req); if (checkFormEncoding.isUserFormUrlEncoded) { usertobeInserted = checkFormEncoding.tankersBookingData; - console.log("thsi true url string");s + console.log("thsi true url string"); + tankersBookingData.customerId = customerId; tankersBookingData.bookingid = bookingId; tankersBookingData.capacity = usertobeInserted.capacity; tankersBookingData.typeofwater = usertobeInserted.typeofwater; diff --git a/src/controllers/userController.js b/src/controllers/userController.js index 1efda3f6..97a46a80 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -9,7 +9,7 @@ const libphonenumberjs = require("libphonenumber-js"); const boom = require("boom"); // Get Data Models -const { User,Counter, generateBookingId,resetCounter } = require('../models/User') +const { User,Counter, generateBookingId,resetCounter,generateCustomerId } = require('../models/User') //const User = require("../models/User"); const customJwtAuth = require("../customAuthJwt"); @@ -163,7 +163,7 @@ exports.editCuurentUserInfo = async (req, reply) => { exports.addUser = async (req, reply) => { try { // await resetCounter();//to set customer id back to 0 - var c_id = await generateBookingId() + var c_id = await generateCustomerId() var building= ((req.body.buildingName).slice(0, 3)).toUpperCase(); var customer_id = `AWS${building}${c_id}` // console.log("This is the reply in the handler after the validations", reply); diff --git a/src/models/User.js b/src/models/User.js index 38151b5f..3f69bbbc 100644 --- a/src/models/User.js +++ b/src/models/User.js @@ -21,8 +21,18 @@ const resetCounter = async () => { }; +const generateCustomerId = async () => { + var result = await Counter.findOneAndUpdate( + { _id: 'customer_id' }, + { $inc: { seq: 1 } }, + { upsert: true, new: true } + ); + + return result.seq; +}; + const generateBookingId = async () => { - const result = await Counter.findOneAndUpdate( + var result = await Counter.findOneAndUpdate( { _id: 'booking_id' }, { $inc: { seq: 1 } }, { upsert: true, new: true } @@ -110,4 +120,4 @@ const User = mongoose.model("User", userSchema); //module.exports = mongoose.model("User", userSchema); -module.exports = { User,Counter, generateBookingId ,resetCounter}; +module.exports = { User,Counter, generateCustomerId,generateBookingId ,resetCounter}; diff --git a/src/routes/tankersRoute.js b/src/routes/tankersRoute.js index b1a5ff73..20c46847 100644 --- a/src/routes/tankersRoute.js +++ b/src/routes/tankersRoute.js @@ -146,11 +146,21 @@ module.exports = function (fastify, opts, next) { fastify.route({ method: "POST", - url: "/api/bookingData", + url: "/api/bookingData/:customerId", schema: { tags: ["Supplier"], description: "This is for storing booking data of a Tanker", summary: "This is for storing booking data of a Tanker", + params: { + required: ["customerId"], + type: "object", + properties: { + customerId: { + type: "string", + description: "customerId", + }, + }, + }, body: { type: "object", properties: {