fix: calculating child and parent balance

This commit is contained in:
Abdalhameed Ahmad
2025-09-09 20:31:48 +03:00
parent e1f50decfa
commit 039c95aa56
12 changed files with 83 additions and 26 deletions

View File

@ -49,9 +49,11 @@ export class AccountService {
async decreaseAccountBalance(accountReference: string, amount: number) {
const account = await this.getAccountByReferenceNumber(accountReference);
/**
*
* While there is no need to check for insufficient balance because this is a webhook handler,
* I just added this check to ensure we don't have corruption in our data especially if this service is used elsewhere.
* I just added this check to ensure we don't have corruption in our data.
*/
if (account.balance < amount) {
@ -61,6 +63,17 @@ export class AccountService {
return this.accountRepository.decreaseAccountBalance(accountReference, amount);
}
increaseReservedBalance(account: Account, amount: number) {
if (account.balance < account.reservedBalance + amount) {
throw new UnprocessableEntityException('ACCOUNT.INSUFFICIENT_BALANCE');
}
return this.accountRepository.increaseReservedBalance(account.id, amount);
}
decrementReservedBalance(account: Account, amount: number) {
return this.accountRepository.decreaseReservedBalance(account.id, amount);
}
//THIS IS A MOCK FUNCTION FOR TESTING PURPOSES ONLY
async fundIban(iban: string, amount: number) {
const account = await this.getAccountByIban(iban);