feat(wallet): add detailed logging for freeze-for-planting API
Added comprehensive logging to track: - Request parameters (userId, amount, orderId) - Wallet balance information - Balance check results - Freeze operation status This helps debug the 400 error when planting-service calls wallet-service. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
d84fc9409b
commit
3714edb30b
|
|
@ -1,4 +1,4 @@
|
||||||
import { Controller, Get, Post, Body, Param } from '@nestjs/common';
|
import { Controller, Get, Post, Body, Param, Logger } from '@nestjs/common';
|
||||||
import { ApiTags, ApiOperation, ApiResponse, ApiParam } from '@nestjs/swagger';
|
import { ApiTags, ApiOperation, ApiResponse, ApiParam } from '@nestjs/swagger';
|
||||||
import { WalletApplicationService } from '@/application/services';
|
import { WalletApplicationService } from '@/application/services';
|
||||||
import { GetMyWalletQuery } from '@/application/queries';
|
import { GetMyWalletQuery } from '@/application/queries';
|
||||||
|
|
@ -19,6 +19,8 @@ import { Public } from '@/shared/decorators';
|
||||||
@ApiTags('Internal Wallet API')
|
@ApiTags('Internal Wallet API')
|
||||||
@Controller('wallets')
|
@Controller('wallets')
|
||||||
export class InternalWalletController {
|
export class InternalWalletController {
|
||||||
|
private readonly logger = new Logger(InternalWalletController.name);
|
||||||
|
|
||||||
constructor(private readonly walletService: WalletApplicationService) {}
|
constructor(private readonly walletService: WalletApplicationService) {}
|
||||||
|
|
||||||
@Get(':userId/balance')
|
@Get(':userId/balance')
|
||||||
|
|
@ -60,13 +62,26 @@ export class InternalWalletController {
|
||||||
async freezeForPlanting(
|
async freezeForPlanting(
|
||||||
@Body() dto: { userId: string; amount: number; orderId: string },
|
@Body() dto: { userId: string; amount: number; orderId: string },
|
||||||
) {
|
) {
|
||||||
const command = new FreezeForPlantingCommand(
|
this.logger.log(`========== freeze-for-planting 请求 ==========`);
|
||||||
dto.userId,
|
this.logger.log(`请求参数: ${JSON.stringify(dto)}`);
|
||||||
dto.amount,
|
this.logger.log(` userId: ${dto.userId}`);
|
||||||
dto.orderId,
|
this.logger.log(` amount: ${dto.amount}`);
|
||||||
);
|
this.logger.log(` orderId: ${dto.orderId}`);
|
||||||
const result = await this.walletService.freezeForPlanting(command);
|
|
||||||
return result;
|
try {
|
||||||
|
const command = new FreezeForPlantingCommand(
|
||||||
|
dto.userId,
|
||||||
|
dto.amount,
|
||||||
|
dto.orderId,
|
||||||
|
);
|
||||||
|
const result = await this.walletService.freezeForPlanting(command);
|
||||||
|
this.logger.log(`冻结成功: ${JSON.stringify(result)}`);
|
||||||
|
return result;
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error(`冻结失败: ${error.message}`);
|
||||||
|
this.logger.error(`错误堆栈: ${error.stack}`);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post('confirm-planting-deduction')
|
@Post('confirm-planting-deduction')
|
||||||
|
|
|
||||||
|
|
@ -187,11 +187,18 @@ export class WalletApplicationService {
|
||||||
success: boolean;
|
success: boolean;
|
||||||
frozenAmount: number;
|
frozenAmount: number;
|
||||||
}> {
|
}> {
|
||||||
|
this.logger.log(`[freezeForPlanting] ========== 开始处理 ==========`);
|
||||||
|
this.logger.log(`[freezeForPlanting] userId: ${command.userId}`);
|
||||||
|
this.logger.log(`[freezeForPlanting] amount: ${command.amount}`);
|
||||||
|
this.logger.log(`[freezeForPlanting] orderId: ${command.orderId}`);
|
||||||
|
|
||||||
const userId = BigInt(command.userId);
|
const userId = BigInt(command.userId);
|
||||||
const amount = Money.USDT(command.amount);
|
const amount = Money.USDT(command.amount);
|
||||||
|
this.logger.log(`[freezeForPlanting] 解析后 amount.value: ${amount.value}`);
|
||||||
|
|
||||||
// 幂等性检查:通过 orderId 检查是否已经冻结
|
// 幂等性检查:通过 orderId 检查是否已经冻结
|
||||||
const existingEntries = await this.ledgerRepo.findByRefOrderId(command.orderId);
|
const existingEntries = await this.ledgerRepo.findByRefOrderId(command.orderId);
|
||||||
|
this.logger.log(`[freezeForPlanting] 已存在的流水条数: ${existingEntries.length}`);
|
||||||
const alreadyFrozen = existingEntries.some(
|
const alreadyFrozen = existingEntries.some(
|
||||||
(entry) => entry.entryType === LedgerEntryType.PLANT_FREEZE,
|
(entry) => entry.entryType === LedgerEntryType.PLANT_FREEZE,
|
||||||
);
|
);
|
||||||
|
|
@ -204,19 +211,32 @@ export class WalletApplicationService {
|
||||||
|
|
||||||
const wallet = await this.walletRepo.findByUserId(userId);
|
const wallet = await this.walletRepo.findByUserId(userId);
|
||||||
if (!wallet) {
|
if (!wallet) {
|
||||||
|
this.logger.error(`[freezeForPlanting] 钱包不存在: userId=${command.userId}`);
|
||||||
throw new WalletNotFoundError(`userId: ${command.userId}`);
|
throw new WalletNotFoundError(`userId: ${command.userId}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.logger.log(`[freezeForPlanting] 钱包信息:`);
|
||||||
|
this.logger.log(`[freezeForPlanting] walletId: ${wallet.walletId}`);
|
||||||
|
this.logger.log(`[freezeForPlanting] accountSequence: ${wallet.accountSequence}`);
|
||||||
|
this.logger.log(`[freezeForPlanting] USDT可用余额: ${wallet.balances.usdt.available.value}`);
|
||||||
|
this.logger.log(`[freezeForPlanting] USDT冻结余额: ${wallet.balances.usdt.frozen.value}`);
|
||||||
|
|
||||||
// 检查余额是否足够
|
// 检查余额是否足够
|
||||||
if (wallet.balances.usdt.available.lessThan(amount)) {
|
if (wallet.balances.usdt.available.lessThan(amount)) {
|
||||||
|
this.logger.error(`[freezeForPlanting] 余额不足!`);
|
||||||
|
this.logger.error(`[freezeForPlanting] 需要: ${command.amount} USDT`);
|
||||||
|
this.logger.error(`[freezeForPlanting] 当前可用: ${wallet.balances.usdt.available.value} USDT`);
|
||||||
throw new BadRequestException(
|
throw new BadRequestException(
|
||||||
`余额不足: 需要 ${command.amount} USDT, 当前可用 ${wallet.balances.usdt.available.value} USDT`,
|
`余额不足: 需要 ${command.amount} USDT, 当前可用 ${wallet.balances.usdt.available.value} USDT`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.logger.log(`[freezeForPlanting] 余额检查通过,开始冻结...`);
|
||||||
|
|
||||||
// 冻结资金
|
// 冻结资金
|
||||||
wallet.freeze(amount);
|
wallet.freeze(amount);
|
||||||
await this.walletRepo.save(wallet);
|
await this.walletRepo.save(wallet);
|
||||||
|
this.logger.log(`[freezeForPlanting] 钱包已保存`);
|
||||||
|
|
||||||
// 记录冻结流水
|
// 记录冻结流水
|
||||||
const ledgerEntry = LedgerEntry.create({
|
const ledgerEntry = LedgerEntry.create({
|
||||||
|
|
@ -229,10 +249,11 @@ export class WalletApplicationService {
|
||||||
memo: 'Plant freeze',
|
memo: 'Plant freeze',
|
||||||
});
|
});
|
||||||
await this.ledgerRepo.save(ledgerEntry);
|
await this.ledgerRepo.save(ledgerEntry);
|
||||||
|
this.logger.log(`[freezeForPlanting] 流水已记录`);
|
||||||
|
|
||||||
await this.walletCacheService.invalidateWallet(userId);
|
await this.walletCacheService.invalidateWallet(userId);
|
||||||
|
|
||||||
this.logger.log(`Frozen ${command.amount} USDT for order ${command.orderId}`);
|
this.logger.log(`[freezeForPlanting] 成功冻结 ${command.amount} USDT for order ${command.orderId}`);
|
||||||
return { success: true, frozenAmount: command.amount };
|
return { success: true, frozenAmount: command.amount };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue