fix(reporting-service): 修复统计 incrementPlanting 对 undefined 参数的处理

当 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 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-01-06 21:18:26 -08:00
parent ead1aac60c
commit d400290652
2 changed files with 18 additions and 10 deletions

View File

@ -34,22 +34,26 @@ export class GlobalStatsRepository implements IGlobalStatsRepository {
treeCount: number,
amount: Decimal,
): Promise<GlobalStatsData> {
// [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 },
},
});

View File

@ -46,22 +46,26 @@ export class RealtimeStatsRepository implements IRealtimeStatsRepository {
amount: Decimal,
): Promise<RealtimeStatsData> {
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 },
},
});