wallet charge request

This commit is contained in:
2026-07-04 11:11:49 +03:30
parent 9f8d39938c
commit 92e97fa9d2
6 changed files with 87 additions and 9 deletions
@@ -14,6 +14,7 @@ import {
import { RestaurantCreditRequestStatus } from '../interface/restaurant-credit-request.interface';
import { CreateExternalInvoice } from '../interface/external-invoice.interface';
import { ChargeRequestDto } from '../dto/charge-request.dto';
import { FinishChargeRequestDto } from '../dto/finish-charge-request.dto';
import { RestMessage } from 'src/common/enums/message.enum';
@Injectable()
@@ -26,6 +27,16 @@ export class RestaurantWalletService {
private readonly configService: ConfigService,
) {}
async getCurrentBalance(restaurantId: string, em: EntityManager = this.defaultEm): Promise<number> {
const latestTransaction = await em.findOne(
RestaurantCreditTransaction,
{ restaurant: { id: restaurantId } },
{ orderBy: { createdAt: 'desc' } },
);
return Number(latestTransaction?.balance ?? 0);
}
async createTransaction(params: {
restaurant: Restaurant;
amount: number;
@@ -40,15 +51,13 @@ export class RestaurantWalletService {
return;
}
const currentCredit = Number(restaurant.credit ?? 0);
if (type === RestaurantCreditTransactionType.DEBIT && currentCredit < amount) {
const currentBalance = await this.getCurrentBalance(restaurant.id, em);
if (type === RestaurantCreditTransactionType.DEBIT && currentBalance < amount) {
throw new BadRequestException(insufficientBalanceMessage ?? 'اعتبار کیف پول رستوران کافی نیست');
}
const newBalance =
type === RestaurantCreditTransactionType.DEBIT ? currentCredit - amount : currentCredit + amount;
restaurant.credit = newBalance;
type === RestaurantCreditTransactionType.DEBIT ? currentBalance - amount : currentBalance + amount;
const creditTransaction = em.create(RestaurantCreditTransaction, {
restaurant,
@@ -58,7 +67,7 @@ export class RestaurantWalletService {
reason,
});
await em.persistAndFlush([restaurant, creditTransaction]);
await em.persistAndFlush(creditTransaction);
}
async chargeRequest(restId: string, dto: ChargeRequestDto): Promise<RestaurantCreditRequest> {
@@ -124,4 +133,39 @@ export class RestaurantWalletService {
return creditRequest;
}
async finishChargeRequest(dto: FinishChargeRequestDto): Promise<RestaurantCreditRequest> {
return this.defaultEm.transactional(async (em) => {
const creditRequest = await em.findOne(
RestaurantCreditRequest,
{ invoiceId: dto.invoiceId },
{ populate: ['restaurant'] },
);
if (!creditRequest) {
throw new NotFoundException(RestMessage.NOT_FOUND);
}
if (creditRequest.status === RestaurantCreditRequestStatus.PAID) {
return creditRequest;
}
if (creditRequest.status !== RestaurantCreditRequestStatus.PENDING) {
throw new BadRequestException('درخواست شارژ کیف پول قابل پردازش نیست');
}
await this.createTransaction({
restaurant: creditRequest.restaurant,
amount: Number(creditRequest.amount),
type: RestaurantCreditTransactionType.CREDIT,
reason: RestaurantCreditTransactionReason.DEPOSIT,
em,
});
creditRequest.status = RestaurantCreditRequestStatus.PAID;
await em.flush();
return creditRequest;
});
}
}