forked from Emmy123222/Stellar-MicroPay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtipsController.js
More file actions
208 lines (187 loc) · 5.91 KB
/
Copy pathtipsController.js
File metadata and controls
208 lines (187 loc) · 5.91 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
206
207
208
/**
* src/controllers/tipsController.js
* Handles tip-related API requests.
*/
"use strict";
const tipsService = require("../services/tipsService");
/**
* @typedef {object} TipRecord
* @property {number} id
* @property {string} senderPublicKey
* @property {string} creatorPublicKey
* @property {string} amount
* @property {string} asset
* @property {string} memo
* @property {string} txHash
* @property {string} timestamp
*/
/**
* @typedef {object} TipsStats
* @property {number} totalTips
* @property {Object<string, {count: number, amount: string}>} totalByAsset
* @property {string|null} averageTip
* @property {string|null} largestTip
* @property {string|null} smallestTip
*/
/**
* POST /api/tips
* Record a new tip.
*
* @param {object} req - Express request
* @param {object} req.body
* @param {string} req.body.senderPublicKey - Sender's Stellar public key (G...)
* @param {string} req.body.creatorPublicKey - Creator's Stellar public key (G...)
* @param {string|number} req.body.amount - Positive tip amount
* @param {string} [req.body.asset] - Asset code, defaults to "XLM"
* @param {string} [req.body.memo] - Optional message from sender
* @param {string} [req.body.txHash] - On-chain transaction hash
* @param {object} res - Express response
* @param {function} next - Express error-handling callback
* @returns {Promise<void>} 201 JSON: `{ success: true, data: TipRecord, message: string }`
*/
async function recordTip(req, res, next) {
try {
const { senderPublicKey, creatorPublicKey, amount, asset, memo, txHash } = req.body;
// Validate input
tipsService.validateTipInput({ senderPublicKey, creatorPublicKey, amount });
const tip = tipsService.recordTip({
senderPublicKey,
creatorPublicKey,
amount,
asset: asset || "XLM",
memo: memo || "",
txHash: txHash || "",
});
res.status(201).json({
success: true,
data: tip,
message: "Tip recorded successfully",
});
} catch (err) {
next(err);
}
}
/**
* GET /api/tips/received/:creatorPublicKey
* Get all tips received by a creator.
*
* @param {object} req - Express request
* @param {object} req.params
* @param {string} req.params.creatorPublicKey - Creator's Stellar public key (G...)
* @param {object} req.query
* @param {string} [req.query.limit] - Max tips to return
* @param {string} [req.query.offset] - Number of tips to skip
* @param {object} res - Express response
* @param {function} next - Express error-handling callback
* @returns {Promise<void>} JSON: `{ success: true, data: { tips: TipRecord[], total: number,
* limit: number, offset: number, stats: TipsStats } }`
*/
async function getTipsReceived(req, res, next) {
try {
const { creatorPublicKey } = req.params;
const { limit, offset } = req.query;
const result = tipsService.getTipsReceived(creatorPublicKey, {
limit: limit ? parseInt(limit, 10) : undefined,
offset: offset ? parseInt(offset, 10) : undefined,
});
// Also get stats
const stats = tipsService.getTipsStats(creatorPublicKey);
res.json({
success: true,
data: {
...result,
stats,
},
});
} catch (err) {
next(err);
}
}
/**
* GET /api/tips/stats/:creatorPublicKey
* Get statistics for tips received by a creator.
*
* @param {object} req - Express request
* @param {object} req.params
* @param {string} req.params.creatorPublicKey - Creator's Stellar public key (G...)
* @param {object} res - Express response
* @param {function} next - Express error-handling callback
* @returns {Promise<void>} JSON: `{ success: true, data: TipsStats }`
*/
async function getTipsStats(req, res, next) {
try {
const { creatorPublicKey } = req.params;
const stats = tipsService.getTipsStats(creatorPublicKey);
res.json({
success: true,
data: stats,
});
} catch (err) {
next(err);
}
}
/**
* GET /api/tips/sent/:senderPublicKey
* Get all tips sent by a user.
*
* @param {object} req - Express request
* @param {object} req.params
* @param {string} req.params.senderPublicKey - Sender's Stellar public key (G...)
* @param {object} req.query
* @param {string} [req.query.limit] - Max tips to return
* @param {string} [req.query.offset] - Number of tips to skip
* @param {object} res - Express response
* @param {function} next - Express error-handling callback
* @returns {Promise<void>} JSON: `{ success: true, data: { tips: TipRecord[], total: number,
* limit: number, offset: number } }`
*/
async function getTipsSent(req, res, next) {
try {
const { senderPublicKey } = req.params;
const { limit, offset } = req.query;
const result = tipsService.getTipsSent(senderPublicKey, {
limit: limit ? parseInt(limit, 10) : undefined,
offset: offset ? parseInt(offset, 10) : undefined,
});
res.json({
success: true,
data: result,
});
} catch (err) {
next(err);
}
}
/**
* GET /api/tips/leaderboard/:creatorPublicKey
* Get top tippers for a creator.
*
* @param {object} req - Express request
* @param {object} req.params
* @param {string} req.params.creatorPublicKey - Creator's Stellar public key (G...)
* @param {object} req.query
* @param {string} [req.query.limit] - Max tippers to return, defaults to 5
* @param {object} res - Express response
* @param {function} next - Express error-handling callback
* @returns {Promise<void>} JSON: `{ success: true, data: Array<{ senderPublicKey: string, totalAmount: string }> }`
*/
async function getTopTippers(req, res, next) {
try {
const { creatorPublicKey } = req.params;
const { limit } = req.query;
const parsedLimit = limit ? parseInt(limit, 10) : 5;
const result = tipsService.getTopTippers(creatorPublicKey, parsedLimit);
res.json({
success: true,
data: result,
});
} catch (err) {
next(err);
}
}
module.exports = {
recordTip,
getTipsReceived,
getTipsStats,
getTipsSent,
getTopTippers,
};