fix(ledger): 修复快照引擎 reduce 初始值类型错误,保障每日快照正常写入

This commit is contained in:
kennethcheng 2026-04-29 12:17:34 +08:00
parent e70c0602c8
commit 8f5ce4bc74

View File

@ -4,6 +4,7 @@ import { db } from '@/db';
import { portfolioSnapshots } from '@/db/schema';
import { getPortfolioPositions } from './portfolio';
import { eq, sql } from 'drizzle-orm';
import Big from 'big.js';
function getTodayInShanghai(): string {
const now = new Date();
@ -22,13 +23,13 @@ export async function recordDailySnapshot() {
const totalValueCny = positions.reduce(
(sum, pos) => sum.plus(pos.cnyValue || '0'),
0 as number | string
);
new Big(0)
).toString();
const totalCostCny = positions.reduce(
(sum, pos) => sum.plus(pos.totalCostCny || '0'),
0 as number | string
);
new Big(0)
).toString();
const dateStr = getTodayInShanghai();
@ -44,8 +45,8 @@ export async function recordDailySnapshot() {
await db
.update(portfolioSnapshots)
.set({
totalValueCny: String(totalValueCny),
totalCostCny: String(totalCostCny),
totalValueCny,
totalCostCny,
updatedAt: now,
})
.where(eq(portfolioSnapshots.date, dateStr));
@ -54,8 +55,8 @@ export async function recordDailySnapshot() {
success: true,
action: 'updated',
date: dateStr,
totalValueCny: String(totalValueCny),
totalCostCny: String(totalCostCny),
totalValueCny,
totalCostCny,
};
}
@ -63,8 +64,8 @@ export async function recordDailySnapshot() {
.insert(portfolioSnapshots)
.values({
date: dateStr,
totalValueCny: String(totalValueCny),
totalCostCny: String(totalCostCny),
totalValueCny,
totalCostCny,
createdAt: now,
updatedAt: now,
});
@ -73,8 +74,8 @@ export async function recordDailySnapshot() {
success: true,
action: 'inserted',
date: dateStr,
totalValueCny: String(totalValueCny),
totalCostCny: String(totalCostCny),
totalValueCny,
totalCostCny,
};
}