fix: macro impacts API returns {exposure_profile, impacts} to match frontend type
API was returning a flat array but frontend expects CompanyMacroImpacts wrapper with exposure_profile and impacts fields. Also queries the exposure_profiles table for the company's active profile.
This commit is contained in:
+34
-5
@@ -2289,12 +2289,37 @@ async def get_macro_impacts_for_ticker(
|
|||||||
limit: int = Query(default=50, le=200),
|
limit: int = Query(default=50, le=200),
|
||||||
offset: int = 0,
|
offset: int = 0,
|
||||||
):
|
):
|
||||||
"""Macro impacts for a specific company.
|
"""Macro impacts and exposure profile for a specific company.
|
||||||
|
|
||||||
|
Returns { exposure_profile, impacts } matching the frontend
|
||||||
|
CompanyMacroImpacts type.
|
||||||
|
|
||||||
Requirements: 8.2
|
Requirements: 8.2
|
||||||
"""
|
"""
|
||||||
|
ticker_upper = ticker.upper()
|
||||||
|
|
||||||
|
# Fetch exposure profile for this company
|
||||||
|
profile_row = await pool.fetchrow(
|
||||||
|
"""SELECT ep.id, ep.company_id, ep.geographic_revenue_mix,
|
||||||
|
ep.supply_chain_regions, ep.key_input_commodities,
|
||||||
|
ep.regulatory_jurisdictions, ep.market_position_tier,
|
||||||
|
ep.export_dependency_pct, ep.source, ep.confidence,
|
||||||
|
ep.version, ep.active, ep.created_at, ep.updated_at
|
||||||
|
FROM exposure_profiles ep
|
||||||
|
JOIN companies c ON c.id = ep.company_id
|
||||||
|
WHERE c.ticker = $1 AND ep.active = TRUE
|
||||||
|
ORDER BY ep.version DESC LIMIT 1""",
|
||||||
|
ticker_upper,
|
||||||
|
)
|
||||||
|
exposure_profile = _row_to_dict(profile_row) if profile_row else None
|
||||||
|
if exposure_profile:
|
||||||
|
exposure_profile["geographic_revenue_mix"] = _parse_jsonb(
|
||||||
|
exposure_profile.get("geographic_revenue_mix")
|
||||||
|
)
|
||||||
|
|
||||||
|
# Fetch macro impact records
|
||||||
conditions = ["mir.ticker = $1"]
|
conditions = ["mir.ticker = $1"]
|
||||||
params: list[Any] = [ticker.upper()]
|
params: list[Any] = [ticker_upper]
|
||||||
idx = 2
|
idx = 2
|
||||||
|
|
||||||
if since:
|
if since:
|
||||||
@@ -2317,12 +2342,16 @@ async def get_macro_impacts_for_ticker(
|
|||||||
LIMIT ${idx} OFFSET ${idx + 1}""",
|
LIMIT ${idx} OFFSET ${idx + 1}""",
|
||||||
*params, limit, offset,
|
*params, limit, offset,
|
||||||
)
|
)
|
||||||
results = []
|
impacts = []
|
||||||
for r in rows:
|
for r in rows:
|
||||||
d = _row_to_dict(r)
|
d = _row_to_dict(r)
|
||||||
d["contributing_factors"] = _parse_jsonb(d.get("contributing_factors"))
|
d["contributing_factors"] = _parse_jsonb(d.get("contributing_factors"))
|
||||||
results.append(d)
|
impacts.append(d)
|
||||||
return results
|
|
||||||
|
return {
|
||||||
|
"exposure_profile": exposure_profile,
|
||||||
|
"impacts": impacts,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user