1
+ import express from 'express' ;
2
+ import transactions from '../db/transactions.js' ;
3
+
4
+ const router = express . Router ( ) ;
5
+
6
+ // POST a new transaction
7
+ router . post ( '/transactions' , async ( req , res , next ) => {
8
+ const { signature, ip_address, wallet_address, github_username, timestamp } = req . body ;
9
+
10
+ if ( ! signature || ! ip_address || ! wallet_address || ! timestamp ) {
11
+ return res . status ( 400 ) . json ( { message : 'Missing required fields (signature, ip_address, wallet_address, timestamp).' } ) ;
12
+ }
13
+
14
+ try {
15
+ const newTransaction = await transactions . createTransaction ( signature , ip_address , wallet_address , github_username ?? '' , timestamp ) ;
16
+ res . status ( 201 ) . json ( newTransaction ) ;
17
+ } catch ( error ) {
18
+ console . error ( 'Error creating transaction:' , error ) ;
19
+ next ( error ) ;
20
+ }
21
+ } ) ;
22
+
23
+ // GET the most recent transaction based on wallet, GitHub or IP
24
+ router . get ( '/transactions/last' , async ( req , res , next ) => {
25
+ const { wallet_address, github_username, ip_address } = req . query ;
26
+
27
+ if ( ! wallet_address && ! github_username && ! ip_address ) {
28
+ return res . status ( 400 ) . json ( { message : 'At least one parameter (wallet_address, github_username, or ip_address) is required.' } ) ;
29
+ }
30
+
31
+ try {
32
+ const lastTransaction = await transactions . getLastTransaction ( { wallet_address, github_username, ip_address } ) ;
33
+
34
+ if ( lastTransaction ) {
35
+ res . status ( 200 ) . json ( lastTransaction ) ;
36
+ } else {
37
+ res . status ( 204 ) . json ( { message : 'No transaction found for the given criteria.' } ) ;
38
+ }
39
+ } catch ( error ) {
40
+ console . error ( 'Error fetching last transaction:' , error ) ;
41
+ next ( error ) ;
42
+ }
43
+ } ) ;
44
+
45
+ // DELETE a transaction by signature
46
+ router . delete ( '/transactions/:signature' , async ( req , res , next ) => {
47
+ const { signature } = req . params ;
48
+
49
+ try {
50
+ const deletedTransaction = await transactions . deleteTransaction ( signature ) ;
51
+ if ( deletedTransaction ) {
52
+ res . status ( 200 ) . json ( deletedTransaction ) ;
53
+ } else {
54
+ console . warn ( `Transaction not found for signature "${ signature } "` ) ;
55
+ res . status ( 404 ) . json ( { message : 'Transaction not found' } ) ;
56
+ }
57
+ } catch ( error ) {
58
+ console . error ( `Error deleting transaction for signature "${ signature } ":` , error ) ;
59
+ next ( error ) ;
60
+ }
61
+ } ) ;
62
+
63
+ export default router ;
0 commit comments