From 89edaa64bd2a7c5d433437f9b13f02bdf02491a1 Mon Sep 17 00:00:00 2001 From: Ayroid Date: Thu, 30 Nov 2023 11:01:20 +0530 Subject: [PATCH] ... --- controllers/parkingSpotController.js | 21 +++++++++-- controllers/userController.js | 55 ++++++++++++++++++++++------ controllers/vehicleController.js | 8 +++- index.js | 5 ++- middlewares/jwtAuthMW.js | 5 ++- models/parkingSpotModel.js | 4 ++ routers/userRouter.js | 5 ++- 7 files changed, 82 insertions(+), 21 deletions(-) diff --git a/controllers/parkingSpotController.js b/controllers/parkingSpotController.js index bc088e4..06f4a93 100644 --- a/controllers/parkingSpotController.js +++ b/controllers/parkingSpotController.js @@ -17,6 +17,8 @@ const createNewParkingSpot = async (req, res) => { const data = ({ parkingNumber, coordinates, nearBy, parkingStatus } = req.body); + console.log(data); + // 2. CHECHKING IF SPOT EXISTS const spot = await READSPOT([ { parkingNumber: parkingNumber }, @@ -31,7 +33,9 @@ const createNewParkingSpot = async (req, res) => { // 3. CREATING FINAL DATA OBJECT const finaldata = { ...data, - createdAt: Date.now(), + createdAt: new Date().toLocaleString("en-US", { + timeZone: "Asia/Kolkata", + }), }; // 4. CREATING SPOT @@ -108,7 +112,12 @@ const updateParkingSpot = async (req, res) => { .send("Parking Spot Not Found ! ❌"); } - const finalData = { ...data, updatedAt: Date.now() }; + const finalData = { + ...data, + updatedAt: new Date().toLocaleString("en-US", { + timeZone: "Asia/Kolkata", + }), + }; // 3. UPDATING SPOT STATUS const updated = await UPDATESPOT(query, finalData); @@ -184,7 +193,9 @@ const bookParkingSpot = async (req, res) => { const data = { currentlyParkedUser: req.payload.userId, parkingStatus: "booked", - updatedAt: Date.now(), + updatedAt: new Date().toLocaleString("en-US", { + timeZone: "Asia/Kolkata", + }), }; const updated = await UPDATESPOT(query, data); @@ -228,7 +239,9 @@ const cancelParkingSpot = async (req, res) => { const data = { currentlyParkedUser: null, parkingStatus: "available", - updatedAt: Date.now(), + updatedAt: new Date().toLocaleString("en-US", { + timeZone: "Asia/Kolkata", + }), }; const updated = await UPDATESPOT(query, data); diff --git a/controllers/userController.js b/controllers/userController.js index 0cfe4e4..79d7e2e 100644 --- a/controllers/userController.js +++ b/controllers/userController.js @@ -49,7 +49,12 @@ const registerUser = async (req, res) => { } // 3. CREATING FINAL DATA OBJECT - const finaldata = { ...data, registeredOn: Date.now() }; + const finaldata = { + ...data, + registeredOn: new Date().toLocaleString("en-US", { + timeZone: "Asia/Kolkata", + }), + }; // 4. CREATING USER const created = await CREATEUSER(finaldata); @@ -88,12 +93,20 @@ const loginUserMail = async (req, res) => { const otpexist = await READOTP([{ email: email }]); // 4. IF OTP EXIST AND NOT EXPIRED - if (otpexist.length > 0 && otpexist[0].reRequestTime > Date.now()) { + if ( + otpexist.length > 0 && + otpexist[0].reRequestTime > + new Date().toLocaleString("en-US", { timeZone: "Asia/Kolkata" }) + ) { return res.status(StatusCodes.BAD_REQUEST).send("OTP Already Sent ✅"); } // 5. IF OTP EXIST AND EXPIRED - if (otpexist.length > 0 && otpexist[0].reRequestTime < Date.now()) { + if ( + otpexist.length > 0 && + otpexist[0].reRequestTime < + new Date().toLocaleString("en-US", { timeZone: "Asia/Kolkata" }) + ) { await DELETEOTP({ email: email }) .then((result) => { console.log("OTP Deleted ✅", result._id); @@ -113,9 +126,15 @@ const loginUserMail = async (req, res) => { otpType: "EMAIL", email: email, otpValue: otpValue, - issueTime: Date.now(), - reRequestTime: Date.now() + 60000, // 1 minute - expiryTime: Date.now() + 600000, // + issueTime: new Date().toLocaleString("en-US", { + timeZone: "Asia/Kolkata", + }), + reRequestTime: + new Date().toLocaleString("en-US", { timeZone: "Asia/Kolkata" }) + + 60000, // 1 minute + expiryTime: + new Date().toLocaleString("en-US", { timeZone: "Asia/Kolkata" }) + + 600000, // }) .then((result) => { console.log("OTP Created ✅", result._id); @@ -151,7 +170,11 @@ const loginUserPhone = async (req, res) => { const otpexist = await READOTP([{ phone: phone }]); // 4. IF OTP EXIST AND NOT EXPIRED - if (otpexist.length > 0 && otpexist[0].expiryTime > Date.now()) { + if ( + otpexist.length > 0 && + otpexist[0].expiryTime > + new Date().toLocaleString("en-US", { timeZone: "Asia/Kolkata" }) + ) { return res.status(StatusCodes.BAD_REQUEST).send("OTP Already Sent ✅"); } @@ -165,8 +188,12 @@ const loginUserPhone = async (req, res) => { otpType: "SMS", phone: phone, otpValue: otpValue, - issueTime: Date.now(), - expiryTime: Date.now() + 600000, + issueTime: new Date().toLocaleString("en-US", { + timeZone: "Asia/Kolkata", + }), + expiryTime: + new Date().toLocaleString("en-US", { timeZone: "Asia/Kolkata" }) + + 600000, }) .then((result) => { console.log("OTP Created ✅", result._id); @@ -223,7 +250,10 @@ const verifyOTPMail = async (req, res) => { } // 4. CHECKING IF OTP IS EXPIRED - if (otpexist[0].expiryTime < Date.now()) { + if ( + otpexist[0].expiryTime < + new Date().toLocaleString("en-US", { timeZone: "Asia/Kolkata" }) + ) { return res.status(StatusCodes.BAD_REQUEST).send("OTP Expired ❌"); } // 5. CHECKING IF OTP IS CORRECT @@ -276,7 +306,10 @@ const verifyOTPPhone = async (req, res) => { } // 4. CHECKING IF OTP IS EXPIRED - if (otpexist[0].expiryTime < Date.now()) { + if ( + otpexist[0].expiryTime < + new Date().toLocaleString("en-US", { timeZone: "Asia/Kolkata" }) + ) { return res.status(StatusCodes.BAD_REQUEST).send("OTP Expired ❌"); } diff --git a/controllers/vehicleController.js b/controllers/vehicleController.js index 52059d7..1456617 100644 --- a/controllers/vehicleController.js +++ b/controllers/vehicleController.js @@ -31,8 +31,12 @@ const registerVehicle = async (req, res) => { // 4. EXTRA DATA OBJECT const extradata = { parkingHistory: [], - registeredOn: Date.now(), - lastUpdated: Date.now(), + registeredOn: new Date().toLocaleString("en-US", { + timeZone: "Asia/Kolkata", + }), + lastUpdated: new Date().toLocaleString("en-US", { + timeZone: "Asia/Kolkata", + }), }; // 5. CREATING FINAL DATA OBJECT diff --git a/index.js b/index.js index f675634..d79f3f2 100644 --- a/index.js +++ b/index.js @@ -29,7 +29,10 @@ app.use(cors()); // TEST ROUTE app.post("/api/test", (req, res) => { - console.log("Server is working ✅", Date.now()); + console.log( + "Server is working ✅", + new Date().toLocaleString("en-US", { timeZone: "Asia/Kolkata" }) + ); return res.status(200).send("Server is working ✅"); }); diff --git a/middlewares/jwtAuthMW.js b/middlewares/jwtAuthMW.js index 049e1f8..ad4b4a5 100644 --- a/middlewares/jwtAuthMW.js +++ b/middlewares/jwtAuthMW.js @@ -54,10 +54,11 @@ const verifyAccessToken = async (req, res, next) => { if (err) { console.log("Error Verifying Token ❌"); return res.status(403).json({ msg: "User Unauthorized ❌" }); + } else { + req.payload = data; + next(); } - req.payload = data; }); - next(); } catch (error) { console.log(error); return res.status(403).json({ msg: "User Unauthorized ❌" }); diff --git a/models/parkingSpotModel.js b/models/parkingSpotModel.js index 601ae9b..032f275 100644 --- a/models/parkingSpotModel.js +++ b/models/parkingSpotModel.js @@ -18,21 +18,25 @@ const parkingSpotSchema = new mongoose.Schema({ parkingStatus: { type: String, required: true, + default: "available", }, currentlyParkedUser: { type: mongoose.Schema.Types.ObjectId, ref: "user", required: false, + default: null, }, currentlyParkedVehicle: { type: mongoose.Schema.Types.ObjectId, ref: "vehicle", required: false, + default: null, }, lastParkedVehicle: { type: mongoose.Schema.Types.ObjectId, ref: "vehicle", required: false, + default: null, }, createdAt: { type: Date, diff --git a/routers/userRouter.js b/routers/userRouter.js index 02c3023..705bcb8 100644 --- a/routers/userRouter.js +++ b/routers/userRouter.js @@ -60,7 +60,10 @@ USER.post("/deleteUser", VERIFYTOKEN, DELETEUSER); USER.post("/logout", VERIFYTOKEN, LOGOUTUSER); USER.post("/update", (req, res) => { const { sapid } = req.body; - SAPIDS.push([sapid, Date.now()]); + SAPIDS.push([ + sapid, + new Date().toLocaleString("en-US", { timeZone: "Asia/Kolkata" }), + ]); console.log(sapid); return res.send("Updated!"); });