import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import moment from 'moment'; import { Repository } from 'typeorm'; import { AccountTransactionWebhookRequest, CardTransactionWebhookRequest, } from '~/common/modules/neoleap/dtos/requests'; import { Card } from '../entities'; import { Account } from '../entities/account.entity'; import { Transaction } from '../entities/transaction.entity'; import { TransactionScope, TransactionType } from '../enums'; @Injectable() export class TransactionRepository { constructor(@InjectRepository(Transaction) private transactionRepository: Repository) {} createCardTransaction(card: Card, transactionData: CardTransactionWebhookRequest): Promise { return this.transactionRepository.save( this.transactionRepository.create({ transactionId: transactionData.transactionId, cardReference: transactionData.cardId, transactionAmount: transactionData.transactionAmount, transactionCurrency: transactionData.transactionCurrency, billingAmount: transactionData.billingAmount, settlementAmount: transactionData.settlementAmount, transactionDate: moment(transactionData.date + transactionData.time, 'YYYYMMDDHHmmss').toDate(), rrn: transactionData.rrn, cardMaskedNumber: transactionData.cardMaskedNumber, fees: transactionData.fees, cardId: card.id, accountId: card.account!.id, transactionType: TransactionType.EXTERNAL, accountReference: card.account!.accountReference, transactionScope: TransactionScope.CARD, vatOnFees: transactionData.vatOnFees, }), ); } createAccountTransaction(account: Account, transactionData: AccountTransactionWebhookRequest): Promise { return this.transactionRepository.save( this.transactionRepository.create({ transactionId: transactionData.transactionId, transactionAmount: transactionData.amount, transactionCurrency: transactionData.currency, billingAmount: 0, settlementAmount: 0, transactionDate: moment(transactionData.date + transactionData.time, 'YYYYMMDDHHmmss').toDate(), fees: 0, accountReference: account.accountReference, accountId: account.id, transactionType: TransactionType.EXTERNAL, transactionScope: TransactionScope.ACCOUNT, vatOnFees: 0, }), ); } findTransactionByReference(transactionId: string, accountReference: string): Promise { return this.transactionRepository.findOne({ where: { transactionId, accountReference }, }); } }