forked from MyZubster-Ecosystem/myzubster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboardController.js
More file actions
166 lines (149 loc) 路 6.55 KB
/
Copy pathdashboardController.js
File metadata and controls
166 lines (149 loc) 路 6.55 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
const Dashboard = require('../models/dashboardModel');
const { v4: uuidv4 } = require('uuid');
// #242: User dashboard - balance and transaction history
exports.getUserDashboard = async (req, res) => {
try {
const userId = req.params.userId || req.user?.userId;
if (!userId) return res.status(400).json({ error: 'userId is required' });
const d = await Dashboard.getOrCreate(userId);
res.json({
userId: d.userId,
balanceMYZ: d.balanceMYZ,
balanceXMR: d.balanceXMR,
transactionCount: d.transactions.length,
transactions: d.transactions.slice(-20).reverse(),
createdAt: d.createdAt,
updatedAt: d.updatedAt
});
} catch (e) { res.status(500).json({ error: e.message }); }
};
// #245: Robot dashboard - earnings and work history
exports.getRobotDashboard = async (req, res) => {
try {
const { robotId } = req.params;
if (!robotId) return res.status(400).json({ error: 'robotId is required' });
const d = await Dashboard.findOne({ robotId });
if (!d) return res.status(404).json({ error: 'Robot not found' });
const earnings = d.transactions.filter(t => t.type === 'earn');
res.json({
robotId: d.robotId,
totalEarnings: d.totalEarnings,
jobsCompleted: d.jobsCompleted,
balanceMYZ: d.balanceMYZ,
balanceXMR: d.balanceXMR,
earningsHistory: earnings.slice(-20).reverse(),
allTransactions: d.transactions.slice(-20).reverse()
});
} catch (e) { res.status(500).json({ error: e.message }); }
};
// #243: P2P transfer endpoint
exports.createP2PTransfer = async (req, res) => {
try {
const { senderId, receiverId, amount, currency, description } = req.body;
if (!senderId || !receiverId || !amount || !currency)
return res.status(400).json({ error: 'senderId, receiverId, amount, and currency are required' });
if (senderId === receiverId) return res.status(400).json({ error: 'Cannot transfer to self' });
if (amount <= 0) return res.status(400).json({ error: 'Amount must be positive' });
const sender = await Dashboard.getOrCreate(senderId);
const receiver = await Dashboard.getOrCreate(receiverId);
// Check balance
if (currency === 'MYZ' && sender.balanceMYZ < amount)
return res.status(400).json({ error: 'Insufficient MYZ balance' });
if (currency === 'XMR' && sender.balanceXMR < amount)
return res.status(400).json({ error: 'Insufficient XMR balance' });
// Execute transfer
sender.addTransaction('transfer_out', amount, currency, receiverId, description || `P2P transfer to ${receiverId}`);
receiver.addTransaction('transfer_in', amount, currency, senderId, description || `P2P transfer from ${senderId}`);
await sender.save();
await receiver.save();
res.json({
message: 'P2P transfer successful',
senderId, receiverId, amount, currency,
senderBalance: currency === 'MYZ' ? sender.balanceMYZ : sender.balanceXMR,
receiverBalance: currency === 'MYZ' ? receiver.balanceMYZ : receiver.balanceXMR
});
} catch (e) { res.status(500).json({ error: e.message }); }
};
// #241: Add payment option to marketplace checkout
exports.addCheckoutPayment = async (req, res) => {
try {
const { userId, orderId, amount, currency, paymentMethod } = req.body;
if (!userId || !orderId || !amount || !currency)
return res.status(400).json({ error: 'userId, orderId, amount, and currency are required' });
const method = paymentMethod || currency;
if (!['MYZ', 'XMR'].includes(method))
return res.status(400).json({ error: 'Payment method must be MYZ or XMR' });
const d = await Dashboard.getOrCreate(userId);
// Check balance
if (method === 'MYZ' && d.balanceMYZ < amount)
return res.status(400).json({ error: 'Insufficient MYZ balance for checkout' });
if (method === 'XMR' && d.balanceXMR < amount)
return res.status(400).json({ error: 'Insufficient XMR balance for checkout' });
d.addTransaction('purchase', amount, method, orderId, `Marketplace checkout for order ${orderId}`);
await d.save();
res.json({
message: 'Payment processed for marketplace checkout',
orderId, amount, currency: method,
remainingBalance: method === 'MYZ' ? d.balanceMYZ : d.balanceXMR
});
} catch (e) { res.status(500).json({ error: e.message }); }
};
// #244: Handle real Monero webhook
exports.handleMoneroWebhook = async (req, res) => {
try {
const { txHash, userId, amount, confirmations, blockHeight } = req.body;
if (!txHash || !userId || !amount)
return res.status(400).json({ error: 'txHash, userId, and amount are required' });
const d = await Dashboard.getOrCreate(userId);
// Check for duplicate webhook
const existing = d.transactions.find(t => t.txId === txHash);
if (existing)
return res.json({ message: 'Webhook already processed', txHash, status: existing.status });
d.addTransaction('webhook', amount, 'XMR', null, `Monero payment received: ${txHash}`);
await d.save();
res.json({
message: 'Monero webhook processed',
txHash, userId, amount,
newBalanceXMR: d.balanceXMR,
confirmations: confirmations || 0,
blockHeight: blockHeight || null
});
} catch (e) { res.status(500).json({ error: e.message }); }
};
// List transactions
exports.listTransactions = async (req, res) => {
try {
const { userId, type, currency } = req.query;
if (!userId) return res.status(400).json({ error: 'userId is required' });
const d = await Dashboard.getOrCreate(userId);
let txs = d.transactions;
if (type) txs = txs.filter(t => t.type === type);
if (currency) txs = txs.filter(t => t.currency === currency);
res.json({
count: txs.length,
transactions: txs.slice(-100).reverse()
});
} catch (e) { res.status(500).json({ error: e.message }); }
};
// Get stats
exports.getStats = async (req, res) => {
try {
const totalUsers = await Dashboard.countDocuments();
const totalMYZ = await Dashboard.aggregate([
{ $group: { _id: null, total: { $sum: '$balanceMYZ' } } }
]);
const totalXMR = await Dashboard.aggregate([
{ $group: { _id: null, total: { $sum: '$balanceXMR' } } }
]);
const totalTransactions = await Dashboard.aggregate([
{ $project: { txCount: { $size: '$transactions' } } },
{ $group: { _id: null, total: { $sum: '$txCount' } } }
]);
res.json({
totalUsers,
totalMYZInCirculation: totalMYZ[0]?.total || 0,
totalXMRInCirculation: totalXMR[0]?.total || 0,
totalTransactions: totalTransactions[0]?.total || 0
});
} catch (e) { res.status(500).json({ error: e.message }); }
};