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);