From 756ebce1211402b7a50afa0a4607983af8f6ce48 Mon Sep 17 00:00:00 2001 From: renee-png Date: Thu, 4 Jun 2026 20:17:07 -0400 Subject: [PATCH] buildium-sync: robustly resolve bill line GL account into expense_account_id Buildium bill imports left expense_account_id null on all bills because the importer only read firstLine.GLAccountId, but the bill-line payload exposes the GL id under GLAccount.Id (nested). chart_of_accounts.account_number already stores the Buildium GL Id, so resolve the line's GL id from either shape before mapping it. Re-syncing backfills existing bills via the update path. Co-Authored-By: Claude Opus 4.8 --- supabase/functions/buildium-sync/index.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/supabase/functions/buildium-sync/index.ts b/supabase/functions/buildium-sync/index.ts index 393fcd2..b7afce3 100644 --- a/supabase/functions/buildium-sync/index.ts +++ b/supabase/functions/buildium-sync/index.ts @@ -2123,12 +2123,21 @@ Deno.serve(async (req) => { const buildiumVendor = buildiumVendorById.get(String(bb.VendorId)) || null; const vendorId = await ensureVendor(buildiumVendor, assocLocalId); - // Pick first line's GL account as expense account if available + // Pick first line's GL account as expense account if available. + // chart_of_accounts.account_number stores the Buildium GL Id (see the + // glaccounts upsert: account_number = String(gl.Id ...)), so resolve the + // line's GL Id from whichever shape Buildium returns it in. const firstLine = Array.isArray(bb.Lines) && bb.Lines.length > 0 ? bb.Lines[0] : null; let expenseAccountId: string | null = null; - if (firstLine?.GLAccountId) { + const lineGlId = firstLine + ? (firstLine.GLAccountId + ?? firstLine.GLAccount?.Id + ?? firstLine.GLAccount?.GLAccountId + ?? null) + : null; + if (lineGlId !== null && lineGlId !== undefined && String(lineGlId) !== "") { const coa = await getCoa(assocLocalId); - expenseAccountId = coa.get(String(firstLine.GLAccountId)) || null; + expenseAccountId = coa.get(String(lineGlId)) || null; } let amount = Number(bb.TotalAmount ?? bb.Amount ?? 0);