made change in booking id and linked booking data with customer id

master
varun 3 years ago
parent ce014e165c
commit 8dd793c117

@ -1,6 +1,8 @@
const { Tanker, Tankerbooking,Bore,GovtPipeLine } = require('../models/tankers') 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"); const boom = require("boom");
@ -97,26 +99,25 @@ exports.deleteTankerInfo = async (req, reply) => {
} }
}; };
let bookingCount = 0
exports.tankerBooking = async (req, reply) => { exports.tankerBooking = async (req, reply) => {
try { try {
var customerId = req.params.customerId;
var b_id = await generateBookingId()
const date = new Date() const date = new Date()
const year = date.getFullYear() const year = date.getFullYear()
const month = (date.getMonth() + 1).toString().padStart(2, '0') const month = (date.getMonth() + 1).toString().padStart(2, '0')
const day = date.getDate().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}${b_id}`
const bookingId = `ARM${year}${month}${day}${count}`
bookingCount = (bookingCount + 1) % 100;
var booking_id = await Tankerbooking.findOne({ bookingid: bookingId}) var booking_id = await Tankerbooking.findOne({ bookingid: bookingId})
if(booking_id){ if(booking_id){
await generateCustomerId()
} }
else { else {
bookingsData = { bookingsData = {
customerId:customerId,
bookingid : bookingId, bookingid : bookingId,
typeofwater: req.body.typeofwater, typeofwater: req.body.typeofwater,
capacity: req.body.capacity, capacity: req.body.capacity,
@ -130,7 +131,8 @@ exports.tankerBooking = async (req, reply) => {
checkFormEncoding = isUserFormUrlEncoded(req); checkFormEncoding = isUserFormUrlEncoded(req);
if (checkFormEncoding.isUserFormUrlEncoded) { if (checkFormEncoding.isUserFormUrlEncoded) {
usertobeInserted = checkFormEncoding.tankersBookingData; usertobeInserted = checkFormEncoding.tankersBookingData;
console.log("thsi true url string");s console.log("thsi true url string");
tankersBookingData.customerId = customerId;
tankersBookingData.bookingid = bookingId; tankersBookingData.bookingid = bookingId;
tankersBookingData.capacity = usertobeInserted.capacity; tankersBookingData.capacity = usertobeInserted.capacity;
tankersBookingData.typeofwater = usertobeInserted.typeofwater; tankersBookingData.typeofwater = usertobeInserted.typeofwater;

@ -9,7 +9,7 @@ const libphonenumberjs = require("libphonenumber-js");
const boom = require("boom"); const boom = require("boom");
// Get Data Models // 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 User = require("../models/User");
const customJwtAuth = require("../customAuthJwt"); const customJwtAuth = require("../customAuthJwt");
@ -163,7 +163,7 @@ exports.editCuurentUserInfo = async (req, reply) => {
exports.addUser = async (req, reply) => { exports.addUser = async (req, reply) => {
try { try {
// await resetCounter();//to set customer id back to 0 // 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 building= ((req.body.buildingName).slice(0, 3)).toUpperCase();
var customer_id = `AWS${building}${c_id}` var customer_id = `AWS${building}${c_id}`
// 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);

@ -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 generateBookingId = async () => {
const result = await Counter.findOneAndUpdate( var result = await Counter.findOneAndUpdate(
{ _id: 'booking_id' }, { _id: 'booking_id' },
{ $inc: { seq: 1 } }, { $inc: { seq: 1 } },
{ upsert: true, new: true } { upsert: true, new: true }
@ -110,4 +120,4 @@ const User = mongoose.model("User", userSchema);
//module.exports = mongoose.model("User", userSchema); //module.exports = mongoose.model("User", userSchema);
module.exports = { User,Counter, generateBookingId ,resetCounter}; module.exports = { User,Counter, generateCustomerId,generateBookingId ,resetCounter};

@ -146,11 +146,21 @@ module.exports = function (fastify, opts, next) {
fastify.route({ fastify.route({
method: "POST", method: "POST",
url: "/api/bookingData", url: "/api/bookingData/:customerId",
schema: { schema: {
tags: ["Supplier"], tags: ["Supplier"],
description: "This is for storing booking data of a Tanker", description: "This is for storing booking data of a Tanker",
summary: "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: { body: {
type: "object", type: "object",
properties: { properties: {

Loading…
Cancel
Save