sinks: feed /import/check enrichment into /import body

/import/check hydrates each ActivityImport with resolved assetId,
exchangeMic, quoteCcy, instrumentType, quoteMode. The /import endpoint
on Wealthfolio 3.2 does NOT re-resolve — passing an un-enriched row
returns 200 OK but silently drops the activity (activities=[] in the
response).

The first live run returned `imported=63 failed=0` but nothing reached
the database. Fixed by posting the hydrated rows from the check response
to /import instead of the original.

Requires the test to also return list-shaped check responses (matches
the upstream Json<Vec<ActivityImport>> signature on the Rust side).

poetry run pytest -q     70 passed
poetry run mypy          clean
poetry run ruff check    clean
This commit is contained in:
Viktor Barzin 2026-04-17 20:54:17 +00:00
parent 80ca009373
commit b363032e42
3 changed files with 47 additions and 25 deletions

View file

@ -195,9 +195,14 @@ class WealthfolioSink:
rows = [self._activity_to_import_row(a) for a in activities]
if not rows:
return []
body = {"activities": rows}
check = await self._request("POST", _IMPORT_CHECK, json=body)
# Step 1 — /import/check hydrates each row with resolved asset_id,
# exchange_mic, quote_ccy, instrument_type, quote_mode (and flags
# errors). The /import endpoint on Wealthfolio 3.2+ DOES NOT
# re-resolve — if we send the un-enriched row the activity is
# silently dropped (import returns 200 OK with activities=[] in
# the payload). We must feed check's output into import.
check = await self._request("POST", _IMPORT_CHECK, json={"activities": rows})
if check.status_code >= 400:
try:
payload = check.json()
@ -205,22 +210,25 @@ class WealthfolioSink:
payload = {"raw": check.text}
raise ImportValidationError(f"Wealthfolio /import/check rejected: {payload}")
# Dry-run may flag per-row errors inside a 200 response.
checked = check.json()
if isinstance(checked, list):
bad = [r for r in checked if isinstance(r, dict) and r.get("errors")]
if bad:
# Show the first few to aid debugging without leaking everything.
raise ImportValidationError(
f"Wealthfolio /import/check flagged {len(bad)} rows; "
f"first: {bad[0]}"
)
if not isinstance(checked, list):
raise ImportValidationError(
f"Wealthfolio /import/check returned non-list: {type(checked).__name__}"
)
real = await self._request("POST", _IMPORT_REAL, json=body)
invalid = [r for r in checked if isinstance(r, dict) and r.get("errors")]
if invalid:
raise ImportValidationError(
f"Wealthfolio /import/check flagged {len(invalid)} row(s); "
f"first: {invalid[0]}"
)
# Drop any row the server marked is_valid=false (shouldn't happen
# without errors, but defensive).
valid_rows = [r for r in checked if isinstance(r, dict) and r.get("isValid")]
real = await self._request("POST", _IMPORT_REAL, json={"activities": valid_rows})
real.raise_for_status()
raw = real.json()
# import_activities returns ImportActivitiesResult which is an object,
# but we also accept a list (older versions). Normalise.
if isinstance(raw, dict) and "activities" in raw:
got = raw["activities"]
assert isinstance(got, list)