@feardread/fear 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/FEAR.js +459 -0
- package/FEARServer.js +280 -0
- package/controllers/agent.js +438 -0
- package/controllers/auth/index.js +345 -0
- package/controllers/auth/token.js +50 -0
- package/controllers/blog.js +105 -0
- package/controllers/brand.js +10 -0
- package/controllers/cart.js +425 -0
- package/controllers/category.js +9 -0
- package/controllers/coupon.js +63 -0
- package/controllers/crud/crud.js +508 -0
- package/controllers/crud/index.js +36 -0
- package/controllers/email.js +34 -0
- package/controllers/enquiry.js +65 -0
- package/controllers/events.js +9 -0
- package/controllers/order.js +125 -0
- package/controllers/payment.js +31 -0
- package/controllers/product.js +147 -0
- package/controllers/review.js +247 -0
- package/controllers/tag.js +10 -0
- package/controllers/task.js +10 -0
- package/controllers/upload.js +41 -0
- package/controllers/user.js +401 -0
- package/index.js +7 -0
- package/libs/agent/index.js +561 -0
- package/libs/agent/modules/ai/ai.js +285 -0
- package/libs/agent/modules/ai/chat.js +518 -0
- package/libs/agent/modules/ai/config.js +688 -0
- package/libs/agent/modules/ai/operations.js +787 -0
- package/libs/agent/modules/analyze/api.js +546 -0
- package/libs/agent/modules/analyze/dorks.js +395 -0
- package/libs/agent/modules/ccard/README.md +454 -0
- package/libs/agent/modules/ccard/audit.js +479 -0
- package/libs/agent/modules/ccard/checker.js +674 -0
- package/libs/agent/modules/ccard/payment-processors.json +16 -0
- package/libs/agent/modules/ccard/validator.js +629 -0
- package/libs/agent/modules/code/analyzer.js +303 -0
- package/libs/agent/modules/code/jquery.js +1093 -0
- package/libs/agent/modules/code/react.js +1536 -0
- package/libs/agent/modules/code/refactor.js +499 -0
- package/libs/agent/modules/crypto/exchange.js +564 -0
- package/libs/agent/modules/net/proxy.js +409 -0
- package/libs/agent/modules/security/cve.js +442 -0
- package/libs/agent/modules/security/monitor.js +360 -0
- package/libs/agent/modules/security/scanner.js +300 -0
- package/libs/agent/modules/security/vulnerability.js +506 -0
- package/libs/agent/modules/security/web.js +465 -0
- package/libs/agent/modules/utils/browser.js +492 -0
- package/libs/agent/modules/utils/colorizer.js +285 -0
- package/libs/agent/modules/utils/manager.js +478 -0
- package/libs/cloud/index.js +228 -0
- package/libs/config/db.js +21 -0
- package/libs/config/validator.js +82 -0
- package/libs/db/index.js +318 -0
- package/libs/emailer/imap.js +126 -0
- package/libs/emailer/info.js +41 -0
- package/libs/emailer/smtp.js +77 -0
- package/libs/handler/async.js +3 -0
- package/libs/handler/error.js +66 -0
- package/libs/handler/index.js +161 -0
- package/libs/logger/index.js +49 -0
- package/libs/logger/morgan.js +24 -0
- package/libs/passport/passport.js +109 -0
- package/libs/search/api.js +384 -0
- package/libs/search/features.js +219 -0
- package/libs/search/service.js +64 -0
- package/libs/swagger/config.js +18 -0
- package/libs/swagger/index.js +35 -0
- package/libs/validator/index.js +254 -0
- package/models/blog.js +31 -0
- package/models/brand.js +12 -0
- package/models/cart.js +14 -0
- package/models/category.js +11 -0
- package/models/coupon.js +9 -0
- package/models/customer.js +0 -0
- package/models/enquiry.js +29 -0
- package/models/events.js +13 -0
- package/models/order.js +94 -0
- package/models/product.js +32 -0
- package/models/review.js +14 -0
- package/models/tag.js +10 -0
- package/models/task.js +11 -0
- package/models/user.js +68 -0
- package/package.json +12 -0
- package/routes/agent.js +615 -0
- package/routes/auth.js +13 -0
- package/routes/blog.js +19 -0
- package/routes/brand.js +15 -0
- package/routes/cart.js +105 -0
- package/routes/category.js +16 -0
- package/routes/coupon.js +15 -0
- package/routes/enquiry.js +14 -0
- package/routes/events.js +16 -0
- package/routes/mail.js +170 -0
- package/routes/order.js +19 -0
- package/routes/product.js +22 -0
- package/routes/review.js +11 -0
- package/routes/task.js +12 -0
- package/routes/user.js +17 -0
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
const { tryCatch } = require("../libs/handler/error");
|
|
2
|
+
const Cart = require("../models/cart");
|
|
3
|
+
const methods = require("./crud");
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Shopping Cart Controller
|
|
7
|
+
* Handles cart operations with proper validation and error handling
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Create or update cart item
|
|
12
|
+
* @param {Object} req - Express request object
|
|
13
|
+
* @param {Object} res - Express response object
|
|
14
|
+
*/
|
|
15
|
+
exports.createCartItem = tryCatch(async (req, res) => {
|
|
16
|
+
const { userId, productId, quantity = 1, price } = req.body;
|
|
17
|
+
|
|
18
|
+
// Validate required fields
|
|
19
|
+
if (!userId || !productId) {
|
|
20
|
+
return res.status(400).json({
|
|
21
|
+
success: false,
|
|
22
|
+
message: "userId and productId are required"
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (!price || price <= 0) {
|
|
27
|
+
return res.status(400).json({
|
|
28
|
+
success: false,
|
|
29
|
+
message: "Valid price is required"
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (quantity <= 0) {
|
|
34
|
+
return res.status(400).json({
|
|
35
|
+
success: false,
|
|
36
|
+
message: "Quantity must be greater than 0"
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Check if item already exists in cart
|
|
41
|
+
return Cart.findOne({ userId, productId })
|
|
42
|
+
.then((existingItem) => {
|
|
43
|
+
if (existingItem) {
|
|
44
|
+
// Update existing cart item quantity
|
|
45
|
+
existingItem.quantity += parseInt(quantity);
|
|
46
|
+
existingItem.price = price; // Update price in case it changed
|
|
47
|
+
existingItem.updatedAt = new Date();
|
|
48
|
+
|
|
49
|
+
return existingItem.save();
|
|
50
|
+
} else {
|
|
51
|
+
// Create new cart item
|
|
52
|
+
const cartData = {
|
|
53
|
+
userId,
|
|
54
|
+
productId,
|
|
55
|
+
quantity: parseInt(quantity),
|
|
56
|
+
price: parseFloat(price),
|
|
57
|
+
createdAt: new Date(),
|
|
58
|
+
updatedAt: new Date()
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
return new Cart(cartData).save();
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
.then((cartItem) => {
|
|
65
|
+
return res.status(201).json({
|
|
66
|
+
success: true,
|
|
67
|
+
message: "Cart item created/updated successfully",
|
|
68
|
+
result: cartItem
|
|
69
|
+
});
|
|
70
|
+
})
|
|
71
|
+
.catch((error) => {
|
|
72
|
+
console.error('Error in createCartItem:', error);
|
|
73
|
+
|
|
74
|
+
if (error.name === "ValidationError") {
|
|
75
|
+
return res.status(400).json({
|
|
76
|
+
success: false,
|
|
77
|
+
message: "Validation failed",
|
|
78
|
+
errors: error.errors
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return res.status(500).json({
|
|
83
|
+
success: false,
|
|
84
|
+
message: "Error creating cart item",
|
|
85
|
+
error: error.message
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Get user's cart with populated product details
|
|
92
|
+
* @param {Object} req - Express request object
|
|
93
|
+
* @param {Object} res - Express response object
|
|
94
|
+
*/
|
|
95
|
+
exports.getUserCart = tryCatch(async (req, res) => {
|
|
96
|
+
// Get userId from params, body, or user session
|
|
97
|
+
const userId = req.params.userId || req.body.id || req.user?._id;
|
|
98
|
+
|
|
99
|
+
if (!userId) {
|
|
100
|
+
return res.status(400).json({
|
|
101
|
+
success: false,
|
|
102
|
+
message: "User ID is required"
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return Cart.find({ userId })
|
|
107
|
+
.populate({
|
|
108
|
+
path: "productId",
|
|
109
|
+
select: "name price discountPrice images category brand stock isActive",
|
|
110
|
+
match: { isActive: { $ne: false } } // Only populate active products
|
|
111
|
+
})
|
|
112
|
+
.sort({ createdAt: -1 })
|
|
113
|
+
.then((cartItems) => {
|
|
114
|
+
// Filter out items where product was not populated (deleted products)
|
|
115
|
+
const validCartItems = cartItems.filter(item => item.productId);
|
|
116
|
+
|
|
117
|
+
// Calculate cart summary
|
|
118
|
+
const cartSummary = {
|
|
119
|
+
totalItems: validCartItems.length,
|
|
120
|
+
totalQuantity: validCartItems.reduce((sum, item) => sum + item.quantity, 0),
|
|
121
|
+
subtotal: validCartItems.reduce((sum, item) => {
|
|
122
|
+
const itemPrice = item.productId.discountPrice || item.productId.price || item.price;
|
|
123
|
+
return sum + (itemPrice * item.quantity);
|
|
124
|
+
}, 0)
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
// Round subtotal to 2 decimal places
|
|
128
|
+
cartSummary.subtotal = Math.round(cartSummary.subtotal * 100) / 100;
|
|
129
|
+
|
|
130
|
+
return res.status(200).json({
|
|
131
|
+
success: true,
|
|
132
|
+
message: `Found ${validCartItems.length} items in cart`,
|
|
133
|
+
result: validCartItems,
|
|
134
|
+
summary: cartSummary
|
|
135
|
+
});
|
|
136
|
+
})
|
|
137
|
+
.catch((error) => {
|
|
138
|
+
console.error('Error in getUserCart:', error);
|
|
139
|
+
return res.status(500).json({
|
|
140
|
+
success: false,
|
|
141
|
+
message: "Error retrieving cart",
|
|
142
|
+
error: error.message
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Empty user's cart (remove all items)
|
|
149
|
+
* @param {Object} req - Express request object
|
|
150
|
+
* @param {Object} res - Express response object
|
|
151
|
+
*/
|
|
152
|
+
exports.emptyUserCart = tryCatch(async (req, res) => {
|
|
153
|
+
// Get userId from params, body, or user session
|
|
154
|
+
const userId = req.params.userId || req.body.id || req.user?._id;
|
|
155
|
+
|
|
156
|
+
if (!userId) {
|
|
157
|
+
return res.status(400).json({
|
|
158
|
+
success: false,
|
|
159
|
+
message: "User ID is required"
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return Cart.deleteMany({ userId })
|
|
164
|
+
.then((deleteResult) => {
|
|
165
|
+
if (deleteResult.deletedCount === 0) {
|
|
166
|
+
return res.status(200).json({
|
|
167
|
+
success: true,
|
|
168
|
+
message: "Cart was already empty",
|
|
169
|
+
deletedCount: 0
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return res.status(200).json({
|
|
174
|
+
success: true,
|
|
175
|
+
message: `Successfully cleared cart - ${deleteResult.deletedCount} items removed`,
|
|
176
|
+
deletedCount: deleteResult.deletedCount
|
|
177
|
+
});
|
|
178
|
+
})
|
|
179
|
+
.catch((error) => {
|
|
180
|
+
console.error('Error in emptyUserCart:', error);
|
|
181
|
+
return res.status(500).json({
|
|
182
|
+
success: false,
|
|
183
|
+
message: "Error emptying cart",
|
|
184
|
+
error: error.message
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Update quantity of specific cart item
|
|
191
|
+
* @param {Object} req - Express request object
|
|
192
|
+
* @param {Object} res - Express response object
|
|
193
|
+
*/
|
|
194
|
+
exports.updateQuantity = tryCatch(async (req, res) => {
|
|
195
|
+
const { cartItemId } = req.params;
|
|
196
|
+
const { newQuantity, userId } = req.body;
|
|
197
|
+
|
|
198
|
+
// Validate required fields
|
|
199
|
+
if (!cartItemId) {
|
|
200
|
+
return res.status(400).json({
|
|
201
|
+
success: false,
|
|
202
|
+
message: "Cart item ID is required"
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (!userId) {
|
|
207
|
+
return res.status(400).json({
|
|
208
|
+
success: false,
|
|
209
|
+
message: "User ID is required"
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (!newQuantity || newQuantity <= 0) {
|
|
214
|
+
return res.status(400).json({
|
|
215
|
+
success: false,
|
|
216
|
+
message: "Valid quantity (greater than 0) is required"
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Validate ObjectId format
|
|
221
|
+
if (!cartItemId.match(/^[0-9a-fA-F]{24}$/)) {
|
|
222
|
+
return res.status(400).json({
|
|
223
|
+
success: false,
|
|
224
|
+
message: "Invalid cart item ID format"
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return Cart.findOne({ _id: cartItemId, userId })
|
|
229
|
+
.then((cartItem) => {
|
|
230
|
+
if (!cartItem) {
|
|
231
|
+
return res.status(404).json({
|
|
232
|
+
success: false,
|
|
233
|
+
message: "Cart item not found or doesn't belong to user"
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Update quantity and timestamp
|
|
238
|
+
cartItem.quantity = parseInt(newQuantity);
|
|
239
|
+
cartItem.updatedAt = new Date();
|
|
240
|
+
|
|
241
|
+
return cartItem.save();
|
|
242
|
+
})
|
|
243
|
+
.then((updatedItem) => {
|
|
244
|
+
return res.status(200).json({
|
|
245
|
+
success: true,
|
|
246
|
+
message: "Cart item quantity updated successfully",
|
|
247
|
+
result: updatedItem
|
|
248
|
+
});
|
|
249
|
+
})
|
|
250
|
+
.catch((error) => {
|
|
251
|
+
console.error('Error in updateQuantity:', error);
|
|
252
|
+
|
|
253
|
+
if (error.name === "ValidationError") {
|
|
254
|
+
return res.status(400).json({
|
|
255
|
+
success: false,
|
|
256
|
+
message: "Validation failed",
|
|
257
|
+
errors: error.errors
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return res.status(500).json({
|
|
262
|
+
success: false,
|
|
263
|
+
message: "Error updating cart item quantity",
|
|
264
|
+
error: error.message
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Remove specific item from cart
|
|
271
|
+
* @param {Object} req - Express request object
|
|
272
|
+
* @param {Object} res - Express response object
|
|
273
|
+
*/
|
|
274
|
+
exports.removeCartItem = tryCatch(async (req, res) => {
|
|
275
|
+
const { cartItemId } = req.params;
|
|
276
|
+
const { userId } = req.body;
|
|
277
|
+
|
|
278
|
+
if (!cartItemId) {
|
|
279
|
+
return res.status(400).json({
|
|
280
|
+
success: false,
|
|
281
|
+
message: "Cart item ID is required"
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (!userId) {
|
|
286
|
+
return res.status(400).json({
|
|
287
|
+
success: false,
|
|
288
|
+
message: "User ID is required"
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Validate ObjectId format
|
|
293
|
+
if (!cartItemId.match(/^[0-9a-fA-F]{24}$/)) {
|
|
294
|
+
return res.status(400).json({
|
|
295
|
+
success: false,
|
|
296
|
+
message: "Invalid cart item ID format"
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
return Cart.findOneAndDelete({ _id: cartItemId, userId })
|
|
301
|
+
.then((deletedItem) => {
|
|
302
|
+
if (!deletedItem) {
|
|
303
|
+
return res.status(404).json({
|
|
304
|
+
success: false,
|
|
305
|
+
message: "Cart item not found or doesn't belong to user"
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
return res.status(200).json({
|
|
310
|
+
success: true,
|
|
311
|
+
message: "Cart item removed successfully",
|
|
312
|
+
result: deletedItem
|
|
313
|
+
});
|
|
314
|
+
})
|
|
315
|
+
.catch((error) => {
|
|
316
|
+
console.error('Error in removeCartItem:', error);
|
|
317
|
+
return res.status(500).json({
|
|
318
|
+
success: false,
|
|
319
|
+
message: "Error removing cart item",
|
|
320
|
+
error: error.message
|
|
321
|
+
});
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Get cart item count for user
|
|
327
|
+
* @param {Object} req - Express request object
|
|
328
|
+
* @param {Object} res - Express response object
|
|
329
|
+
*/
|
|
330
|
+
exports.getCartCount = tryCatch(async (req, res) => {
|
|
331
|
+
const userId = req.params.userId || req.body.id || req.user?._id;
|
|
332
|
+
|
|
333
|
+
if (!userId) {
|
|
334
|
+
return res.status(400).json({
|
|
335
|
+
success: false,
|
|
336
|
+
message: "User ID is required"
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
return Cart.countDocuments({ userId })
|
|
341
|
+
.then((count) => {
|
|
342
|
+
return res.status(200).json({
|
|
343
|
+
success: true,
|
|
344
|
+
message: "Cart count retrieved successfully",
|
|
345
|
+
result: { count }
|
|
346
|
+
});
|
|
347
|
+
})
|
|
348
|
+
.catch((error) => {
|
|
349
|
+
console.error('Error in getCartCount:', error);
|
|
350
|
+
return res.status(500).json({
|
|
351
|
+
success: false,
|
|
352
|
+
message: "Error getting cart count",
|
|
353
|
+
error: error.message
|
|
354
|
+
});
|
|
355
|
+
});
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Sync cart prices with current product prices
|
|
360
|
+
* @param {Object} req - Express request object
|
|
361
|
+
* @param {Object} res - Express response object
|
|
362
|
+
*/
|
|
363
|
+
exports.syncCartPrices = tryCatch(async (req, res) => {
|
|
364
|
+
const userId = req.params.userId || req.body.id || req.user?._id;
|
|
365
|
+
|
|
366
|
+
if (!userId) {
|
|
367
|
+
return res.status(400).json({
|
|
368
|
+
success: false,
|
|
369
|
+
message: "User ID is required"
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
return Cart.find({ userId })
|
|
374
|
+
.populate('productId', 'price discountPrice')
|
|
375
|
+
.then((cartItems) => {
|
|
376
|
+
const updatePromises = cartItems.map((item) => {
|
|
377
|
+
if (item.productId) {
|
|
378
|
+
const currentPrice = item.productId.discountPrice || item.productId.price;
|
|
379
|
+
if (currentPrice !== item.price) {
|
|
380
|
+
item.price = currentPrice;
|
|
381
|
+
item.updatedAt = new Date();
|
|
382
|
+
return item.save();
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
return Promise.resolve(item);
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
return Promise.all(updatePromises);
|
|
389
|
+
})
|
|
390
|
+
.then((updatedItems) => {
|
|
391
|
+
return res.status(200).json({
|
|
392
|
+
success: true,
|
|
393
|
+
message: "Cart prices synchronized successfully",
|
|
394
|
+
result: updatedItems
|
|
395
|
+
});
|
|
396
|
+
})
|
|
397
|
+
.catch((error) => {
|
|
398
|
+
console.error('Error in syncCartPrices:', error);
|
|
399
|
+
return res.status(500).json({
|
|
400
|
+
success: false,
|
|
401
|
+
message: "Error syncing cart prices",
|
|
402
|
+
error: error.message
|
|
403
|
+
});
|
|
404
|
+
});
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
// Extend with CRUD methods for additional cart operations
|
|
408
|
+
const crud = methods.crudController(Cart);
|
|
409
|
+
for (const prop in crud) {
|
|
410
|
+
if (crud.hasOwnProperty(prop)) {
|
|
411
|
+
module.exports[prop] = crud[prop];
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// Export all methods
|
|
416
|
+
module.exports = {
|
|
417
|
+
...module.exports,
|
|
418
|
+
createCartItem: exports.createCartItem,
|
|
419
|
+
getUserCart: exports.getUserCart,
|
|
420
|
+
emptyUserCart: exports.emptyUserCart,
|
|
421
|
+
updateQuantity: exports.updateQuantity,
|
|
422
|
+
removeCartItem: exports.removeCartItem,
|
|
423
|
+
getCartCount: exports.getCartCount,
|
|
424
|
+
syncCartPrices: exports.syncCartPrices
|
|
425
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
const Coupon = require("../models/coupon");
|
|
2
|
+
const asynHandler = require("../libs/handler/async");
|
|
3
|
+
const methods = require("./crud");
|
|
4
|
+
|
|
5
|
+
module.exports = methods.crudController( Coupon );
|
|
6
|
+
|
|
7
|
+
/*
|
|
8
|
+
const createCoupon = asynHandler(async (req, res) => {
|
|
9
|
+
try {
|
|
10
|
+
const newCoupon = await Coupon.create(req.body);
|
|
11
|
+
res.json(newCoupon);
|
|
12
|
+
} catch (error) {
|
|
13
|
+
throw new Error(error);
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
const getAllCoupons = asynHandler(async (req, res) => {
|
|
17
|
+
try {
|
|
18
|
+
const coupons = await Coupon.find();
|
|
19
|
+
res.json(coupons);
|
|
20
|
+
} catch (error) {
|
|
21
|
+
throw new Error(error);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
const updateCoupon = asynHandler(async (req, res) => {
|
|
25
|
+
const { id } = req.params;
|
|
26
|
+
validateMongoDbId(id);
|
|
27
|
+
try {
|
|
28
|
+
const updatecoupon = await Coupon.findByIdAndUpdate(id, req.body, {
|
|
29
|
+
new: true,
|
|
30
|
+
});
|
|
31
|
+
res.json(updatecoupon);
|
|
32
|
+
} catch (error) {
|
|
33
|
+
throw new Error(error);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
const deleteCoupon = asynHandler(async (req, res) => {
|
|
37
|
+
const { id } = req.params;
|
|
38
|
+
validateMongoDbId(id);
|
|
39
|
+
try {
|
|
40
|
+
const deletecoupon = await Coupon.findByIdAndDelete(id);
|
|
41
|
+
res.json(deletecoupon);
|
|
42
|
+
} catch (error) {
|
|
43
|
+
throw new Error(error);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
const getCoupon = asynHandler(async (req, res) => {
|
|
47
|
+
const { id } = req.params;
|
|
48
|
+
validateMongoDbId(id);
|
|
49
|
+
try {
|
|
50
|
+
const getAcoupon = await Coupon.findById(id);
|
|
51
|
+
res.json(getAcoupon);
|
|
52
|
+
} catch (error) {
|
|
53
|
+
throw new Error(error);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
module.exports = {
|
|
57
|
+
createCoupon,
|
|
58
|
+
getAllCoupons,
|
|
59
|
+
updateCoupon,
|
|
60
|
+
deleteCoupon,
|
|
61
|
+
getCoupon,
|
|
62
|
+
};
|
|
63
|
+
*/
|