forked from Emmy123222/Stellar-MicroPay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaymentController.js
More file actions
110 lines (98 loc) · 3.24 KB
/
Copy pathpaymentController.js
File metadata and controls
110 lines (98 loc) · 3.24 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
/**
* src/controllers/paymentController.js
* Handles payment history and stats requests.
*/
"use strict";
const stellarService = require("../services/stellarService");
/**
* @typedef {object} PaymentRecord
* @property {string} id
* @property {"sent"|"received"} type
* @property {string} amount
* @property {string} asset
* @property {string} from
* @property {string} to
* @property {string} createdAt
* @property {string} transactionHash
* @property {string} pagingToken
* @property {string} [memo]
*/
/**
* GET /api/payments/:publicKey
*
* @param {object} req - Express request
* @param {object} req.params
* @param {string} req.params.publicKey - Stellar public key (G...)
* @param {object} req.query
* @param {string} [req.query.limit] - Max records to return (1-100, default 20)
* @param {string} [req.query.cursor] - Horizon paging token to continue from
* @param {object} res - Express response
* @param {function} next - Express error-handling callback
* @returns {Promise<void>} JSON: `{ success: true, data: PaymentRecord[] }`,
* or 400 JSON: `{ error: string }` when limit is not a positive integer
*/
async function getPayments(req, res, next) {
try {
const { publicKey } = req.params;
// #197: explicit validation — || 20 silently swallows limit=0; NaN propagates to Horizon
const rawLimit = req.query.limit;
let limit = 20;
if (rawLimit !== undefined) {
const parsed = parseInt(rawLimit, 10);
if (isNaN(parsed) || !Number.isSafeInteger(parsed) || parsed < 1) {
return res.status(400).json({ error: "limit must be a positive integer" });
}
limit = Math.min(parsed, 100);
}
const cursor = req.query.cursor || undefined;
const payments = await stellarService.getPayments(publicKey, { limit, cursor });
res.json({ success: true, data: payments });
} catch (err) {
next(err);
}
}
/**
* GET /api/payments/:publicKey/stats
* Computes aggregate payment statistics for a wallet.
*
* @param {object} req - Express request
* @param {object} req.params
* @param {string} req.params.publicKey - Stellar public key (G...)
* @param {object} res - Express response
* @param {function} next - Express error-handling callback
* @returns {Promise<void>} JSON: `{ success: true, data: { publicKey: string, totalSentXLM: string,
* totalReceivedXLM: string, sentCount: number, receivedCount: number, totalTransactions: number } }`
*/
async function getStats(req, res, next) {
try {
const { publicKey } = req.params;
const payments = await stellarService.getPayments(publicKey, { limit: 100 });
let totalSent = 0;
let totalReceived = 0;
let sentCount = 0;
let receivedCount = 0;
for (const p of payments) {
if (p.type === "sent") {
totalSent += parseFloat(p.amount);
sentCount++;
} else {
totalReceived += parseFloat(p.amount);
receivedCount++;
}
}
res.json({
success: true,
data: {
publicKey,
totalSentXLM: totalSent.toFixed(7),
totalReceivedXLM: totalReceived.toFixed(7),
sentCount,
receivedCount,
totalTransactions: sentCount + receivedCount,
},
});
} catch (err) {
next(err);
}
}
module.exports = { getPayments, getStats };