feat(portfolio): expose corporate actions in unified ledger

This commit is contained in:
2026-09-13 13:52:50 +09:00
parent 77468631b1
commit a4664f5991
7 changed files with 96 additions and 8 deletions
+14 -1
View File
@@ -31,6 +31,7 @@ from app.schemas.v1 import (
DividendManualLedgerResponse,
HoldingsCostBasisResponse,
LedgerHistoryItem,
CorporateActionLedgerHistoryItem,
LedgerHistoryResponse,
LedgerTradeRequest,
LedgerTradeResponse,
@@ -810,7 +811,19 @@ def _decode_ledger_cursor(cursor: str, *, portfolio_id: str, source: str | None)
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="invalid ledger cursor") from None
def _ledger_entry_to_history_item(entry) -> LedgerHistoryItem | DividendLedgerHistoryItem:
def _ledger_entry_to_history_item(entry) -> LedgerHistoryItem | DividendLedgerHistoryItem | CorporateActionLedgerHistoryItem:
if getattr(entry, "corporate_action", None):
event = entry.corporate_action
return CorporateActionLedgerHistoryItem(
ledger_entry_id=entry.ledger_entry_id, portfolio_id=entry.portfolio_id,
source="corporate_action", event_id=str(event["corporate_action_id"]),
event_type=event["action_type"], effective_date=event["effective_date"],
quantity_before=event["quantity_before"], quantity_after=event["quantity_after"],
average_price_before=event["average_price_before"], average_price_after=event["average_price_after"],
total_cost_basis=event["total_cost_basis"], cash_balance=event["cash_balance"],
profit_buffer=event["profit_buffer"], evidence_ref=event["evidence_ref"],
actor_user_id=event.get("actor_user_id"), recorded_at=entry.recorded_at,
)
action = (entry.action or entry.side or "").upper()
if action == "DIVIDEND":
amount_decimal = entry.price_decimal if entry.price_decimal is not None else Decimal(str(entry.price))