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