Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayroid committed Nov 30, 2023
1 parent 2b8db09 commit 89edaa6
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 21 deletions.
21 changes: 17 additions & 4 deletions controllers/parkingSpotController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
55 changes: 44 additions & 11 deletions controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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 ✅");
}

Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ❌");
}

Expand Down
8 changes: 6 additions & 2 deletions controllers/vehicleController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ✅");
});

Expand Down
5 changes: 3 additions & 2 deletions middlewares/jwtAuthMW.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ❌" });
Expand Down
4 changes: 4 additions & 0 deletions models/parkingSpotModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion routers/userRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
});
Expand Down

0 comments on commit 89edaa6

Please sign in to comment.