mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-11-26 16:44:54 +00:00
Compare commits
5 Commits
5ffe18ede3
...
money-requ
| Author | SHA1 | Date | |
|---|---|---|---|
| 2172051093 | |||
| d6fb5f48d9 | |||
| 99af65a300 | |||
| 0c9b40132a | |||
| 3b295ea79f |
@ -64,9 +64,8 @@ export class AccountService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
increaseReservedBalance(account: Account, amount: number) {
|
increaseReservedBalance(account: Account, amount: number) {
|
||||||
if (account.balance < account.reservedBalance + amount) {
|
// Balance check is performed by the caller (e.g., transferToChild)
|
||||||
throw new UnprocessableEntityException('CARD.INSUFFICIENT_BALANCE');
|
// to ensure correct account (guardian vs child) is validated
|
||||||
}
|
|
||||||
return this.accountRepository.increaseReservedBalance(account.id, amount);
|
return this.accountRepository.increaseReservedBalance(account.id, amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -148,7 +148,18 @@ export class CardService {
|
|||||||
async transferToChild(juniorId: string, amount: number) {
|
async transferToChild(juniorId: string, amount: number) {
|
||||||
const card = await this.getCardByCustomerId(juniorId);
|
const card = await this.getCardByCustomerId(juniorId);
|
||||||
|
|
||||||
if (amount > card.account.balance - card.account.reservedBalance) {
|
this.logger.debug(`Transfer to child - juniorId: ${juniorId}, parentId: ${card.parentId}, cardId: ${card.id}`);
|
||||||
|
this.logger.debug(`Card account - balance: ${card.account.balance}, reserved: ${card.account.reservedBalance}`);
|
||||||
|
|
||||||
|
const fundingAccount = card.parentId
|
||||||
|
? await this.accountService.getAccountByCustomerId(card.parentId)
|
||||||
|
: card.account;
|
||||||
|
|
||||||
|
this.logger.debug(`Funding account - balance: ${fundingAccount.balance}, reserved: ${fundingAccount.reservedBalance}, available: ${fundingAccount.balance - fundingAccount.reservedBalance}`);
|
||||||
|
this.logger.debug(`Amount requested: ${amount}`);
|
||||||
|
|
||||||
|
if (amount > fundingAccount.balance - fundingAccount.reservedBalance) {
|
||||||
|
this.logger.error(`Insufficient balance - requested: ${amount}, available: ${fundingAccount.balance - fundingAccount.reservedBalance}`);
|
||||||
throw new BadRequestException('CARD.INSUFFICIENT_BALANCE');
|
throw new BadRequestException('CARD.INSUFFICIENT_BALANCE');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,7 +167,7 @@ export class CardService {
|
|||||||
await Promise.all([
|
await Promise.all([
|
||||||
this.neoleapService.updateCardControl(card.cardReference, finalAmount.toNumber()),
|
this.neoleapService.updateCardControl(card.cardReference, finalAmount.toNumber()),
|
||||||
this.updateCardLimit(card.id, finalAmount.toNumber()),
|
this.updateCardLimit(card.id, finalAmount.toNumber()),
|
||||||
this.accountService.increaseReservedBalance(card.account, amount),
|
this.accountService.increaseReservedBalance(fundingAccount, amount),
|
||||||
this.transactionService.createInternalChildTransaction(card.id, amount),
|
this.transactionService.createInternalChildTransaction(card.id, amount),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|||||||
@ -42,10 +42,18 @@ export class TransactionService {
|
|||||||
const total = new Decimal(body.transactionAmount).plus(body.billingAmount).plus(body.fees).plus(body.vatOnFees);
|
const total = new Decimal(body.transactionAmount).plus(body.billingAmount).plus(body.fees).plus(body.vatOnFees);
|
||||||
|
|
||||||
if (card.customerType === CustomerType.CHILD) {
|
if (card.customerType === CustomerType.CHILD) {
|
||||||
|
if (card.parentId) {
|
||||||
|
const parentAccount = await this.accountService.getAccountByCustomerId(card.parentId);
|
||||||
|
await Promise.all([
|
||||||
|
this.accountService.decreaseAccountBalance(parentAccount.accountReference, total.toNumber()),
|
||||||
|
this.accountService.decrementReservedBalance(parentAccount, total.toNumber()),
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
this.accountService.decreaseAccountBalance(card.account.accountReference, total.toNumber()),
|
this.accountService.decreaseAccountBalance(card.account.accountReference, total.toNumber()),
|
||||||
this.accountService.decrementReservedBalance(card.account, total.toNumber()),
|
this.accountService.decrementReservedBalance(card.account, total.toNumber()),
|
||||||
]);
|
]);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
await this.accountService.decreaseAccountBalance(card.account.accountReference, total.toNumber());
|
await this.accountService.decreaseAccountBalance(card.account.accountReference, total.toNumber());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,6 +19,10 @@
|
|||||||
"TOKEN_EXPIRED": "رمز المستخدم منتهي الصلاحية."
|
"TOKEN_EXPIRED": "رمز المستخدم منتهي الصلاحية."
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"QR": {
|
||||||
|
"CODE_USED_OR_EXPIRED": "تم استخدام رمز QR مسبقًا أو انتهت صلاحيته."
|
||||||
|
},
|
||||||
|
|
||||||
"USER": {
|
"USER": {
|
||||||
"PHONE_ALREADY_VERIFIED": "تم التحقق من رقم الهاتف بالفعل.",
|
"PHONE_ALREADY_VERIFIED": "تم التحقق من رقم الهاتف بالفعل.",
|
||||||
"EMAIL_ALREADY_VERIFIED": "تم التحقق من عنوان البريد الإلكتروني بالفعل.",
|
"EMAIL_ALREADY_VERIFIED": "تم التحقق من عنوان البريد الإلكتروني بالفعل.",
|
||||||
|
|||||||
@ -19,6 +19,10 @@
|
|||||||
"TOKEN_EXPIRED": "The user token has expired."
|
"TOKEN_EXPIRED": "The user token has expired."
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"QR": {
|
||||||
|
"CODE_USED_OR_EXPIRED": "The QR code has already been used or expired."
|
||||||
|
},
|
||||||
|
|
||||||
"USER": {
|
"USER": {
|
||||||
"PHONE_ALREADY_VERIFIED": "The phone number has already been verified.",
|
"PHONE_ALREADY_VERIFIED": "The phone number has already been verified.",
|
||||||
"EMAIL_ALREADY_VERIFIED": "The email address has already been verified.",
|
"EMAIL_ALREADY_VERIFIED": "The email address has already been verified.",
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import { Roles } from '~/auth/enums';
|
|||||||
import { CardService, TransactionService } from '~/card/services';
|
import { CardService, TransactionService } from '~/card/services';
|
||||||
import { NeoLeapService } from '~/common/modules/neoleap/services';
|
import { NeoLeapService } from '~/common/modules/neoleap/services';
|
||||||
import { PageOptionsRequestDto } from '~/core/dtos';
|
import { PageOptionsRequestDto } from '~/core/dtos';
|
||||||
|
import { ErrorCategory } from '~/core/enums';
|
||||||
import { setIf } from '~/core/utils';
|
import { setIf } from '~/core/utils';
|
||||||
import { CustomerService } from '~/customer/services';
|
import { CustomerService } from '~/customer/services';
|
||||||
import { DocumentService, OciService } from '~/document/services';
|
import { DocumentService, OciService } from '~/document/services';
|
||||||
@ -158,7 +159,14 @@ export class JuniorService {
|
|||||||
async validateToken(token: string) {
|
async validateToken(token: string) {
|
||||||
this.logger.log(`Validating token ${token}`);
|
this.logger.log(`Validating token ${token}`);
|
||||||
const juniorId = await this.userTokenService.validateToken(token, UserType.JUNIOR);
|
const juniorId = await this.userTokenService.validateToken(token, UserType.JUNIOR);
|
||||||
return this.findJuniorById(juniorId!, true);
|
const junior = await this.findJuniorById(juniorId!, true);
|
||||||
|
|
||||||
|
if (junior.customer?.user?.password) {
|
||||||
|
this.logger.error(`Token ${token} already used for junior ${juniorId}`);
|
||||||
|
throw new BadRequestException({ message: 'QR.CODE_USED_OR_EXPIRED', category: ErrorCategory.BUSINESS_ERROR });
|
||||||
|
}
|
||||||
|
|
||||||
|
return junior;
|
||||||
}
|
}
|
||||||
|
|
||||||
async generateToken(juniorId: string) {
|
async generateToken(juniorId: string) {
|
||||||
|
|||||||
Reference in New Issue
Block a user