import { Transaction } from '@entity/Transaction' import { IsNull } from 'typeorm' import { getDataSource } from '@/typeorm/DataSource' // https://www.artima.com/articles/the-dci-architecture-a-new-vision-of-object-oriented-programming export const TransactionRepository = getDataSource() .getRepository(Transaction) .extend({ findBySignature(signature: Buffer): Promise { return this.findOneBy({ signature: Buffer.from(signature) }) }, findByMessageId(iotaMessageId: string): Promise { return this.findOneBy({ iotaMessageId: Buffer.from(iotaMessageId, 'hex') }) }, async getNextPendingTransaction(): Promise { return await this.findOne({ where: { iotaMessageId: IsNull() }, order: { createdAt: 'ASC' }, relations: { signingAccount: true }, }) }, findExistingTransactionAndMissingMessageIds(messageIDsHex: string[]): Promise { return this.createQueryBuilder('Transaction') .where('HEX(Transaction.iota_message_id) IN (:...messageIDs)', { messageIDs: messageIDsHex, }) .leftJoinAndSelect('Transaction.community', 'Community') .leftJoinAndSelect('Transaction.otherCommunity', 'OtherCommunity') .leftJoinAndSelect('Transaction.recipientAccount', 'RecipientAccount') .leftJoinAndSelect('Transaction.backendTransactions', 'BackendTransactions') .leftJoinAndSelect('RecipientAccount.user', 'RecipientUser') .leftJoinAndSelect('Transaction.signingAccount', 'SigningAccount') .leftJoinAndSelect('SigningAccount.user', 'SigningUser') .getMany() }, removeConfirmedTransaction(transactions: Transaction[]): Transaction[] { return transactions.filter( (transaction: Transaction) => transaction.runningHash === undefined || transaction.runningHash.length === 0, ) }, })