-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathuser.controller.js
205 lines (191 loc) · 5.46 KB
/
user.controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const userService = require("../services/user.service");
const bookingService = require("../services/booking.service");
const garageService=require("../services/garage.service");
const slotService=require("../services/slot.service");
const jwt = require("jsonwebtoken");
let options = {
path: "/",
sameSite: true,
maxAge: 1000 * 60 * 60 * Number(process.env.EXPIRY),
httpOnly: true,
};
let options_otp = {
path: "/",
sameSite: true,
maxAge: 1000 * 60 * 5,
httpOnly: true,
};
const renderRegister = (req, res) => {
res.render("users/register");
};
const renderLogin = (req, res) => {
res.render("users/login");
};
const renderDashboard = async (req, res) => {
var bookings = await bookingService.FindByUser(req.body.user_id);
for (let booking of bookings) {
if (
booking.status !== "Completed" &&
booking.end_time <= new Date().getTime() / 1000
) {
await bookingService.completeBooking(booking._id);
}
}
res.render("users/dashboard", { body: req.body, bookings: bookings });
};
const renderAddMoney = (req, res) => {
res.render("users/addmoney", { money: req.body.money, body: req.body });
};
const renderVerify = async (req, res) => {
if (req.body.verified === true) {
res.send("Already Verified");
} else {
var result = await sendOtp(req.body.username);
res.cookie("otp", result.token, options_otp);
res.render("users/verify", { email: req.body.email });
}
};
const register = async (req, res) => {
try {
const result = await userService.Register(req.body);
res.cookie("isloggedin", result.token, options);
req.flash("alert", "Registered Successfully. Please Verify your Email.");
res.redirect("/users/verify");
} catch (err) {
req.flash("err", "Error :" + err);
res.redirect("/users/register");
}
};
const sendOtp = async (username) => {
try {
var gen_otp = await userService.generateOtp(username);
return gen_otp;
} catch (err) {
return err;
}
};
const login = async (req, res) => {
try {
const result = await userService.Login(
req.body.username,
req.body.password
);
res.cookie("isloggedin", result.token, options);
req.flash("success", "Logged in Successfully.");
res.redirect("/users/dashboard");
} catch (err) {
req.flash("err", "Error :" + err);
res.redirect("/users/login");
}
};
const logout = (req, res) => {
res.clearCookie("isloggedin");
req.flash("success", "Logged out Successfully.");
res.redirect("/");
};
const verify = async (req, res) => {
var otp = req.body.otp;
let token = req.cookies["otp"];
let decoded = jwt.verify(token, process.env.JWT_SECRET);
if (!decoded) return res.json("Expired or Invalid login token");
else {
if (decoded.otp === otp) {
await userService.verified(req.body.username);
res.clearCookie("otp");
req.flash("success", "Verification Complete. Welcome Aboard.");
res.redirect("/users/dashboard");
} else {
res.send("reenter otp");
}
}
};
const renderImage = (req, res) => {
res.render("users/uploadimage", { body: req.body });
};
const uploadImage = async (req, res) => {
var path = req.file["path"];
var userid = req.body.user_id;
console.log(path + " " + userid);
try {
await userService.updateImage(userid, path);
req.flash("success", "Image Uploaded Successfully");
res.redirect("/users/dashboard");
} catch (err) {
req.flash("err", "Error :" + err);
res.redirect("/users/changeimage");
}
};
const addMoney = async (req, res) => {
var add_money = req.body.added_money;
try {
await userService.addMoney(req.body.user_id, add_money);
req.flash("success", "Money Added Successfully");
res.redirect("/users/dashboard");
} catch (err) {
req.flash("err", "Error :" + err);
res.redirect("/users/addmoney");
}
};
const apiOtp = (req, res) => {
var str = req.params.value;
let token = req.cookies["otp"];
let decoded = jwt.verify(token, process.env.JWT_SECRET);
if (decoded.otp === str) res.send("1");
else res.send("0");
};
const resendOTP = (req, res) => {
req.flash("alert", "Your OTP has been resent successfully to your email.");
res.redirect("/users/verify");
};
const renderTransactions = async (req, res) => {
var transactions = await userService.getTransactions(req.body.user_id);
res.render("users/transactions", {
body: req.body,
transactions: transactions,
});
};
const renderBookings = async (req, res) => {
var bookings = await bookingService.FindByUser(req.body.user_id);
for (let booking of bookings) {
if (
booking.status !== "Completed" &&
booking.end_time <= new Date().getTime() / 1000
) {
await bookingService.completeBooking(booking._id);
}
}
res.render("users/bookings", { body: req.body, bookings: bookings });
};
const renderBooking=async(req,res)=>
{
var id=req.params.id;
var booking=await bookingService.FindBooking(id);
if(!booking){
req.flash("err", "No Booking Found");
res.redirect("/users/dashboard");
}
else{
var slot=await slotService.FindSlot(booking.slot_id);
var garage=await garageService.FindGarage(slot.garage_id);
res.render("users/mybooking",{ body: req.body, booking: booking ,slot:slot,garage:garage});
}
}
module.exports = {
renderLogin,
renderRegister,
register,
login,
renderDashboard,
logout,
renderVerify,
verify,
renderImage,
uploadImage,
renderAddMoney,
addMoney,
apiOtp,
resendOTP,
renderTransactions,
renderBookings,
renderBooking
};