1- import { asArray , asObject , asOptional , asString } from 'cleaners'
1+ import { asArray , asObject , asOptional , asString , asValue } from 'cleaners'
22import Router from 'express-promise-router'
33
44import { reportsApps , reportsTransactions } from '../../indexApi'
5- import { asApps , asDbTx } from '../../types'
5+ import { asApps , asDbTx , StandardTx } from '../../types'
66
7- interface CheckTxsResponse {
7+ interface CheckTxsSuccessResponse
8+ extends Omit < StandardTx , 'rawTx' | 'usdValue' > {
9+ pluginId : string
10+ usdValue ?: number
11+ }
12+
13+ interface CheckTxsPartialSuccessResponse {
814 pluginId : string
915 orderId : string
10- error ?: string
1116 usdValue ?: number
1217}
1318
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+
1434const asCheckTxsFetch = asArray (
1535 asObject ( {
1636 key : asString ,
@@ -34,9 +54,10 @@ const CHECKTXS_BATCH_LIMIT = 100
3454export const checkTxsRouter = Router ( )
3555
3656checkTxsRouter . post ( '/' , async function ( req , res ) {
37- let queryResult
57+ let queryResult , params
3858 try {
3959 queryResult = asCheckTxsReq ( req . body )
60+ params = asCheckTxsParams ( req . query )
4061 } catch ( e ) {
4162 return res . status ( 400 ) . send ( `Missing Request fields.` )
4263 }
@@ -64,23 +85,34 @@ checkTxsRouter.post('/', async function(req, res) {
6485 const dbResult = await reportsTransactions . fetch ( { keys } )
6586 const cleanedResult = asCheckTxsFetch ( dbResult . rows )
6687 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 ,
7593 error : `Could not find transaction: ${ result . key } `
7694 }
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
77110 }
78- if ( result . doc != null ) tx . usdValue = result . doc . usdValue
79111 return tx
80112 } )
81113 res . json ( { appId, data } )
82114 } catch ( e ) {
83115 console . log ( e )
84- return res . status ( 500 ) . send ( `Internal Server Error.` )
116+ res . status ( 500 ) . send ( `Internal Server Error.` )
85117 }
86118} )
0 commit comments