import { getConnection , getMongoManager } from 'typeorm' ; import { injectable } from 'inversify' ; import { Transaction , TRANSACTION_STATUS_PENDING , TRANSACTION_STATUS_CONFIRMED , TRANSACTION_STATUS_FAILED } from '../../entities/transaction' ; import { User } from '../../entities/user' ; const DIRECTION_IN = 'in' ; const DIRECTION_OUT = 'out' ; /**
 *
 */ interface ExtendedTransaction extends Transaction { direction : string ; } /**
 *
 */ export interface TransactionRepositoryInterface { save ( tx ) : Promise < Transaction > ; getAllByWalletAndStatusIn ( wallet , statuses , types , skip , limit ) : Promise < ExtendedTransaction [ ] > ; getAllCountByWalletAndStatusIn ( wallet , statuses , types ) : Promise < number > ; getByHash ( transactionHash ) : Promise < Transaction > ; getByVerificationId ( verificationId ) : Promise < Transaction > ; } export function allStatuses ( ) { return [ TRANSACTION_STATUS_PENDING , TRANSACTION_STATUS_CONFIRMED , TRANSACTION_STATUS_FAILED ] ; } /**
 *
 */ @ injectable ( ) export class TransactionRepository implements TransactionRepositoryInterface { param tx save ( tx ) { return getConnection ( ) . getMongoRepository ( Transaction ) . save ( tx ) ; } param wallet param statuses param types param skip param limit async getAllByWalletAndStatusIn ( wallet , statuses , types , skip , limit ) { const data = await getMongoManager ( ) . createEntityCursor ( Transaction , { $and : [ { $or : [ { from : wallet . address } , { to : wallet . address } ] } , { status : { $in : statuses } } , { type : { $in : types } } ] } ) . skip ( skip ) . limit ( Math . max ( limit , 1 ) ) . sort ( { timestamp : - 1 } ) . toArray ( ) as ExtendedTransaction [ ] ; for ( let transaction of data ) { if ( transaction . from === wallet . address ) { transaction . direction = DIRECTION_OUT ; } else { transaction . direction = DIRECTION_IN ; } } return data ; } param wallet param statuses param types getAllCountByWalletAndStatusIn ( wallet , statuses , types ) { const count = getMongoManager ( ) . createEntityCursor ( Transaction , { $and : [ { $or : [ { from : wallet . address } , { to : wallet . address } ] } , { status : { $in : statuses } } , { type : { $in : types } } ] } ) . count ( false ) ; return count ; } param transactionHash getByHash ( transactionHash ) { return getConnection ( ) . getMongoRepository ( Transaction ) . findOne ( { transactionHash } ) ; } param verificationId async getByVerificationId ( verificationId ) { const result = await getConnection ( ) . getMongoRepository ( Transaction ) . createEntityCursor ( { 'verification.id' : verificationId } ) . toArray ( ) ; return result . pop ( ) ; } } const TransactionRepositoryType = Symbol ( 'TransactionRepositoryInterface' ) ; export { TransactionRepositoryType } ;