diff --git a/src/card/services/card.service.ts b/src/card/services/card.service.ts index 23c6f6f..5c2dc4e 100644 --- a/src/card/services/card.service.ts +++ b/src/card/services/card.service.ts @@ -148,7 +148,11 @@ export class CardService { async transferToChild(juniorId: string, amount: number) { const card = await this.getCardByCustomerId(juniorId); - if (amount > card.account.balance - card.account.reservedBalance) { + const fundingAccount = card.parentId + ? await this.accountService.getAccountByCustomerId(card.parentId) + : card.account; + + if (amount > fundingAccount.balance - fundingAccount.reservedBalance) { throw new BadRequestException('CARD.INSUFFICIENT_BALANCE'); } @@ -156,7 +160,7 @@ export class CardService { await Promise.all([ this.neoleapService.updateCardControl(card.cardReference, finalAmount.toNumber()), this.updateCardLimit(card.id, finalAmount.toNumber()), - this.accountService.increaseReservedBalance(card.account, amount), + this.accountService.increaseReservedBalance(fundingAccount, amount), this.transactionService.createInternalChildTransaction(card.id, amount), ]); diff --git a/src/card/services/transaction.service.ts b/src/card/services/transaction.service.ts index 39ca853..613d10c 100644 --- a/src/card/services/transaction.service.ts +++ b/src/card/services/transaction.service.ts @@ -42,10 +42,18 @@ export class TransactionService { const total = new Decimal(body.transactionAmount).plus(body.billingAmount).plus(body.fees).plus(body.vatOnFees); if (card.customerType === CustomerType.CHILD) { - await Promise.all([ - this.accountService.decreaseAccountBalance(card.account.accountReference, total.toNumber()), - this.accountService.decrementReservedBalance(card.account, total.toNumber()), - ]); + if (card.parentId) { + const parentAccount = await this.accountService.getAccountByCustomerId(card.parentId); + await Promise.all([ + this.accountService.decreaseAccountBalance(parentAccount.accountReference, total.toNumber()), + this.accountService.decrementReservedBalance(parentAccount, total.toNumber()), + ]); + } else { + await Promise.all([ + this.accountService.decreaseAccountBalance(card.account.accountReference, total.toNumber()), + this.accountService.decrementReservedBalance(card.account, total.toNumber()), + ]); + } } else { await this.accountService.decreaseAccountBalance(card.account.accountReference, total.toNumber()); }