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:
parent
ead1aac60c
commit
d400290652
|
|
@ -34,22 +34,26 @@ export class GlobalStatsRepository implements IGlobalStatsRepository {
|
||||||
treeCount: number,
|
treeCount: number,
|
||||||
amount: Decimal,
|
amount: Decimal,
|
||||||
): Promise<GlobalStatsData> {
|
): Promise<GlobalStatsData> {
|
||||||
|
// [2026-01-07] 修复:防御性处理,确保 treeCount 不为 undefined
|
||||||
|
const safeTreeCount = treeCount ?? 0;
|
||||||
|
const safeAmount = amount ?? new Decimal(0);
|
||||||
|
|
||||||
this.logger.debug(
|
this.logger.debug(
|
||||||
`Incrementing global planting: trees=${treeCount}, amount=${amount}`,
|
`Incrementing global planting: trees=${safeTreeCount}, amount=${safeAmount}`,
|
||||||
);
|
);
|
||||||
|
|
||||||
const result = await this.prisma.globalStats.upsert({
|
const result = await this.prisma.globalStats.upsert({
|
||||||
where: { statsKey: this.GLOBAL_KEY },
|
where: { statsKey: this.GLOBAL_KEY },
|
||||||
create: {
|
create: {
|
||||||
statsKey: this.GLOBAL_KEY,
|
statsKey: this.GLOBAL_KEY,
|
||||||
totalPlantingCount: treeCount,
|
totalPlantingCount: safeTreeCount,
|
||||||
totalOrderCount: 1,
|
totalOrderCount: 1,
|
||||||
totalPlantingAmount: amount,
|
totalPlantingAmount: safeAmount,
|
||||||
},
|
},
|
||||||
update: {
|
update: {
|
||||||
totalPlantingCount: { increment: treeCount },
|
totalPlantingCount: { increment: safeTreeCount },
|
||||||
totalOrderCount: { increment: 1 },
|
totalOrderCount: { increment: 1 },
|
||||||
totalPlantingAmount: { increment: amount },
|
totalPlantingAmount: { increment: safeAmount },
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -46,22 +46,26 @@ export class RealtimeStatsRepository implements IRealtimeStatsRepository {
|
||||||
amount: Decimal,
|
amount: Decimal,
|
||||||
): Promise<RealtimeStatsData> {
|
): Promise<RealtimeStatsData> {
|
||||||
const statsDate = this.normalizeDate(date);
|
const statsDate = this.normalizeDate(date);
|
||||||
|
// [2026-01-07] 修复:防御性处理,确保 treeCount 不为 undefined
|
||||||
|
const safeTreeCount = treeCount ?? 0;
|
||||||
|
const safeAmount = amount ?? new Decimal(0);
|
||||||
|
|
||||||
this.logger.debug(
|
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({
|
const result = await this.prisma.realtimeStats.upsert({
|
||||||
where: { statsDate },
|
where: { statsDate },
|
||||||
create: {
|
create: {
|
||||||
statsDate,
|
statsDate,
|
||||||
dailyPlantingCount: treeCount,
|
dailyPlantingCount: safeTreeCount,
|
||||||
dailyOrderCount: 1,
|
dailyOrderCount: 1,
|
||||||
dailyPlantingAmount: amount,
|
dailyPlantingAmount: safeAmount,
|
||||||
},
|
},
|
||||||
update: {
|
update: {
|
||||||
dailyPlantingCount: { increment: treeCount },
|
dailyPlantingCount: { increment: safeTreeCount },
|
||||||
dailyOrderCount: { increment: 1 },
|
dailyOrderCount: { increment: 1 },
|
||||||
dailyPlantingAmount: { increment: amount },
|
dailyPlantingAmount: { increment: safeAmount },
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue