mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-11-26 08:34:55 +00:00
ZOD-309-child-transaction-history-parent-→-child-transfers
This commit is contained in:
@ -2,7 +2,7 @@ import { BadRequestException, Injectable, Logger } from '@nestjs/common';
|
||||
import { IsNull, Not } from 'typeorm';
|
||||
import { Transactional } from 'typeorm-transactional';
|
||||
import { Roles } from '~/auth/enums';
|
||||
import { CardService } from '~/card/services';
|
||||
import { CardService, TransactionService } from '~/card/services';
|
||||
import { NeoLeapService } from '~/common/modules/neoleap/services';
|
||||
import { PageOptionsRequestDto } from '~/core/dtos';
|
||||
import { setIf } from '~/core/utils';
|
||||
@ -20,6 +20,7 @@ import {
|
||||
import { Junior } from '../entities';
|
||||
import { JuniorRepository } from '../repositories';
|
||||
import { QrcodeService } from './qrcode.service';
|
||||
import { JuniorHomeResponseDto, PagedChildTransfersResponseDto } from '~/card/dtos/responses';
|
||||
|
||||
@Injectable()
|
||||
export class JuniorService {
|
||||
@ -34,6 +35,7 @@ export class JuniorService {
|
||||
private readonly qrCodeService: QrcodeService,
|
||||
private readonly neoleapService: NeoLeapService,
|
||||
private readonly cardService: CardService,
|
||||
private readonly transactionService: TransactionService,
|
||||
) {}
|
||||
|
||||
@Transactional()
|
||||
@ -221,6 +223,56 @@ export class JuniorService {
|
||||
return this.cardService.getWeeklySummary(juniorId);
|
||||
}
|
||||
|
||||
async getJuniorHome(juniorId: string, userId: string, size: number): Promise<JuniorHomeResponseDto> {
|
||||
this.logger.log(`Getting home for junior ${juniorId}`);
|
||||
|
||||
// Check if user is the junior themselves or their guardian
|
||||
let junior: Junior | null;
|
||||
if (juniorId === userId) {
|
||||
// User is the junior accessing their own home
|
||||
junior = await this.findJuniorById(juniorId, false);
|
||||
} else {
|
||||
// User might be the guardian accessing junior's home
|
||||
junior = await this.findJuniorById(juniorId, false, userId);
|
||||
}
|
||||
|
||||
if (!junior) {
|
||||
throw new BadRequestException('JUNIOR.NOT_FOUND');
|
||||
}
|
||||
|
||||
const card = junior.customer?.cards?.[0];
|
||||
const availableBalance = card ? Math.min(card.limit, card.account.balance) : 0;
|
||||
|
||||
const recentTransfers = await this.transactionService.getChildTransfers(juniorId, 1, size);
|
||||
|
||||
return new JuniorHomeResponseDto(availableBalance, recentTransfers);
|
||||
}
|
||||
|
||||
async getJuniorTransfers(
|
||||
juniorId: string,
|
||||
userId: string,
|
||||
page: number,
|
||||
size: number,
|
||||
): Promise<PagedChildTransfersResponseDto> {
|
||||
this.logger.log(`Getting transfers for junior ${juniorId}`);
|
||||
|
||||
// Check if user is the junior themselves or their guardian
|
||||
let junior: Junior | null;
|
||||
if (juniorId === userId) {
|
||||
// User is the junior accessing their own transfers
|
||||
junior = await this.findJuniorById(juniorId, false);
|
||||
} else {
|
||||
// User might be the guardian accessing junior's transfers
|
||||
junior = await this.findJuniorById(juniorId, false, userId);
|
||||
}
|
||||
|
||||
if (!junior) {
|
||||
throw new BadRequestException('JUNIOR.NOT_FOUND');
|
||||
}
|
||||
|
||||
return this.transactionService.getChildTransfersPaginated(juniorId, page, size);
|
||||
}
|
||||
|
||||
private async prepareJuniorImages(juniors: Junior[]) {
|
||||
this.logger.log(`Preparing junior images`);
|
||||
await Promise.all(
|
||||
|
||||
Reference in New Issue
Block a user