fix(import): SIE bulk-delete on service client + provider/reporting/b… (#724)

* fix(import): SIE bulk-delete on service client + provider/reporting/banking fixes

Rebuilt branch onto main as a single commit.

- import: run SIE bulk-delete RPCs on the service client to escape the 8s
  statement_timeout; undo_sie_import now takes an explicit actor (p_user_id)
  so its owner/admin gate works when auth.uid() is NULL on the service
  client (migration 20260624120000) + pg-real regression test
- providers: distinguish missing Fortnox license from expired connection;
  provider_consent_tokens PK regression test
- reports: include unmapped BAS expense groups in the income statement
- enable-banking: reconnect closed/expired bank sessions in place
- bookkeeping: surface linked invoices as underlag on the verifikat view
- scripts: track BL cleanup/diagnostic tooling; data files (*.csv) are
  git-ignored and consentId is now a required arg with no silent default

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(import): add Cache-Control header to journal entry references response

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Mattsson
2026-06-14 23:40:26 +02:00
committed by GitHub
co-authored by Claude Opus 4.8
parent db8983ba9e
commit 43925bc2d3
37 changed files with 2456 additions and 1152 deletions
@@ -0,0 +1,63 @@
import { describe, it, expect } from 'vitest'
import { randomUUID } from 'node:crypto'
import { getPool } from '@/tests/pg/setup'
import { seedCompany } from '@/tests/pg/fixtures'
// Regression guard for a destructive bug: resolveConsent()'s optimistic
// concurrency guard ran `UPDATE provider_consent_tokens … .select('id')`, but
// this table's PRIMARY KEY is `consent_id` and it has NO `id` column. Postgres
// rejected the whole statement ("column provider_consent_tokens.id does not
// exist"), which surfaced as updateError AFTER the provider had already rotated
// the refresh token — permanently breaking the consent.
//
// Unit mocks can't catch this (they replay queued data regardless of the
// selected columns), so we assert the real query shapes against real Postgres.
async function seedConsentWithToken(): Promise<{ consentId: string; expiresAt: string }> {
const { companyId } = await seedCompany()
const consentId = randomUUID()
const expiresAt = '2020-01-01T00:00:00.000Z'
await getPool().query(
`INSERT INTO provider_consents (id, company_id, name, status, provider)
VALUES ($1, $2, $3, 1, 'fortnox')`,
[consentId, companyId, `pg-real-${consentId}`],
)
await getPool().query(
`INSERT INTO provider_consent_tokens
(consent_id, provider, access_token, refresh_token, token_expires_at)
VALUES ($1, 'fortnox', 'old-access', 'old-refresh', $2)`,
[consentId, expiresAt],
)
return { consentId, expiresAt }
}
describe('provider_consent_tokens guarded update (pg-real)', () => {
it('the rotation UPDATE … RETURNING consent_id is valid and matches the row', async () => {
const { consentId, expiresAt } = await seedConsentWithToken()
// This mirrors resolveConsent()'s guarded update exactly. `consent_id` is
// the PK; selecting it must succeed and return the matched row.
const { rows } = await getPool().query(
`UPDATE provider_consent_tokens
SET access_token = $1, refresh_token = $2, token_expires_at = $3
WHERE consent_id = $4 AND token_expires_at = $5
RETURNING consent_id`,
['new-access', 'new-refresh', '2030-01-01T00:00:00.000Z', consentId, expiresAt],
)
expect(rows).toHaveLength(1)
expect(rows[0].consent_id).toBe(consentId)
})
it('there is no `id` column to select (proves why the old query broke)', async () => {
const { consentId } = await seedConsentWithToken()
await expect(
getPool().query(
`SELECT id FROM provider_consent_tokens WHERE consent_id = $1`,
[consentId],
),
).rejects.toThrow(/column .*id.* does not exist/i)
})
})
@@ -9,9 +9,15 @@ vi.mock('@/lib/providers/briox/oauth', () => ({
refreshBrioxToken: vi.fn(),
}));
vi.mock('@/lib/providers/fortnox/oauth', () => ({
refreshFortnoxToken: vi.fn(),
}));
import { createServiceClient } from '@/lib/supabase/server';
import { refreshBrioxToken } from '@/lib/providers/briox/oauth';
import { refreshFortnoxToken } from '@/lib/providers/fortnox/oauth';
import { resolveConsent } from '../resolve-consent';
import { ProviderCallError } from '../with-provider-call';
const consentRow = { id: 'c1', company_id: 'co1', provider: 'briox', status: 1 };
@@ -52,7 +58,7 @@ describe('resolveConsent — Briox token refresh concurrency', () => {
it('persists the rotated pair when the guarded update wins the race', async () => {
mock.enqueue({ data: [consentRow] }); // consent lookup
mock.enqueue({ data: [expiredTokens] }); // expired token row
mock.enqueue({ data: [{ id: 't1' }] }); // guarded update matched 1 row
mock.enqueue({ data: [{ consent_id: 'c1' }] }); // guarded update matched 1 row (PK is consent_id, not id)
const result = await resolveConsent('co1', 'c1');
@@ -98,4 +104,63 @@ describe('resolveConsent — Briox token refresh concurrency', () => {
message: expect.stringContaining('re-enter the credentials'),
});
});
it('rethrows a dead refresh token as PROVIDER_AUTH_EXPIRED so callers prompt reconnect', async () => {
mock.enqueue({ data: [consentRow] }); // consent lookup
mock.enqueue({ data: [expiredTokens] }); // expired token row
// Mirrors Fortnox's `400 invalid_grant`: the raw helper throws a plain
// Error whose status lives only in the message string. resolveConsent must
// still classify it as an expired connection, not let it fall through to a
// generic 500 at the route.
vi.mocked(refreshBrioxToken).mockRejectedValueOnce(
new Error('Briox token refresh failed: 400 {"error":"invalid_grant"}'),
);
const err = await resolveConsent('co1', 'c1').catch((e) => e);
expect(err).toBeInstanceOf(ProviderCallError);
expect(err.code).toBe('PROVIDER_AUTH_EXPIRED');
expect(err.provider).toBe('briox');
});
it('maps Fortnox error_missing_license to PROVIDER_LICENSE_MISSING (not a revivable reconnect)', async () => {
const fortnoxConsent = { id: 'c2', company_id: 'co1', provider: 'fortnox', status: 1 };
mock.enqueue({ data: [fortnoxConsent] }); // consent lookup
mock.enqueue({ data: [expiredTokens] }); // expired token row
// Fortnox answers the token endpoint with error_missing_license when the
// customer's integration license has lapsed. Re-auth can't revive it — the
// license must be re-ordered first — so it gets its own code rather than the
// generic "reconnect" PROVIDER_AUTH_EXPIRED.
vi.mocked(refreshFortnoxToken).mockRejectedValueOnce(
new Error(
'Fortnox token refresh failed: 401 {"error":"error_missing_license","error_description":"The client credentials are invalid"}',
),
);
const err = await resolveConsent('co1', 'c2').catch((e) => e);
expect(err).toBeInstanceOf(ProviderCallError);
expect(err.code).toBe('PROVIDER_LICENSE_MISSING');
expect(err.provider).toBe('fortnox');
});
it('keeps a Fortnox invalid_grant as PROVIDER_AUTH_EXPIRED (revivable by reconnect)', async () => {
const fortnoxConsent = { id: 'c2', company_id: 'co1', provider: 'fortnox', status: 1 };
mock.enqueue({ data: [fortnoxConsent] }); // consent lookup
mock.enqueue({ data: [expiredTokens] }); // expired token row
// A plain expired/revoked grant IS revivable by reconnecting — it must not
// be mis-mapped to the license code.
vi.mocked(refreshFortnoxToken).mockRejectedValueOnce(
new Error('Fortnox token refresh failed: 400 {"error":"invalid_grant"}'),
);
const err = await resolveConsent('co1', 'c2').catch((e) => e);
expect(err).toBeInstanceOf(ProviderCallError);
expect(err.code).toBe('PROVIDER_AUTH_EXPIRED');
expect(err.provider).toBe('fortnox');
});
});