mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-11-26 00:24:54 +00:00
feat: transfer money to child
This commit is contained in:
@ -60,4 +60,10 @@ export class CardRepository {
|
||||
statusDescription: statusDescription,
|
||||
});
|
||||
}
|
||||
|
||||
updateCardLimit(cardId: string, newLimit: number) {
|
||||
return this.cardRepository.update(cardId, {
|
||||
limit: newLimit,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ -57,6 +57,28 @@ export class TransactionRepository {
|
||||
);
|
||||
}
|
||||
|
||||
createInternalChildTransaction(card: Card, amount: number): Promise<Transaction> {
|
||||
return this.transactionRepository.save(
|
||||
this.transactionRepository.create({
|
||||
transactionId: `CHILD-${card.id}-${Date.now()}`,
|
||||
transactionAmount: amount,
|
||||
transactionCurrency: '682',
|
||||
billingAmount: 0,
|
||||
settlementAmount: 0,
|
||||
transactionDate: new Date(),
|
||||
fees: 0,
|
||||
cardId: card.id,
|
||||
cardReference: card.cardReference,
|
||||
cardMaskedNumber: card.firstSixDigits + '******' + card.lastFourDigits,
|
||||
accountId: card.account!.id,
|
||||
transactionType: TransactionType.INTERNAL,
|
||||
accountReference: card.account!.accountReference,
|
||||
transactionScope: TransactionScope.CARD,
|
||||
vatOnFees: 0,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
findTransactionByReference(transactionId: string, accountReference: string): Promise<Transaction | null> {
|
||||
return this.transactionRepository.findOne({
|
||||
where: { transactionId, accountReference },
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import { BadRequestException, forwardRef, Inject, Injectable } from '@nestjs/common';
|
||||
import Decimal from 'decimal.js';
|
||||
import { Transactional } from 'typeorm-transactional';
|
||||
import { AccountCardStatusChangedWebhookRequest } from '~/common/modules/neoleap/dtos/requests';
|
||||
import { NeoLeapService } from '~/common/modules/neoleap/services';
|
||||
@ -10,12 +11,14 @@ import { CardColors } from '../enums';
|
||||
import { CardStatusMapper } from '../mappers/card-status.mapper';
|
||||
import { CardRepository } from '../repositories';
|
||||
import { AccountService } from './account.service';
|
||||
import { TransactionService } from './transaction.service';
|
||||
|
||||
@Injectable()
|
||||
export class CardService {
|
||||
constructor(
|
||||
private readonly cardRepository: CardRepository,
|
||||
private readonly accountService: AccountService,
|
||||
@Inject(forwardRef(() => TransactionService)) private readonly transactionService: TransactionService,
|
||||
@Inject(forwardRef(() => NeoLeapService)) private readonly neoleapService: NeoLeapService,
|
||||
@Inject(forwardRef(() => CustomerService)) private readonly customerService: CustomerService,
|
||||
) {}
|
||||
@ -99,4 +102,26 @@ export class CardService {
|
||||
|
||||
return this.neoleapService.getEmbossingInformation(card);
|
||||
}
|
||||
|
||||
async updateCardLimit(cardId: string, newLimit: number) {
|
||||
const { affected } = await this.cardRepository.updateCardLimit(cardId, newLimit);
|
||||
|
||||
if (affected === 0) {
|
||||
throw new BadRequestException('CARD.NOT_FOUND');
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional()
|
||||
async transferToChild(juniorId: string, amount: number) {
|
||||
const card = await this.getCardByCustomerId(juniorId);
|
||||
|
||||
const finalAmount = Decimal(amount).plus(card.limit);
|
||||
await Promise.all([
|
||||
this.neoleapService.updateCardControl(card.cardReference, finalAmount.toNumber()),
|
||||
this.updateCardLimit(card.id, finalAmount.toNumber()),
|
||||
this.transactionService.createInternalChildTransaction(card.id, amount),
|
||||
]);
|
||||
|
||||
return finalAmount.toNumber();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { Injectable, UnprocessableEntityException } from '@nestjs/common';
|
||||
import { forwardRef, Inject, Injectable, UnprocessableEntityException } from '@nestjs/common';
|
||||
import Decimal from 'decimal.js';
|
||||
import { Transactional } from 'typeorm-transactional';
|
||||
import {
|
||||
@ -15,7 +15,7 @@ export class TransactionService {
|
||||
constructor(
|
||||
private readonly transactionRepository: TransactionRepository,
|
||||
private readonly accountService: AccountService,
|
||||
private readonly cardService: CardService,
|
||||
@Inject(forwardRef(() => CardService)) private readonly cardService: CardService,
|
||||
) {}
|
||||
|
||||
@Transactional()
|
||||
@ -51,6 +51,12 @@ export class TransactionService {
|
||||
return transaction;
|
||||
}
|
||||
|
||||
async createInternalChildTransaction(cardId: string, amount: number) {
|
||||
const card = await this.cardService.getCardById(cardId);
|
||||
const transaction = await this.transactionRepository.createInternalChildTransaction(card, amount);
|
||||
return transaction;
|
||||
}
|
||||
|
||||
private async findExistingTransaction(transactionId: string, accountReference: string): Promise<Transaction | null> {
|
||||
const existingTransaction = await this.transactionRepository.findTransactionByReference(
|
||||
transactionId,
|
||||
|
||||
@ -8,8 +8,18 @@ import { ApiDataPageResponse, ApiDataResponse, ApiLangRequestHeader } from '~/co
|
||||
import { PageOptionsRequestDto } from '~/core/dtos';
|
||||
import { CustomParseUUIDPipe } from '~/core/pipes';
|
||||
import { ResponseFactory } from '~/core/utils';
|
||||
import { CreateJuniorRequestDto, SetThemeRequestDto, UpdateJuniorRequestDto } from '../dtos/request';
|
||||
import { JuniorResponseDto, QrCodeValidationResponseDto, ThemeResponseDto } from '../dtos/response';
|
||||
import {
|
||||
CreateJuniorRequestDto,
|
||||
SetThemeRequestDto,
|
||||
TransferToJuniorRequestDto,
|
||||
UpdateJuniorRequestDto,
|
||||
} from '../dtos/request';
|
||||
import {
|
||||
JuniorResponseDto,
|
||||
QrCodeValidationResponseDto,
|
||||
ThemeResponseDto,
|
||||
TransferToJuniorResponseDto,
|
||||
} from '../dtos/response';
|
||||
import { JuniorService } from '../services';
|
||||
|
||||
@Controller('juniors')
|
||||
@ -100,4 +110,18 @@ export class JuniorController {
|
||||
|
||||
return ResponseFactory.data(new QrCodeValidationResponseDto(junior));
|
||||
}
|
||||
|
||||
@Post(':juniorId/transfer')
|
||||
@UseGuards(RolesGuard)
|
||||
@AllowedRoles(Roles.GUARDIAN)
|
||||
@ApiDataResponse(TransferToJuniorResponseDto)
|
||||
async doesJuniorBelongToGuardian(
|
||||
@AuthenticatedUser() user: IJwtPayload,
|
||||
@Param('juniorId', CustomParseUUIDPipe) juniorId: string,
|
||||
@Body() body: TransferToJuniorRequestDto,
|
||||
) {
|
||||
const newAmount = await this.juniorService.transferToJunior(juniorId, body, user.sub);
|
||||
|
||||
return ResponseFactory.data(new TransferToJuniorResponseDto(newAmount));
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
export * from './create-junior.request.dto';
|
||||
export * from './set-theme.request.dto';
|
||||
export * from './transfer-to-junior.request.dto';
|
||||
export * from './update-junior.request.dto';
|
||||
|
||||
12
src/junior/dtos/request/transfer-to-junior.request.dto.ts
Normal file
12
src/junior/dtos/request/transfer-to-junior.request.dto.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNumber } from 'class-validator';
|
||||
import { i18nValidationMessage as i18n } from 'nestjs-i18n';
|
||||
|
||||
export class TransferToJuniorRequestDto {
|
||||
@ApiProperty({ example: 300.42 })
|
||||
@IsNumber(
|
||||
{ maxDecimalPlaces: 3 },
|
||||
{ message: i18n('validation.IsNumber', { path: 'general', property: 'transferToJunior.amount' }) },
|
||||
)
|
||||
amount!: number;
|
||||
}
|
||||
@ -2,3 +2,4 @@ export * from './junior.response.dto';
|
||||
export * from './qr-code-validation-details.response.dto';
|
||||
export * from './qr-code-validation.response.dto';
|
||||
export * from './theme.response.dto';
|
||||
export * from './transfer-to-junior.response.dto';
|
||||
|
||||
10
src/junior/dtos/response/transfer-to-junior.response.dto.ts
Normal file
10
src/junior/dtos/response/transfer-to-junior.response.dto.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
|
||||
export class TransferToJuniorResponseDto {
|
||||
@ApiProperty({ example: 300.42 })
|
||||
newAmount!: number;
|
||||
|
||||
constructor(newAmount: number) {
|
||||
this.newAmount = newAmount;
|
||||
}
|
||||
}
|
||||
@ -10,7 +10,12 @@ import { DocumentService, OciService } from '~/document/services';
|
||||
import { UserType } from '~/user/enums';
|
||||
import { UserService } from '~/user/services';
|
||||
import { UserTokenService } from '~/user/services/user-token.service';
|
||||
import { CreateJuniorRequestDto, SetThemeRequestDto, UpdateJuniorRequestDto } from '../dtos/request';
|
||||
import {
|
||||
CreateJuniorRequestDto,
|
||||
SetThemeRequestDto,
|
||||
TransferToJuniorRequestDto,
|
||||
UpdateJuniorRequestDto,
|
||||
} from '../dtos/request';
|
||||
import { Junior } from '../entities';
|
||||
import { JuniorRepository } from '../repositories';
|
||||
import { QrcodeService } from './qrcode.service';
|
||||
@ -167,6 +172,17 @@ export class JuniorService {
|
||||
return !!junior;
|
||||
}
|
||||
|
||||
async transferToJunior(juniorId: string, body: TransferToJuniorRequestDto, guardianId: string) {
|
||||
const doesBelong = await this.doesJuniorBelongToGuardian(guardianId, juniorId);
|
||||
|
||||
if (!doesBelong) {
|
||||
this.logger.error(`Junior ${juniorId} does not belong to guardian ${guardianId}`);
|
||||
throw new BadRequestException('JUNIOR.NOT_BELONG_TO_GUARDIAN');
|
||||
}
|
||||
|
||||
return this.cardService.transferToChild(juniorId, body.amount);
|
||||
}
|
||||
|
||||
private async prepareJuniorImages(juniors: Junior[]) {
|
||||
this.logger.log(`Preparing junior images`);
|
||||
await Promise.all(
|
||||
|
||||
Reference in New Issue
Block a user