From 211c3f98071c957aa7b7ab7f4546b7edc12b659f Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Sat, 4 Jul 2026 14:53:42 +0330 Subject: [PATCH] add logs for charge wallet --- .../providers/restaurant-wallet.service.ts | 20 +++++++++++++++++++ .../providers/restaurants.service.ts | 16 ++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/modules/restaurants/providers/restaurant-wallet.service.ts b/src/modules/restaurants/providers/restaurant-wallet.service.ts index d748137..e64f71c 100644 --- a/src/modules/restaurants/providers/restaurant-wallet.service.ts +++ b/src/modules/restaurants/providers/restaurant-wallet.service.ts @@ -149,6 +149,8 @@ export class RestaurantWalletService { } async finishChargeRequest(dto: FinishChargeRequestDto): Promise { + this.logger.log(`Processing finish charge request for invoice ${dto.invoiceId}`); + return this.defaultEm.transactional(async (em) => { const creditRequest = await em.findOne( RestaurantCreditRequest, @@ -157,17 +159,30 @@ export class RestaurantWalletService { ); if (!creditRequest) { + this.logger.warn(`Credit request not found for invoice ${dto.invoiceId}`); throw new NotFoundException(RestMessage.NOT_FOUND); } + const restaurantId = creditRequest.restaurant.id; + if (creditRequest.status === RestaurantCreditRequestStatus.PAID) { + this.logger.warn( + `Duplicate finish charge callback for invoice ${dto.invoiceId} (restaurant: ${restaurantId}, already PAID)`, + ); return creditRequest; } if (creditRequest.status !== RestaurantCreditRequestStatus.PENDING) { + this.logger.warn( + `Cannot finish charge for invoice ${dto.invoiceId} (restaurant: ${restaurantId}, status: ${creditRequest.status})`, + ); throw new BadRequestException('درخواست شارژ کیف پول قابل پردازش نیست'); } + this.logger.log( + `Crediting wallet for restaurant ${restaurantId}: amount=${creditRequest.amount}, invoice=${dto.invoiceId}`, + ); + await this.createTransaction({ restaurant: creditRequest.restaurant, amount: Number(creditRequest.amount), @@ -179,6 +194,11 @@ export class RestaurantWalletService { creditRequest.status = RestaurantCreditRequestStatus.PAID; await em.flush(); + const newBalance = await this.getCurrentBalance(restaurantId, em); + this.logger.log( + `Charge request completed for invoice ${dto.invoiceId} (restaurant: ${restaurantId}, credited: ${creditRequest.amount}, new balance: ${newBalance})`, + ); + return creditRequest; }); } diff --git a/src/modules/restaurants/providers/restaurants.service.ts b/src/modules/restaurants/providers/restaurants.service.ts index f0b06a7..d2c296a 100644 --- a/src/modules/restaurants/providers/restaurants.service.ts +++ b/src/modules/restaurants/providers/restaurants.service.ts @@ -1,4 +1,4 @@ -import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common'; +import { BadRequestException, Injectable, Logger, NotFoundException } from '@nestjs/common'; import { SetupRestaurantDto } from '../dto/setup-restaurant.dto'; import { UpdateRestaurantDto } from '../dto/update-restaurant.dto'; import { UpdateRestaurantBgDto } from '../dto/update-restaurant-bg.dto'; @@ -41,6 +41,8 @@ import { FindRestaurantCreditRequestsDto } from '../dto/find-restaurant-credit-r @Injectable() export class RestaurantsService { + private readonly logger = new Logger(RestaurantsService.name); + constructor( private readonly em: EntityManager, private readonly restRepository: RestRepository, @@ -349,8 +351,16 @@ export class RestaurantsService { return this.restaurantWalletService.chargeRequest(restId, dto); } - finishChargeRequest(dto: FinishChargeRequestDto) { - return this.restaurantWalletService.finishChargeRequest(dto); + async finishChargeRequest(dto: FinishChargeRequestDto) { + this.logger.log(`Finishing charge request for invoice ${dto.invoiceId}`); + + const creditRequest = await this.restaurantWalletService.finishChargeRequest(dto); + + this.logger.log( + `Charge request finished for invoice ${dto.invoiceId} (restaurant: ${creditRequest.restaurant.id}, amount: ${creditRequest.amount}, status: ${creditRequest.status})`, + ); + + return creditRequest; } async updateBackground(id: string, dto: UpdateRestaurantBgDto): Promise {