import { Injectable, UnprocessableEntityException } from '@nestjs/common'; import { CreateApplicationResponse } from '~/common/modules/neoleap/dtos/response'; import { Account } from '../entities/account.entity'; import { AccountRepository } from '../repositories/account.repository'; @Injectable() export class AccountService { constructor(private readonly accountRepository: AccountRepository) {} createAccount(data: CreateApplicationResponse): Promise { return this.accountRepository.createAccount(data); } async getAccountByReferenceNumber(accountReference: string): Promise { const account = await this.accountRepository.getAccountByReferenceNumber(accountReference); if (!account) { throw new UnprocessableEntityException('ACCOUNT.NOT_FOUND'); } return account; } async getAccountByAccountNumber(accountNumber: string): Promise { const account = await this.accountRepository.getAccountByAccountNumber(accountNumber); 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); } }