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
| <!--route.js--->
const express = require('express')
const isAuth = (req, res, next) => {passport.authenticate('jwt', { session: false, failureRedirect: '/user-login' })(req, res, next)}
const JWT = require('jsonwebtoken')
const router = express.Router()
const passport = require('passport')
const UserProducts = require('../models/userproduct');
const Product = require('../models/product');
const User = require('../models/user');
const DiscountCodes = require('../models/discountCodes')
const { v4: uuidv4 } = require('uuid');
let delay = 1.5;
router.get('/store', isAuth, async (req, res) => {
try{
const all = await Product.find()
const products = []
for(let p of all) {
products.push({ productId: p.productId, Name: p.Name, Description: p.Description, Cost: p.Cost })
}
const user = await User.findById(req.user.userId);
return res.render('store', { Authenticated: true, Balance: user.Balance, Product: products})
} catch{
return res.render('error', { Authenticated: true, message: 'Error during request' })
}
})
router.get('/redeem', isAuth, async (req, res) => {
try {
const user = await User.findById(req.user.userId);
if (!user) {
return res.render('error', { Authenticated: true, message: 'User not found' });
}
// Now handle the DiscountCode (Gift Card)
let { discountCode } = req.query;
if (!discountCode) {
return res.render('error', { Authenticated: true, message: 'Discount code is required!' });
}
const discount = await DiscountCodes.findOne({discountCode})
if (!discount) {
return res.render('error', { Authenticated: true, message: 'Invalid discount code!' });
}
// Check if the voucher has already been redeemed today
const today = new Date();
const lastRedemption = user.lastVoucherRedemption;
if (lastRedemption) {
const isSameDay = lastRedemption.getFullYear() === today.getFullYear() &&
lastRedemption.getMonth() === today.getMonth() &&
lastRedemption.getDate() === today.getDate();
if (isSameDay) {
return res.json({success: false, message: 'You have already redeemed your gift card today!' });
}
}
// Apply the gift card value to the user's balance
const { Balance } = await User.findById(req.user.userId).select('Balance');
user.Balance = Balance + discount.value;
// Introduce a slight delay to ensure proper logging of the transaction
// and prevent potential database write collisions in high-load scenarios.
new Promise(resolve => setTimeout(resolve, delay * 1000));
user.lastVoucherRedemption = today;
await user.save();
return res.json({
success: true,
message: 'Gift card redeemed successfully! New Balance: ' + user.Balance // Send success message
});
} catch (error) {
console.error('Error during gift card redemption:', error);
return res.render('error', { Authenticated: true, message: 'Error redeeming gift card'});
}
});
router.get('/redeemVoucher', isAuth, async (req, res) => {
const user = await User.findById(req.user.userId);
return res.render('redeemVoucher', { Authenticated: true, Balance: user.Balance })
});
router.get('/register-user', (req, res) => {
return res.render('register-user')
})
router.post('/register-user', (req, res, next) => {
let { username , password } = req.body
if (username == null || password == null){
return next({message: "Error"})
}
if(!username || !password) {
return next({ message: 'You forgot to enter your credentials!' })
}
if(password.length <= 2) {
return next({ message: 'Please choose a longer password.. :-(' })
}
User.register(new User({ username }), password, (err, user) => {
if(err && err.toString().includes('registered')) {
return next({ message: 'Username taken' })
} else if(err) {
return next({ message: 'Error during registration' })
}
const jwtoken = JWT.sign({userId: user._id}, process.env.JWT_SECRET, {algorithm: 'HS256',expiresIn: '10h'})
res.cookie('jwt', jwtoken, { httpOnly: true })
return res.json({success: true, message: 'Account registered.'})
})
})
router.get('/user-login', (req, res) => {
return res.render('user-login')
})
router.post('/user-login', (req, res, next) => {
passport.authenticate('user-local', (_, user, err) => {
if(err) {
return next({ message: 'Error during login' })
}
const jwtoken = JWT.sign({userId: user._id}, process.env.JWT_SECRET, {algorithm: 'HS256',expiresIn: '10h'})
res.cookie('jwt', jwtoken, { httpOnly: true })
return res.json({
success: true,
message: 'Logged'
})
})(req, res, next)
})
router.get('/user-logout', (req, res) => {
res.clearCookie('jwt')
res.redirect('/')
})
function parseCost(cost) {
if (cost.toLowerCase() === "free") {
return 0;
}
const match = cost.match(/\d+/); // Extract numbers from the string
return match ? parseInt(match[0], 10) : NaN; // Return the number or NaN if not found
}
router.post('/store', isAuth, async (req, res, next) => {
const productId = req.body.productId;
if (!productId) {
return next({ message: 'productId is required.' });
}
try {
// Find the product by Name
const all = await Product.find()
product = null
for(let p of all) {
if(p.productId === productId){
product = p
}
}
if (!product) {
return next({ message: 'Product not found.' });
}
// Parse the product cost into a numeric value
let productCost = parseCost(product.Cost);
if (isNaN(productCost)) {
return next({ message: 'Invalid product cost format.' });
}
// Fetch the authenticated user
const user = await User.findById(req.user.userId);
if (!user) {
return next({ message: 'User not found.' });
}
// Check if the user can afford the product
if (user.Balance >= productCost) {
// Generate a UUID v4 as a transaction ID
const transactionId = uuidv4();
// Deduct the product cost and save the user
user.Balance -= productCost;
await user.save();
// Create a new UserProduct entry
const userProduct = new UserProducts({
transactionId: transactionId,
user: user._id,
productId: product._id, // Reference the product purchased
});
await userProduct.save(); // Save the UserProduct entry
// Add the UserProduct reference to the user's ownedproducts array
if (!user.ownedproducts.includes(userProduct._id)) {
user.ownedproducts.push(userProduct._id);
await user.save(); // Save the updated user
}
// Prepare the response data
const responseData = {
success: true,
message: `Product correctly bought! Remaining balance: ${user.Balance}`,
product: {
Name: product.Name,
Description: product.Description,
},
};
if (product.productId === 4) {
responseData.product.FLAG = product.FLAG || 'No flag available';
}
return res.json(responseData);
} else {
return res.json({success: false, message: 'Insufficient balance to purchase this product.' });
}
} catch (error) {
console.error('Error during product payment:', error);
return res.json({success: false, message: 'An error occurred during product payment.' });
}
});
router.get('/', (req, res, next) => {
passport.authenticate('jwt', async (err, r) => {
let { userId } = r
if (!userId) {
return res.render('home', {
Authenticated: false
})
}
try {
// Fetch the user and populate the ownedproducts, which are UserProducts
const user = await User.findById(userId)
.populate({
path: 'ownedproducts', // Populate the UserProducts
populate: {
path: 'productId', // Populate the product details
model: 'Product' // The model to fetch the product details
}
})
.exec()
// Map the owned products with product details and transactionId
const ownedproducts = user.ownedproducts.map((userProduct) => {
const product = userProduct.productId; // Access the populated product details
return {
Name: product.Name, // Name of the product
Description: product.Description, // Description of the product
Cost: product.Cost, // Cost of the product
FLAG: product.FLAG || null, // Flag (only exists for certain products)
transactionId: userProduct.transactionId // Add transactionId here
}
})
return res.render('home', {
Authenticated: true,
username: user.username,
Balance: user.Balance, // Pass balance as a variable to the template
ownedproducts: ownedproducts // Pass the products with transactionId
})
} catch (err) {
console.error('Error fetching user or products:', err)
return next(err) // Handle any errors (e.g., database issues)
}
})(req, res, next)
})
router.use((err, req, res, next) => {
res.status(err.status || 400).json({
success: false,
error: err.message || 'Invalid Request',
})
})
module.exports = router
|