forked from MyZubster-Ecosystem/myzubster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtripController.js
More file actions
205 lines (169 loc) · 6.09 KB
/
Copy pathtripController.js
File metadata and controls
205 lines (169 loc) · 6.09 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
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 Trip = require('../models/Trip');
// Saldo utenti (condiviso con githubWebhookController)
const users = {
'DanielIoni-creator': { walletAddress: '45M4DW1ug8bdQowWpxucTpgsfjLbVxbYaAra79VewmBobuuhgqTjyD4R3DzpqLM2veiphcB16n24qN1QbLg3y2PYGK3Qkoe', myzBalance: 100 }
};
const REWARD_PER_KM = 0.2; // MYZ per km
const MILESTONE_KM = 100; // km per milestone
const MILESTONE_BONUS = 10; // MYZ bonus per milestone
module.exports = {
// Registra una tratta completata
completeTrip: async (req, res) => {
try {
const { userId, escrowId, distanceKm, durationMin, startLocation, endLocation, scooterId } = req.body;
if (!userId || !distanceKm || distanceKm <= 0) {
return res.status(400).json({ error: 'userId e distanceKm (positivo) sono obbligatori' });
}
// Calcola la ricompensa base
const rewardMYZ = Math.round(distanceKm * REWARD_PER_KM * 100) / 100;
// Salva la tratta
const trip = new Trip({
escrowId,
userId,
distanceKm,
durationMin: durationMin || 0,
startLocation: startLocation || null,
endLocation: endLocation || null,
rewardMYZ,
scooterId: scooterId || 'UNKNOWN'
});
await trip.save();
// Aggiungi MYZ al saldo dell'utente
if (!users[userId]) {
users[userId] = { walletAddress: '', myzBalance: 0 };
}
users[userId].myzBalance = (users[userId].myzBalance || 0) + rewardMYZ;
// Calcola il totale km dell'utente
const totalKmResult = await Trip.aggregate([
{ $match: { userId } },
{ $group: { _id: null, total: { $sum: '$distanceKm' } } }
]);
const totalKm = totalKmResult.length > 0 ? totalKmResult[0].total : 0;
// Controlla se ha raggiunto una milestone
let bonusMYZ = 0;
const currentMilestone = Math.floor(totalKm / MILESTONE_KM);
const previousMilestone = Math.floor((totalKm - distanceKm) / MILESTONE_KM);
if (currentMilestone > previousMilestone && currentMilestone >= 1) {
bonusMYZ = MILESTONE_BONUS * (currentMilestone - previousMilestone);
users[userId].myzBalance += bonusMYZ;
}
// Aggiorna la tratta con bonus e totale
trip.bonusMYZ = bonusMYZ;
trip.totalMYZ = rewardMYZ + bonusMYZ;
await trip.save();
res.json({
success: true,
trip: {
id: trip._id,
distanceKm,
rewardMYZ,
bonusMYZ,
totalMYZ: rewardMYZ + bonusMYZ,
totalKm: totalKm,
newBalance: users[userId].myzBalance
}
});
} catch (error) {
console.error('❌ Errore registrazione tratta:', error);
res.status(500).json({ error: 'Errore interno', message: error.message });
}
},
// Ottieni lo storico tratte di un utente
getUserTrips: async (req, res) => {
try {
const { userId } = req.params;
const { limit = 50, offset = 0 } = req.query;
const trips = await Trip.find({ userId })
.sort({ createdAt: -1 })
.skip(parseInt(offset))
.limit(parseInt(limit));
const totalKmResult = await Trip.aggregate([
{ $match: { userId } },
{ $group: { _id: null, total: { $sum: '$distanceKm' } } }
]);
const totalKm = totalKmResult.length > 0 ? totalKmResult[0].total : 0;
const totalTrips = await Trip.countDocuments({ userId });
res.json({
success: true,
totalKm,
totalTrips,
trips
});
} catch (error) {
console.error('❌ Errore recupero tratte:', error);
res.status(500).json({ error: 'Errore interno' });
}
},
// Ottieni le statistiche di un utente
getUserStats: async (req, res) => {
try {
const { userId } = req.params;
const totalKmResult = await Trip.aggregate([
{ $match: { userId } },
{ $group: { _id: null, total: { $sum: '$distanceKm' } } }
]);
const totalKm = totalKmResult.length > 0 ? totalKmResult[0].total : 0;
const totalRewardsResult = await Trip.aggregate([
{ $match: { userId } },
{ $group: { _id: null, total: { $sum: '$totalMYZ' } } }
]);
const totalMYZ = totalRewardsResult.length > 0 ? totalRewardsResult[0].total : 0;
const totalTrips = await Trip.countDocuments({ userId });
const milestones = Math.floor(totalKm / MILESTONE_KM);
res.json({
success: true,
stats: {
totalKm,
totalMYZ,
totalTrips,
milestones,
nextMilestone: (milestones + 1) * MILESTONE_KM,
kmToNextMilestone: (milestones + 1) * MILESTONE_KM - totalKm
}
});
} catch (error) {
console.error('❌ Errore recupero statistiche:', error);
res.status(500).json({ error: 'Errore interno' });
}
},
// Webhook per ricevere dati GPS dal monopattino
gpsWebhook: async (req, res) => {
try {
const { scooterId, userId, lat, lng, distanceKm, durationMin } = req.body;
if (!scooterId || !userId || !distanceKm) {
return res.status(400).json({ error: 'scooterId, userId e distanceKm sono obbligatori' });
}
// Registra la tratta automaticamente
const result = await module.exports.completeTrip({
body: { userId, distanceKm, durationMin, startLocation: { lat, lng }, scooterId }
}, res);
return result;
} catch (error) {
console.error('❌ Errore GPS webhook:', error);
res.status(500).json({ error: 'Errore interno' });
}
},
// Ottieni la classifica (leaderboard)
getLeaderboard: async (req, res) => {
try {
const { limit = 10 } = req.query;
const leaderboard = await Trip.aggregate([
{ $group: {
_id: '$userId',
totalKm: { $sum: '$distanceKm' },
totalMYZ: { $sum: '$totalMYZ' },
totalTrips: { $sum: 1 }
}},
{ $sort: { totalKm: -1 } },
{ $limit: parseInt(limit) }
]);
res.json({
success: true,
leaderboard
});
} catch (error) {
console.error('❌ Errore leaderboard:', error);
res.status(500).json({ error: 'Errore interno' });
}
}
};