mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2026-03-10 15:21:45 +00:00
43 lines
1.5 KiB
TypeScript
43 lines
1.5 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { CustomerService } from '~/customer/services';
|
|
import { ParentHomeResponseDto, PagedParentTransfersResponseDto } from '~/card/dtos/responses';
|
|
import { TransactionService } from '~/card/services/transaction.service';
|
|
|
|
@Injectable()
|
|
export class GuardianTransactionsService {
|
|
constructor(
|
|
private readonly customerService: CustomerService,
|
|
private readonly transactionService: TransactionService,
|
|
) {}
|
|
|
|
async getHome(guardianId: string, size: number): Promise<ParentHomeResponseDto> {
|
|
const parent = await this.customerService.findCustomerById(guardianId);
|
|
const primaryCard = parent.cards?.[0];
|
|
|
|
let availableBalance = 0;
|
|
if (primaryCard) {
|
|
const hasLimit = typeof primaryCard.limit === 'number' && !Number.isNaN(primaryCard.limit);
|
|
const hasBalance = primaryCard.account && typeof primaryCard.account.balance === 'number';
|
|
if (hasLimit && hasBalance && primaryCard.limit > 0) {
|
|
availableBalance = Math.min(primaryCard.limit, primaryCard.account.balance);
|
|
} else if (hasBalance) {
|
|
availableBalance = primaryCard.account.balance;
|
|
}
|
|
}
|
|
|
|
const recentTransfers = await this.transactionService.getParentTransfersOnly(guardianId, 1, size);
|
|
|
|
return new ParentHomeResponseDto(availableBalance, recentTransfers);
|
|
}
|
|
|
|
async getTransfers(
|
|
guardianId: string,
|
|
page: number,
|
|
size: number,
|
|
): Promise<PagedParentTransfersResponseDto> {
|
|
return this.transactionService.getParentTransfersPaginated(guardianId, page, size);
|
|
}
|
|
}
|
|
|
|
|