fix(ledger): 修复 portfolio 接口组装时 avgCost 变量未定义的 ReferenceError

This commit is contained in:
kennethcheng 2026-05-01 04:25:06 +08:00
parent 9ce398efb1
commit f537dcf303
2 changed files with 14 additions and 3 deletions

View File

@ -192,4 +192,11 @@
- 在 `src/actions/portfolio.ts` 顶部引入 `calculateAssetMetrics` 工具函数,实现财务引擎接入。
- 重构 `getPortfolioPositions` 的第二个循环:对每个资产调用 `calculateAssetMetrics(transactions, latestPrice)`,将返回的 `holdings`、`averageCost`、`dilutedCost`、`floatingPnl`、`accumulatedPnl`、`marketValue` 映射到 Position 对象的 Native 币种字段。
- Dashboard 表格字段精确对齐:现價→`latestPrice`、市值→`metrics.marketValue`、攤薄/成本→`metrics.dilutedCost / metrics.averageCost`、浮動盈虧→`metrics.floatingPnl`、累計盈虧→`metrics.accumulatedPnl`。
- 累计盈亏验证公式:`accumulatedPnl = marketValue + 卖出/分红现金 - 总投入`确保有卖出或分红记录的资产如英特尔、分红ETF数据精确。
- 累计盈亏验证公式:`accumulatedPnl = marketValue + 卖出/分红现金 - 总投入`确保有卖出或分红记录的资产如英特尔、分红ETF数据精确。
## 修复 getPortfolioPositions 中接入财务引擎时的变量作用域丢失与解构映射错误 (Task 56c)
- 修复了 `src/actions/portfolio.ts``getPortfolioPositions` 函数的 ReferenceError`avgCost is not defined` 和 `dilutedCost is not defined`
- 根本原因:在将财务引擎 (`calculateAssetMetrics`) 接入 portfolio 引擎时,`avgCost` 和 `dilutedCost` 变量名在结果对象装配环节被直接引用,但它们从未在本作用域中声明——它们实际上是 `metrics` 对象的属性 (`metrics.averageCost`, `metrics.dilutedCost`)。
- 核心修复:将 `avgCost: avgCost.toString()` 替换为 `avgCost: metrics.averageCost`,将 `dilutedCost: dilutedCost.toString()` 替换为 `dilutedCost: metrics.dilutedCost`
- 同时新增 `floatingPnl``accumulatedPnl` 字段映射到 Position 接口,补齐了财务引擎产出的六大核心指标中缺失的两个字段。
- 遵循 `metrics` 返回值已是 string 类型的规范,不再调用 `.toString()` 导致冗余转换。

View File

@ -24,6 +24,8 @@ interface Position {
realizedPnlCny: string;
avgCost: string;
dilutedCost: string;
floatingPnl: string;
accumulatedPnl: string;
holdingDays: number;
exchange: string;
accumulatedDividendsCny: string;
@ -366,8 +368,10 @@ export async function getPortfolioPositions(): Promise<Position[]> {
totalBuyCost: holding.totalBuyCostCny.toString(),
totalBuyQuantity: holding.totalBuyQuantity.toString(),
realizedPnlCny: holding.realizedPnlCny.toString(),
avgCost: avgCost.toString(),
dilutedCost: dilutedCost.toString(),
avgCost: metrics.averageCost,
dilutedCost: metrics.dilutedCost,
floatingPnl: metrics.floatingPnl,
accumulatedPnl: metrics.accumulatedPnl,
holdingDays,
exchange: holding.exchange,
accumulatedDividendsCny: holding.accumulatedDividendsCny.toString(),