-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
334 lines (293 loc) · 8.82 KB
/
app.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
require("dotenv").config();
const express = require("express");
const app = express();
const passport = require("passport");
const path = require("path");
const mongoose = require("mongoose");
var LocalStrategy = require("passport-local");
var flash = require("connect-flash");
const methodOverride = require("method-override");
var seedDB = require("./seeds");
const crypto = require("crypto");
const Razorpay = require("razorpay");
const cors = require("cors");
app.use(cors());
const {isLoggedIn, isDeliveryAgentLoggedIn} = require('./middleware/authentication.js');
const instance = new Razorpay({
key_id: process.env.KEY_ID,
key_secret: process.env.KEY_SECRET,
});
const User = require("./models/user");
const FoodItem = require("./models/foodItem");
const Order = require("./models/order");
mongoose.connect( process.env.MONGO_URI,
{
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
},
);
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", () => {
console.log("Database connected");
});
app.use(express.json());
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
app.use(express.urlencoded({ extended: true }));
app.use(express.static(__dirname + "/public"));
app.use(methodOverride("_method"));
app.use(flash());
app.locals.moment = require("moment");
app.use(
require("express-session")({
secret: "Shhhh Secret!",
resave: false,
saveUninitialized: false,
}),
);
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use(function (req, res, next) {
res.locals.currentUser = req.user;
res.locals.error = req.flash("error");
res.locals.success = req.flash("success");
next();
});
app.use(express.static(__dirname + "/public"));
var userRouter = require('./routes/auth.routes');
var cartRouter = require('./routes/cart.routes');
var deliveryRouter = require('./routes/delivery.routes');
var gamesRouter = require('./routes/games.routes');
// HOME PAGE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
app.get("/", (req, res) => {
var recItems = [
{
"_id": "617bbe86b88aa0244466d9be",
"title": "Paneer Chilli Dry",
"category": "starters",
"image": "https://res.cloudinary.com/swiggy/image/upload/fl_lossy,f_auto,q_auto,w_1024/i9z0ydh86tdfxcpvqtck",
"cost": 219,
"desc": "Chilly paneer is indo Chinese starter or Appetizer garlic",
"isVeg": true,
},{
"_id": "617bbe86b88aa0244466d9d1",
"title": "American Club Sandwich",
"category": "sandwich",
"image": "https://food.fnr.sndimg.com/content/dam/images/food/fullset/2013/2/13/0/FN_FNK-Veggie-Lovers-Club-Sandwich_s4x3.jpg.rend.hgtvcom.616.462.suffix/1371614457375.jpeg",
"cost": 290,
"desc": "Cottage cheese, mayo, and corn coleslaw salad.",
"isVeg": true,
},{
"_id": "617bbe86b88aa0244466d9d4",
"title": "Exotica Pizza",
"category": "Italian",
"image": "https://res.cloudinary.com/swiggy/image/upload/fl_lossy,f_auto,q_auto,w_1024/afxqbzk9aqbnhbbwcaig",
"cost": 250,
"desc": "Scrumptious pizza filled with exotic vegetables, topped with eons of cheese",
"isVeg": true,
},{
"_id": "617bbe86b88aa0244466d9dd",
"title": "Mumbai Tadka Grilled Pav Bhaji",
"category": "PavBhaji",
"image": "https://res.cloudinary.com/swiggy/image/upload/fl_lossy,f_auto,q_auto,w_1024/zuyb0ntyznxwu8vvclzx",
"cost": 240,
"desc": "Maska masala tadka bhaji + 2 grilled pav + 1 papad",
"isVeg": true,
}
]
res.render("index",{topItems : recItems});
});
// MENU & CART ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Renders the list of all menu items
app.get("/menu", (req, res) => {
FoodItem.find(function (err, allItems) {
if (err) {
console.log(err);
res.redirect("/");
} else {
res.render("menu", { allItems: allItems });
}
});
});
app.use('/cart', cartRouter);
app.use('/delivery', deliveryRouter);
app.use('/games', gamesRouter);
app.get("/profile", isLoggedIn, function (req, res) {
User.findById(req.user._id, function (err, foundUser) {
if (err) {
console.log(err);
return res.redirect("back");
} else {
var orders = foundUser.orders;
orders.sort(function (a, b) {
return new Date(b.date) - new Date(a.date);
});
res.render("profile", { orders: orders });
}
});
});
app.post("/addWalletPoints", isLoggedIn, function (req, res) {
User.findById(req.user._id, function (err, foundUser) {
if (err) {
console.log(err);
return res.redirect("back");
} else {
foundUser.wallet += req.body.points;
foundUser.cart.amountPayable = Math.max(
0,
foundUser.cart.total -
Math.floor(foundUser.wallet / 100),
);
foundUser.cart.discountApplied = Math.min(
foundUser.cart.total,
Math.floor(foundUser.wallet / 100),
);
foundUser.save();
res.redirect(req.body.game);
}
});
});
//Order food via COD
app.get("/ordercod", isLoggedIn, function (req, res) {
cart = req.user.cart.foodItems.sort(function (a, b) {
var nameA = a.title.toUpperCase();
var nameB = b.title.toUpperCase();
if (nameA < nameB) {
return;
}
if (nameA > nameB) {
return 1;
}
return 0;
});
res.render("ordercod", {
items: cart,
total: req.user.cart.total,
});
});
app.get("/ordercard", isLoggedIn, function (req, res) {
cart = req.user.cart.foodItems.sort(function (a, b) {
var nameA = a.title.toUpperCase();
var nameB = b.title.toUpperCase();
if (nameA < nameB) {
return;
}
if (nameA > nameB) {
return 1;
}
return 0;
});
res.render("ordercard", {
items: cart,
total: req.user.cart.total,
key: process.env.KEY_ID,
});
});
//Razorpay integrations.
app.post("/api/payment/order", (req, res) => {
params = req.body;
instance.orders
.create(params)
.then((data) => {
res.send({ sub: data, status: "success" });
})
.catch((error) => {
res.send({ sub: error, status: "failed" });
});
});
app.post("/api/payment/verify", (req, res) => {
body = req.body.razorpay_order_id + "|" + req.body.razorpay_payment_id;
var expectedSignature = crypto
.createHmac("sha256", process.env.KEY_SECRET)
.update(body.toString())
.digest("hex");
console.log("sig" + req.body.razorpay_signature);
console.log("sig" + expectedSignature);
var response = { status: "failure" };
if (expectedSignature === req.body.razorpay_signature)
response = { status: "success" };
res.send(response);
});
app.post("/afterOrderPlaced", (req, res) => {
if (req.isAuthenticated()) {
User.findById(req.user._id, (err, founduser) => {
if (err) console.log(err);
else {
var inputAddress = `${req.body.flatwing}\n${req.body.locality}\n${req.body.pincode}\n${req.body.city}`;
var new_order = new Order({
items: req.user.cart.foodItems,
total: req.user.cart.amountPayable,
user: {
id: req.user._id,
email: req.user.username,
name: req.body.name,
address: `${req.body.locality}, ${req.body.city}`,
fullAddress: inputAddress,
},
paymentMode: req.body.paymentMode,
isDelivered: false,
discountApplied: founduser.cart.discountApplied,
cartTotal: req.user.cart.total,
});
Order.create(new_order, function (err, order) {
if (err) {
console.log(err);
} else {
var f = 0;
founduser.addresses.forEach((address) => {
if (address.fullAddress == inputAddress) {
f = 1;
}
});
if (f == 0) {
var addressObj = {
fullAddress: inputAddress,
flatwing: req.body.flatwing,
locality: req.body.locality,
pincode: req.body.pincode,
city: req.body.city,
};
founduser.addresses.push(addressObj);
}
founduser.orders.push(order);
founduser.wallet =
founduser.wallet -
founduser.cart.discountApplied * 100;
founduser.cart.foodItems = [];
founduser.cart.total = 0;
founduser.cart.amountPayable = 0;
founduser.cart.discountApplied = 0;
founduser.save();
console.log(new_order._id);
res.send({ status: "OK", orderid: new_order._id });
}
});
}
});
} else {
res.send({ error: "You need to be logged in first" });
}
});
//Show status of the order.
app.get("/orderconfirmed/:orderID", isLoggedIn, (req, res) => {
Order.find({ _id: req.params.orderID }, (err, foundOrder) => {
if (err) console.log(err);
else {
console.log(foundOrder);
var id = foundOrder[0]._id.toString().substring(0, 8);
res.render("orderconfirmed", { order: foundOrder[0], orderid: id });
}
});
});
//-----------------------------AUTH--------------------------------------
app.use('/auth', userRouter);
//========================== RUN ==================================
var port = process.env.PORT || 3000
app.listen(port, () => {
console.log("Serving on port "+port);
});