1
- import { asArray , asObject , asOptional , asString } from 'cleaners'
1
+ import { asArray , asObject , asOptional , asString , asValue } from 'cleaners'
2
2
import Router from 'express-promise-router'
3
3
4
4
import { reportsApps , reportsTransactions } from '../../indexApi'
5
- import { asApps , asDbTx } from '../../types'
5
+ import { asApps , asDbTx , StandardTx } from '../../types'
6
6
7
- interface CheckTxsResponse {
7
+ interface CheckTxsSuccessResponse
8
+ extends Omit < StandardTx , 'rawTx' | 'usdValue' > {
9
+ pluginId : string
10
+ usdValue ?: number
11
+ }
12
+
13
+ interface CheckTxsPartialSuccessResponse {
8
14
pluginId : string
9
15
orderId : string
10
- error ?: string
11
16
usdValue ?: number
12
17
}
13
18
19
+ interface CheckTxsFailureResponse {
20
+ pluginId : string
21
+ orderId : string
22
+ error : string
23
+ }
24
+
25
+ type CheckTxsResponse =
26
+ | CheckTxsSuccessResponse
27
+ | CheckTxsPartialSuccessResponse
28
+ | CheckTxsFailureResponse
29
+
30
+ const asCheckTxsParams = asObject ( {
31
+ info : asOptional ( asValue ( 'all' ) )
32
+ } )
33
+
14
34
const asCheckTxsFetch = asArray (
15
35
asObject ( {
16
36
key : asString ,
@@ -34,9 +54,10 @@ const CHECKTXS_BATCH_LIMIT = 100
34
54
export const checkTxsRouter = Router ( )
35
55
36
56
checkTxsRouter . post ( '/' , async function ( req , res ) {
37
- let queryResult
57
+ let queryResult , params
38
58
try {
39
59
queryResult = asCheckTxsReq ( req . body )
60
+ params = asCheckTxsParams ( req . query )
40
61
} catch ( e ) {
41
62
return res . status ( 400 ) . send ( `Missing Request fields.` )
42
63
}
@@ -64,23 +85,34 @@ checkTxsRouter.post('/', async function(req, res) {
64
85
const dbResult = await reportsTransactions . fetch ( { keys } )
65
86
const cleanedResult = asCheckTxsFetch ( dbResult . rows )
66
87
const data : CheckTxsResponse [ ] = cleanedResult . map ( ( result , index ) => {
67
- const tx : CheckTxsResponse = {
68
- pluginId : queryResult . data [ index ] . pluginId ,
69
- orderId : queryResult . data [ index ] . orderId ,
70
- usdValue : undefined
71
- }
72
- if ( result . error != null ) {
73
- return {
74
- ...tx ,
88
+ const { doc } = result
89
+ if ( result . error != null || doc == null ) {
90
+ const txError : CheckTxsFailureResponse = {
91
+ pluginId : queryResult . data [ index ] . pluginId ,
92
+ orderId : queryResult . data [ index ] . orderId ,
75
93
error : `Could not find transaction: ${ result . key } `
76
94
}
95
+ return txError
96
+ }
97
+ const usdValue = doc . usdValue >= 0 ? doc . usdValue : undefined
98
+ if ( params . info === 'all' ) {
99
+ const tx : CheckTxsSuccessResponse = {
100
+ pluginId : queryResult . data [ index ] . pluginId ,
101
+ ...doc
102
+ }
103
+ tx . usdValue = usdValue
104
+ return tx
105
+ }
106
+ const tx : CheckTxsPartialSuccessResponse = {
107
+ pluginId : queryResult . data [ index ] . pluginId ,
108
+ orderId : queryResult . data [ index ] . orderId ,
109
+ usdValue
77
110
}
78
- if ( result . doc != null ) tx . usdValue = result . doc . usdValue
79
111
return tx
80
112
} )
81
113
res . json ( { appId, data } )
82
114
} catch ( e ) {
83
115
console . log ( e )
84
- return res . status ( 500 ) . send ( `Internal Server Error.` )
116
+ res . status ( 500 ) . send ( `Internal Server Error.` )
85
117
}
86
118
} )
0 commit comments