diff --git a/lib/import/__tests__/account-sync.test.ts b/lib/import/__tests__/account-sync.test.ts index 1b801f1b..bcc30b85 100644 --- a/lib/import/__tests__/account-sync.test.ts +++ b/lib/import/__tests__/account-sync.test.ts @@ -46,6 +46,9 @@ function buildCapturingSupabase(opts?: { return { select: () => ({ eq: () => ({ + // Mirrors the stable `.order('account_number')` the sync query now + // chains before `.range()` for paging stability. + order: () => ({ range: (from: number, to: number) => ({ then: ( resolve: (v: { @@ -60,6 +63,7 @@ function buildCapturingSupabase(opts?: { resolve({ data: existing.slice(from, to + 1), error: null }) }, }), + }), }), }), insert: (rows: Array>) => { diff --git a/lib/import/account-sync.ts b/lib/import/account-sync.ts index f259da1f..d94c4775 100644 --- a/lib/import/account-sync.ts +++ b/lib/import/account-sync.ts @@ -154,6 +154,11 @@ export async function syncMappedAccounts( .from('chart_of_accounts') .select('account_number, account_name') .eq('company_id', companyId) + // Stable total order on the unique account_number — paging is only + // correct with a deterministic order, else rows duplicate/skip across + // pages (see fetch-all.ts ordering invariant). The result is read into + // a Map below, so this order is invisible to callers. + .order('account_number', { ascending: true }) .range(from, to) ) existingByNumber = new Map(