Skip to content

Commit ed6366e

Browse files
authored
Merge pull request #201 from EdgeApp/paul/checkTxsMoreInfo
Paul/check txs more info
2 parents 483660e + 8e2af50 commit ed6366e

File tree

2 files changed

+52
-19
lines changed

2 files changed

+52
-19
lines changed

src/routes/v1/checkTxs.ts

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
1-
import { asArray, asObject, asOptional, asString } from 'cleaners'
1+
import { asArray, asObject, asOptional, asString, asValue } from 'cleaners'
22
import Router from 'express-promise-router'
33

44
import { 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+
1434
const asCheckTxsFetch = asArray(
1535
asObject({
1636
key: asString,
@@ -34,9 +54,10 @@ const CHECKTXS_BATCH_LIMIT = 100
3454
export const checkTxsRouter = Router()
3555

3656
checkTxsRouter.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
})

src/types.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
asObject,
88
asOptional,
99
asString,
10+
asUndefined,
1011
asUnknown,
1112
asValue
1213
} from 'cleaners'
@@ -110,14 +111,14 @@ const asExchangeType = asValue('fiat', 'swap')
110111

111112
export const asStandardTx = asObject({
112113
orderId: asString,
113-
countryCode: asEither(asString, asNull),
114+
countryCode: asEither(asString, asNull, asUndefined),
114115
depositTxid: asOptional(asString),
115116
depositAddress: asOptional(asString),
116117
depositCurrency: asString,
117118
depositAmount: asSafeNumber,
118-
direction: asDirection,
119-
exchangeType: asExchangeType,
120-
paymentType: asEither(asFiatPaymentType, asNull),
119+
direction: asOptional(asDirection),
120+
exchangeType: asOptional(asExchangeType),
121+
paymentType: asEither(asFiatPaymentType, asNull, asUndefined),
121122
payoutTxid: asOptional(asString),
122123
payoutAddress: asOptional(asString),
123124
payoutCurrency: asString,

0 commit comments

Comments
 (0)