mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-08-25 21:59:40 +00:00
feat: finish working on account transaction webhook
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Injectable, UnprocessableEntityException } from '@nestjs/common';
|
||||
import { Account } from '../entities/account.entity';
|
||||
import { AccountRepository } from '../repositories/account.repository';
|
||||
|
||||
@ -9,4 +9,30 @@ export class AccountService {
|
||||
createAccount(accountId: string): Promise<Account> {
|
||||
return this.accountRepository.createAccount(accountId);
|
||||
}
|
||||
|
||||
async getAccountByReferenceNumber(accountReference: string): Promise<Account> {
|
||||
const account = await this.accountRepository.getAccountByReferenceNumber(accountReference);
|
||||
if (!account) {
|
||||
throw new UnprocessableEntityException('ACCOUNT.NOT_FOUND');
|
||||
}
|
||||
return account;
|
||||
}
|
||||
|
||||
async creditAccountBalance(accountReference: string, amount: number) {
|
||||
return this.accountRepository.topUpAccountBalance(accountReference, amount);
|
||||
}
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
if (account.balance < amount) {
|
||||
throw new UnprocessableEntityException('ACCOUNT.INSUFFICIENT_BALANCE');
|
||||
}
|
||||
|
||||
return this.accountRepository.decreaseAccountBalance(accountReference, amount);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user