forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransactionAPI.js
More file actions
111 lines (99 loc) · 3.21 KB
/
Copy pathtransactionAPI.js
File metadata and controls
111 lines (99 loc) · 3.21 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
/**
* Transaction API utilities
*/
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3001/api';
export const transactionAPI = {
/**
* Fetch transactions using cursor-based pagination.
* Returns { data, nextCursor }.
*/
async getTransactionsCursor(userId, { limit = 25, cursor } = {}) {
const params = new URLSearchParams({ userId, limit });
if (cursor) params.set('cursor', cursor);
const response = await fetch(`${API_BASE_URL}/transactions/history?${params}`);
if (!response.ok) {
throw new Error(`Failed to fetch transactions: ${response.statusText}`);
}
return response.json();
},
/**
* Fetch paginated transactions (offset-based, kept for stats/export)
*/
async getTransactions(userId, filters = {}) {
const params = new URLSearchParams({
userId,
limit: filters.limit || 20,
offset: filters.offset || 0,
...(filters.type && { type: filters.type }),
...(filters.dateFrom && { dateFrom: filters.dateFrom }),
...(filters.dateTo && { dateTo: filters.dateTo }),
...(filters.campaignId && { campaignId: filters.campaignId }),
});
const response = await fetch(`${API_BASE_URL}/transactions?${params}`);
if (!response.ok) {
throw new Error(`Failed to fetch transactions: ${response.statusText}`);
}
return response.json();
},
/**
* Export all transactions as CSV
*/
async exportTransactionsCSV(userId, filters = {}) {
const params = new URLSearchParams({
userId,
limit: 10000, // Export limit
offset: 0,
...(filters.type && { type: filters.type }),
...(filters.dateFrom && { dateFrom: filters.dateFrom }),
...(filters.dateTo && { dateTo: filters.dateTo }),
...(filters.campaignId && { campaignId: filters.campaignId }),
});
const response = await fetch(`${API_BASE_URL}/transactions/export/csv?${params}`);
if (!response.ok) {
throw new Error(`Failed to export transactions: ${response.statusText}`);
}
return response.blob();
},
/**
* Get transaction details by ID
*/
async getTransactionById(transactionId) {
const response = await fetch(`${API_BASE_URL}/transactions/${transactionId}`);
if (!response.ok) {
throw new Error(`Failed to fetch transaction: ${response.statusText}`);
}
return response.json();
},
/**
* Verify transaction on Stellar blockchain
*/
async verifyTransaction(txHash) {
try {
const response = await fetch(
`https://stellar.expert/api/v2/tx/${txHash}`
);
if (!response.ok) {
throw new Error('Transaction not found');
}
return response.json();
} catch (error) {
return null;
}
},
/**
* Get transaction statistics
*/
async getTransactionStats(userId, filters = {}) {
const params = new URLSearchParams({
userId,
...(filters.dateFrom && { dateFrom: filters.dateFrom }),
...(filters.dateTo && { dateTo: filters.dateTo }),
});
const response = await fetch(`${API_BASE_URL}/transactions/stats?${params}`);
if (!response.ok) {
throw new Error(`Failed to fetch stats: ${response.statusText}`);
}
return response.json();
},
};
export default transactionAPI;