forked from MyZubster-Ecosystem/myzubster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcouponController.js
More file actions
78 lines (75 loc) · 3.07 KB
/
Copy pathcouponController.js
File metadata and controls
78 lines (75 loc) · 3.07 KB
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
const githubController = require('./githubWebhookController');
const users = githubController.users || {};
const Coupon = require('../models/Coupon');
function generateCouponCode() {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
let code = '';
for (let i = 0; i < 8; i++) {
code += chars.charAt(Math.floor(Math.random() * chars.length));
}
return 'URBAN-' + code;
}
module.exports = {
createCoupon: async (req, res) => {
try {
const { userId, discountType, discountValue, minMYZ, expiresInDays = 30 } = req.body;
if (!userId || !discountType || !discountValue || !minMYZ) {
return res.status(400).json({ error: 'Dati mancanti' });
}
const user = users[userId];
if (!user) {
return res.status(404).json({ error: 'Utente non trovato' });
}
if (user.myzBalance < minMYZ) {
return res.status(400).json({ error: 'MYZ insufficienti' });
}
user.myzBalance -= minMYZ;
const coupon = new Coupon({
userId,
code: generateCouponCode(),
discountType,
discountValue,
minMYZ,
expiresAt: new Date(Date.now() + expiresInDays * 24 * 60 * 60 * 1000),
isActive: true
});
await coupon.save();
res.status(201).json({
success: true,
coupon: { code: coupon.code, discountType: coupon.discountType, discountValue: coupon.discountValue, expiresAt: coupon.expiresAt },
newBalance: user.myzBalance
});
} catch (error) {
console.error('❌ Errore creazione coupon:', error);
res.status(500).json({ error: 'Errore interno', details: error.message });
}
},
redeemCoupon: async (req, res) => {
try {
const { code, userId } = req.body;
if (!code || !userId) return res.status(400).json({ error: 'Codice e userId obbligatori' });
const coupon = await Coupon.findOne({ code, isActive: true });
if (!coupon) return res.status(404).json({ error: 'Coupon non valido o scaduto' });
if (coupon.usedCount >= coupon.maxUses) return res.status(400).json({ error: 'Coupon già utilizzato' });
if (new Date() > coupon.expiresAt) return res.status(400).json({ error: 'Coupon scaduto' });
coupon.usedCount += 1;
coupon.redeemedAt = new Date();
if (coupon.usedCount >= coupon.maxUses) coupon.isActive = false;
await coupon.save();
res.json({ success: true, discount: { type: coupon.discountType, value: coupon.discountValue }, message: `Sconto applicato: ${coupon.discountValue} ${coupon.discountType === 'percentage' ? '%' : 'XMR'}` });
} catch (error) {
console.error('❌ Errore riscatto coupon:', error);
res.status(500).json({ error: 'Errore interno', details: error.message });
}
},
getUserCoupons: async (req, res) => {
try {
const { userId } = req.params;
const coupons = await Coupon.find({ userId, isActive: true }).select('-__v');
res.json({ success: true, coupons });
} catch (error) {
console.error('❌ Errore recupero coupon:', error);
res.status(500).json({ error: 'Errore interno' });
}
}
};