import { CacheScope } from 'apollo-cache-control' ; import { CACHE_TTL , TRANSACTION_PURPOSE , TRANSACTION_STATE , TransactionRemarkText , } from 'common/enums' ; import { connectionFromArray , fromConnectionArgs , fromGlobalId , } from 'common/utils' ; import { WalletToTransactionsResolver } from 'definitions' ; const resolver = async ( { id : userId } , { input } , { dataSources : { paymentService } , viewer } , { cacheControl } ) => { const { id , states } = input ; const { take , skip } = fromConnectionArgs ( input ) ; let txId ; if ( id ) { txId = fromGlobalId ( id ) . id ; } const totalCount = await paymentService . totalTransactionCount ( { userId , id : txId , states : states as any , excludeCanceledLIKE : true , notIn : [ 'purpose' , [ TRANSACTION_PURPOSE . subscription ] ] , } ) ; if ( txId ) { cacheControl . setCacheHint ( { maxAge : CACHE_TTL . INSTANT , scope : CacheScope . Private , } ) ; } const transactions = await paymentService . findTransactions ( { userId , id : txId , states : states as any , excludeCanceledLIKE : true , notIn : [ 'purpose' , [ TRANSACTION_PURPOSE . subscription ] ] , skip , take , } ) ; const getTxMessage = ( tx ) => { if ( ! tx . remark ) { return '' ; } if ( tx . purpose === TRANSACTION_STATE . failed ) { const text = TransactionRemarkText [ viewer . language ] ; if ( Object . keys ( text ) . includes ( tx . remark ) ) { return text [ tx . remark as keyof typeof text ] ; } else { return text . unknow_error ; } } } ; return connectionFromArray ( transactions . map ( ( tx ) => ( { ... tx , amount : tx . delta , message : getTxMessage ( tx ) , } ) ) , input , totalCount ) ; } ; export default resolver ;