-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-transactions-by-state.js
More file actions
186 lines (164 loc) Β· 5.96 KB
/
Copy pathtest-transactions-by-state.js
File metadata and controls
186 lines (164 loc) Β· 5.96 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
/**
* Test script for the new GET /transactions endpoint with state filtering
*/
const BASE_URL = process.env.API_BASE_URL || 'http://localhost:3000/api/v1';
async function testTransactionsByState() {
console.log('π§ͺ Testing GET /transactions endpoint with state filtering...\n');
const testCases = [
{
name: 'Get all transactions (no filter)',
url: `${BASE_URL}/transactions`
},
{
name: 'Filter by PENDING state',
url: `${BASE_URL}/transactions?state=PENDING`
},
{
name: 'Filter by COMPLETED state',
url: `${BASE_URL}/transactions?state=COMPLETED`
},
{
name: 'Filter by PAID state',
url: `${BASE_URL}/transactions?state=PAID`
},
{
name: 'Filter by INITIALIZED state',
url: `${BASE_URL}/transactions?state=INITIALIZED`
},
{
name: 'Filter by PAYOUT_FAILED state',
url: `${BASE_URL}/transactions?state=PAYOUT_FAILED`
},
{
name: 'Filter by currency (USD)',
url: `${BASE_URL}/transactions?currency=USD`
},
{
name: 'Filter by currency (NGN)',
url: `${BASE_URL}/transactions?currency=NGN`
},
{
name: 'Combine state and currency filters',
url: `${BASE_URL}/transactions?state=COMPLETED¤cy=USD`
},
{
name: 'Filter by payment link ID',
url: `${BASE_URL}/transactions?paymentLinkId=your-payment-link-id-here`
},
{
name: 'With pagination',
url: `${BASE_URL}/transactions?state=COMPLETED&page=1&limit=5`
},
{
name: 'With sorting by creation date (desc)',
url: `${BASE_URL}/transactions?sortBy=createdAt&sortOrder=desc`
},
{
name: 'With sorting by paid date (desc)',
url: `${BASE_URL}/transactions?state=PAID&sortBy=paidAt&sortOrder=desc`
},
{
name: 'Complex query with multiple filters',
url: `${BASE_URL}/transactions?state=COMPLETED¤cy=USD&sortBy=updatedAt&sortOrder=desc&page=1&limit=10`
},
{
name: 'Invalid state (should ignore invalid filter)',
url: `${BASE_URL}/transactions?state=INVALID_STATE`
},
{
name: 'Invalid currency (should ignore invalid filter)',
url: `${BASE_URL}/transactions?currency=INVALID_CURRENCY`
}
];
for (const testCase of testCases) {
console.log(`π ${testCase.name}`);
console.log(`π ${testCase.url}`);
try {
const response = await fetch(testCase.url);
const data = await response.json();
if (response.ok) {
console.log(`β
Status: ${response.status}`);
console.log(`π Results: ${data.data?.transactions?.length || 0} transactions`);
console.log(`π Total: ${data.data?.total || 0}, Page: ${data.data?.page || 1}, Limit: ${data.data?.limit || 20}`);
if (data.data?.transactions?.length > 0) {
const states = [...new Set(data.data.transactions.map(t => t.state))];
const currencies = [...new Set(data.data.transactions.map(t => t.currency))];
console.log(`π·οΈ States found: ${states.join(', ')}`);
console.log(`π° Currencies found: ${currencies.join(', ')}`);
// Show first transaction as sample
const firstTx = data.data.transactions[0];
console.log(`π Sample transaction: ${firstTx.id} - ${firstTx.state} - ${firstTx.amount} ${firstTx.currency}`);
}
} else {
console.log(`β Status: ${response.status}`);
console.log(`π¬ Error: ${data.error || data.message}`);
}
} catch (error) {
console.log(`π₯ Request failed: ${error.message}`);
}
console.log(''); // Empty line for readability
}
}
// Function to test specific state counts
async function testStateCounts() {
console.log('π Testing transaction counts by state...\n');
const states = ['PENDING', 'INITIALIZED', 'PAID', 'COMPLETED', 'PAYOUT_FAILED'];
for (const state of states) {
try {
const response = await fetch(`${BASE_URL}/transactions?state=${state}`);
const data = await response.json();
if (response.ok) {
console.log(`${state}: ${data.data?.total || 0} transactions`);
} else {
console.log(`${state}: Error - ${data.error || data.message}`);
}
} catch (error) {
console.log(`${state}: Request failed - ${error.message}`);
}
}
console.log('');
}
// Function to test currency distribution
async function testCurrencyDistribution() {
console.log('π° Testing transaction distribution by currency...\n');
const currencies = ['NGN', 'USD', 'GBP', 'EUR'];
for (const currency of currencies) {
try {
const response = await fetch(`${BASE_URL}/transactions?currency=${currency}`);
const data = await response.json();
if (response.ok) {
console.log(`${currency}: ${data.data?.total || 0} transactions`);
} else {
console.log(`${currency}: Error - ${data.error || data.message}`);
}
} catch (error) {
console.log(`${currency}: Request failed - ${error.message}`);
}
}
console.log('');
}
// Run all tests if this script is executed directly
if (require.main === module) {
(async () => {
try {
await testTransactionsByState();
await testStateCounts();
await testCurrencyDistribution();
console.log('π All tests completed!');
console.log('\nπ Endpoint Summary:');
console.log('- GET /transactions - Fetch all transactions with optional filtering');
console.log('- Supports filtering by: state, currency, paymentLinkId');
console.log('- Supports pagination: page, limit');
console.log('- Supports sorting: sortBy, sortOrder');
console.log('- Available states: PENDING, INITIALIZED, PAID, COMPLETED, PAYOUT_FAILED');
console.log('- Available currencies: NGN, USD, GBP, EUR');
} catch (error) {
console.error('Test execution failed:', error);
}
})();
}
module.exports = {
testTransactionsByState,
testStateCounts,
testCurrencyDistribution
};