From d400290652022cab5b0e3bef91cd278c822e4292 Mon Sep 17 00:00:00 2001 From: hailin Date: Tue, 6 Jan 2026 21:18:26 -0800 Subject: [PATCH] =?UTF-8?q?fix(reporting-service):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1=20incrementPlanting=20=E5=AF=B9=20undefined?= =?UTF-8?q?=20=E5=8F=82=E6=95=B0=E7=9A=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 当 planting.order.paid 事件中 treeCount 为 undefined 时: - GlobalStatsRepository: 使用 ?? 0 提供默认值 - RealtimeStatsRepository: 使用 ?? 0 提供默认值 - 避免 Prisma upsert 因 undefined increment 值而报错 问题原因:planting-service 发送的事件数据中 treeCount 可能为 undefined 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- .../repositories/global-stats.repository.impl.ts | 14 +++++++++----- .../repositories/realtime-stats.repository.impl.ts | 14 +++++++++----- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/backend/services/reporting-service/src/infrastructure/persistence/repositories/global-stats.repository.impl.ts b/backend/services/reporting-service/src/infrastructure/persistence/repositories/global-stats.repository.impl.ts index 790aa1dd..fefd6e5b 100644 --- a/backend/services/reporting-service/src/infrastructure/persistence/repositories/global-stats.repository.impl.ts +++ b/backend/services/reporting-service/src/infrastructure/persistence/repositories/global-stats.repository.impl.ts @@ -34,22 +34,26 @@ export class GlobalStatsRepository implements IGlobalStatsRepository { treeCount: number, amount: Decimal, ): Promise { + // [2026-01-07] 修复:防御性处理,确保 treeCount 不为 undefined + const safeTreeCount = treeCount ?? 0; + const safeAmount = amount ?? new Decimal(0); + this.logger.debug( - `Incrementing global planting: trees=${treeCount}, amount=${amount}`, + `Incrementing global planting: trees=${safeTreeCount}, amount=${safeAmount}`, ); const result = await this.prisma.globalStats.upsert({ where: { statsKey: this.GLOBAL_KEY }, create: { statsKey: this.GLOBAL_KEY, - totalPlantingCount: treeCount, + totalPlantingCount: safeTreeCount, totalOrderCount: 1, - totalPlantingAmount: amount, + totalPlantingAmount: safeAmount, }, update: { - totalPlantingCount: { increment: treeCount }, + totalPlantingCount: { increment: safeTreeCount }, totalOrderCount: { increment: 1 }, - totalPlantingAmount: { increment: amount }, + totalPlantingAmount: { increment: safeAmount }, }, }); diff --git a/backend/services/reporting-service/src/infrastructure/persistence/repositories/realtime-stats.repository.impl.ts b/backend/services/reporting-service/src/infrastructure/persistence/repositories/realtime-stats.repository.impl.ts index 4ea811f5..9f59120b 100644 --- a/backend/services/reporting-service/src/infrastructure/persistence/repositories/realtime-stats.repository.impl.ts +++ b/backend/services/reporting-service/src/infrastructure/persistence/repositories/realtime-stats.repository.impl.ts @@ -46,22 +46,26 @@ export class RealtimeStatsRepository implements IRealtimeStatsRepository { amount: Decimal, ): Promise { const statsDate = this.normalizeDate(date); + // [2026-01-07] 修复:防御性处理,确保 treeCount 不为 undefined + const safeTreeCount = treeCount ?? 0; + const safeAmount = amount ?? new Decimal(0); + this.logger.debug( - `Incrementing planting: date=${statsDate.toISOString()}, trees=${treeCount}, amount=${amount}`, + `Incrementing planting: date=${statsDate.toISOString()}, trees=${safeTreeCount}, amount=${safeAmount}`, ); const result = await this.prisma.realtimeStats.upsert({ where: { statsDate }, create: { statsDate, - dailyPlantingCount: treeCount, + dailyPlantingCount: safeTreeCount, dailyOrderCount: 1, - dailyPlantingAmount: amount, + dailyPlantingAmount: safeAmount, }, update: { - dailyPlantingCount: { increment: treeCount }, + dailyPlantingCount: { increment: safeTreeCount }, dailyOrderCount: { increment: 1 }, - dailyPlantingAmount: { increment: amount }, + dailyPlantingAmount: { increment: safeAmount }, }, });