mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-08-25 05:42:27 +00:00
48 lines
1.8 KiB
TypeScript
48 lines
1.8 KiB
TypeScript
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<Account> {
|
|
return this.accountRepository.createAccount(data);
|
|
}
|
|
|
|
async getAccountByReferenceNumber(accountReference: string): Promise<Account> {
|
|
const account = await this.accountRepository.getAccountByReferenceNumber(accountReference);
|
|
if (!account) {
|
|
throw new UnprocessableEntityException('ACCOUNT.NOT_FOUND');
|
|
}
|
|
return account;
|
|
}
|
|
|
|
async getAccountByAccountNumber(accountNumber: string): Promise<Account> {
|
|
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);
|
|
}
|
|
}
|