Files
accounted/extensions/general/stripe/__tests__/payouts.test.ts
T
MattssonandClaude Fable 5 d840257c0c Add/stripe connect transactions (#1139)
* fix(mcp-oauth): allow ChatGPT connector callbacks and resume OAuth after login

Add chatgpt.com/connector/oauth/* (per-instance) and the legacy
chatgpt.com/connector_platform_oauth_redirect to the built-in OAuth
redirect allowlist so ChatGPT MCP connectors can register and authorize.

Fix the login page dropping the ?next= destination: an OAuth-initiated
visit that required login previously ended on the dashboard and the
connection flow silently died. Login now resumes to the sanitized next
path (hard navigation, since the consent page is route-handler HTML),
carries it through the MFA step-up as returnTo, and /mfa/verify
hard-navigates for /api/ destinations.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(transactions): dedup incoming feed rows against booked hand-entered twins

Users who bookkeep via MCP/chat first and connect their bank afterwards got
the same movement twice: the synced row's external_id lives in a different
namespace, the free-form manual title never text-bridges the bank's raw
string, and the cross-channel mirror deliberately excluded manual/mcp rows.

Extend the mirror with a booked-hand-entered track: an incoming feed row is
skipped when a BOOKED manual/mcp row shares its (date, ore) bucket count-
symmetrically. Gates beyond the feed-vs-feed mirror: stored row must be
booked (staged rows never consume an import), currencies must not contradict
(bucket key is date+ore only), the cash-account guard applies to the count
exactly as to consumption, and symmetry uses the Layer-1-unmatched incoming
count so an already-stored row cannot inflate it. Consumption stamps the
batch cash_account_id onto an account-unbound hand row, so one hand row can
never consume feed rows on other accounts in later syncs.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(bookkeeping): inline verifikat rattelse (strike lines + text/date edit)

Second sanctioned correction track under BFL 5 kap 5/9 pp, Fortnox-style:
strike lines inside a posted verifikat with replacements in the same
voucher, and correct description/entry_date without an andringsverifikat.
Envelope: posted entries, open unlocked periods, company lock date,
same-period date moves, structural/FX/doc-linked lines excluded, and a
reconciliation guard preserving per-account net on bank/reskontra sides of
externally linked entries. Every rattelse writes an immutable who/when row
(journal_entry_rattelse_log, WORM, archived as rakenskapsinformation) and
struck originals render struck-through in the verifikat; list rows and the
detail header carry a Rattad marker. CLAUDE.md hard rule 1 and the
swedish-accounting-compliance skill are amended to state the two-track
rule. Staging carries the DDL; prod gets it on merge.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat: live saldo in booking form, prior-year window comparison, hideable assistant FAB

- Manual journal entry: saldo column now shows before -> after computed
  from the typed debit/credit amounts (direction feedback while booking)
- Resultatrapport: a narrowed date range now compares against the same
  window shifted one year back (#862), merged across fiscal periods for
  brutet rakenskapsar; P&L rows report window activity instead of
  rolled-forward YTD closing
- Assistant FAB: per-user hide toggle (user_preferences.hide_assistant_fab,
  settings > assistant), sidebar entry unaffected; collapsed sessions keep
  their reopen handle

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(stripe): sync balance transactions as a bank feed on 1686

Import the connected Stripe balance into the transactions inbox, opt-in
per connection (transaction_sync_enabled on stripe_connections):

- Balance transactions map to feed rows with the two-row gross+fee split
  and frozen external_id formats (stripe_{acct}_{txn} / _fee), dated on
  created, bound to a provisioned "Stripe-saldo" cash account on 1686 so
  booking settles against the clearing account by construction.
- Double-booking protection: settled payment-link charges import
  pre-linked to their settlement entry; payout rows import pre-linked to
  the payout entry; processPayoutPaidEvent claims the payout's fee rows
  at booking time (linkPayoutFeedRows, idempotent from both directions).
- Cursor last_balance_txn_synced_at with 24h overlap; first run
  backfills 90 days floored at the day after the company lock date.
- Nightly cron /api/extensions/stripe/transactions/cron (03:30),
  transaction-sync toggle route, "Synka nu" covers both feeds, settings
  panel toggle with last-synced/backfill note, sv+en strings.
- Migration 20260723200000 (applied to staging).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(transactions): offer match-to-voucher on unbooked history rows

Unbooked transactions with is_business already set (e.g. left behind when
a voucher was removed without a full uncategorize) land in the history
list instead of the inbox, where the match-against-existing-voucher
action did not exist, leaving them with no path back to voucher
matching. Add the same menu item to the history list for unbooked rows.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(transactions): enhance ownership checks and error handling in journal entry routes

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-24 01:16:20 +02:00

299 lines
9.9 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { createQueuedMockSupabase } from '@/tests/helpers'
import type { SupabaseClient } from '@supabase/supabase-js'
import type Stripe from 'stripe'
import type { CreateJournalEntryInput } from '@/types'
const balanceTransactionsList = vi.fn()
vi.mock('@/lib/stripe/client', () => ({
getStripe: () => ({ balanceTransactions: { list: balanceTransactionsList } }),
}))
vi.mock('@/lib/bookkeeping/engine', () => ({
createJournalEntry: vi.fn(),
findFiscalPeriod: vi.fn(),
}))
// The feed-row claim is transaction-sync's concern; here we only assert the
// payout flow invokes it with the booked entry and the payout's balance txns.
vi.mock('../lib/transaction-sync', () => ({
linkPayoutFeedRows: vi.fn().mockResolvedValue(0),
}))
import { createJournalEntry, findFiscalPeriod } from '@/lib/bookkeeping/engine'
import { linkPayoutFeedRows } from '../lib/transaction-sync'
import { processPayoutPaidEvent } from '../lib/payouts'
import type { StripeConnection } from '../types'
const CONNECTION: StripeConnection = {
id: 'conn-1',
company_id: 'company-1',
user_id: 'user-1',
stripe_account_id: 'acct_1',
livemode: false,
status: 'active',
oauth_state: null,
display_name: null,
last_event_created_at: null,
last_event_id: null,
transaction_sync_enabled: false,
last_balance_txn_synced_at: null,
error_message: null,
connected_at: '2026-07-01T00:00:00.000Z',
disconnected_at: null,
created_at: '2026-07-01T00:00:00.000Z',
updated_at: '2026-07-01T00:00:00.000Z',
}
// 2026-07-10 arrival: 1 000,00 gross, 25,50 fees, 974,50 net.
function makePayoutEvent(overrides: Record<string, unknown> = {}) {
return {
id: 'evt_po_1',
type: 'payout.paid',
created: 1_767_000_000,
data: {
object: {
id: 'po_1',
amount: 97450,
currency: 'sek',
arrival_date: 1_767_000_000,
livemode: false,
...overrides,
},
},
} as unknown as Stripe.Event
}
function stubBalanceTxns(txns: Array<Record<string, unknown>>) {
balanceTransactionsList.mockReturnValue({
autoPagingToArray: vi.fn().mockResolvedValue(txns),
})
}
const CLEAN_TXNS = [
{ type: 'charge', amount: 60000, fee: 1530, currency: 'sek' },
{ type: 'charge', amount: 40000, fee: 1020, currency: 'sek' },
{ type: 'payout', amount: -97450, fee: 0, currency: 'sek' },
]
describe('processPayoutPaidEvent', () => {
beforeEach(() => {
vi.clearAllMocks()
vi.stubEnv('STRIPE_SECRET_KEY', 'sk_test_123')
vi.mocked(findFiscalPeriod).mockResolvedValue('period-1')
vi.mocked(createJournalEntry).mockResolvedValue({ id: 'je-po-1' } as never)
stubBalanceTxns(CLEAN_TXNS)
})
afterEach(() => {
vi.unstubAllEnvs()
})
it('books a clean SEK payout with reverse-charge fee lines', async () => {
const { supabase, enqueue } = createQueuedMockSupabase()
enqueue({ data: [{ id: 'po-row-1' }] }) // claim
enqueue({ data: { vat_registered: true } }) // company_settings
enqueue({ data: null }) // finalize row
const outcome = await processPayoutPaidEvent(
supabase as unknown as SupabaseClient,
CONNECTION,
makePayoutEvent(),
)
expect(outcome).toEqual({ status: 'booked', reason: null })
expect(vi.mocked(createJournalEntry)).toHaveBeenCalledTimes(1)
const input = vi.mocked(createJournalEntry).mock.calls[0][3] as CreateJournalEntryInput
expect(input.source_type).toBe('stripe_payout')
const byAccount = new Map(
input.lines.map((l) => [l.account_number, l]),
)
// Net to bank
expect(byAccount.get('1930')).toMatchObject({ debit_amount: 974.5 })
// Fees as cost
expect(byAccount.get('6570')).toMatchObject({ debit_amount: 25.5 })
// Ruta 21 basis pair
expect(byAccount.get('4535')).toMatchObject({ debit_amount: 25.5 })
expect(byAccount.get('4598')).toMatchObject({ credit_amount: 25.5 })
// Fiktiv moms 25% of fees
expect(byAccount.get('2645')).toMatchObject({ debit_amount: 6.38 })
expect(byAccount.get('2614')).toMatchObject({ credit_amount: 6.38 })
// Clearing of the gross
expect(byAccount.get('1686')).toMatchObject({ credit_amount: 1000 })
// The entry balances to the öre.
const debits = input.lines.reduce((s, l) => s + (l.debit_amount || 0), 0)
const credits = input.lines.reduce((s, l) => s + (l.credit_amount || 0), 0)
expect(Math.round(debits * 100)).toBe(Math.round(credits * 100))
})
it('claims the payout feed rows against the booked entry', async () => {
const { supabase, enqueue } = createQueuedMockSupabase()
enqueue({ data: [{ id: 'po-row-1' }] }) // claim
enqueue({ data: { vat_registered: true } }) // company_settings
enqueue({ data: null }) // finalize row
await processPayoutPaidEvent(
supabase as unknown as SupabaseClient,
CONNECTION,
makePayoutEvent(),
)
expect(vi.mocked(linkPayoutFeedRows)).toHaveBeenCalledTimes(1)
const [, companyId, accountId, journalEntryId, txns] =
vi.mocked(linkPayoutFeedRows).mock.calls[0]
expect(companyId).toBe('company-1')
expect(accountId).toBe('acct_1')
expect(journalEntryId).toBe('je-po-1')
expect(txns).toEqual(CLEAN_TXNS)
})
it('does not claim feed rows when the payout is not auto-booked', async () => {
stubBalanceTxns([
...CLEAN_TXNS,
{ type: 'refund', amount: -10000, fee: 0, currency: 'sek' },
])
const { supabase, enqueue } = createQueuedMockSupabase()
enqueue({ data: [{ id: 'po-row-1' }] })
enqueue({ data: { vat_registered: true } })
enqueue({ data: null })
await processPayoutPaidEvent(
supabase as unknown as SupabaseClient,
CONNECTION,
makePayoutEvent(),
)
expect(vi.mocked(linkPayoutFeedRows)).not.toHaveBeenCalled()
})
it('skips a payout already claimed by an earlier run', async () => {
const { supabase, enqueue } = createQueuedMockSupabase()
enqueue({ data: [] }) // claim conflict
const outcome = await processPayoutPaidEvent(
supabase as unknown as SupabaseClient,
CONNECTION,
makePayoutEvent(),
)
expect(outcome.status).toBe('already_processed')
expect(vi.mocked(createJournalEntry)).not.toHaveBeenCalled()
})
it('sends payouts containing refunds to needs_review', async () => {
stubBalanceTxns([
...CLEAN_TXNS,
{ type: 'refund', amount: -10000, fee: 0, currency: 'sek' },
])
const { supabase, enqueue } = createQueuedMockSupabase()
enqueue({ data: [{ id: 'po-row-1' }] })
enqueue({ data: { vat_registered: true } })
enqueue({ data: null })
const outcome = await processPayoutPaidEvent(
supabase as unknown as SupabaseClient,
CONNECTION,
makePayoutEvent(),
)
expect(outcome.status).toBe('needs_review')
expect(outcome.reason).toBe('non_deterministic_txn_refund')
expect(vi.mocked(createJournalEntry)).not.toHaveBeenCalled()
})
it('sends non-SEK payouts to needs_review', async () => {
const { supabase, enqueue } = createQueuedMockSupabase()
enqueue({ data: [{ id: 'po-row-1' }] })
enqueue({ data: null }) // finalize
const outcome = await processPayoutPaidEvent(
supabase as unknown as SupabaseClient,
CONNECTION,
makePayoutEvent({ currency: 'eur' }),
)
expect(outcome.status).toBe('needs_review')
expect(outcome.reason).toBe('non_sek_payout')
})
it('sends arithmetic drift to needs_review', async () => {
stubBalanceTxns([
{ type: 'charge', amount: 60000, fee: 1530, currency: 'sek' },
{ type: 'payout', amount: -97450, fee: 0, currency: 'sek' },
]) // gross 600 - fees 15.30 = 584.70 != net 974.50
const { supabase, enqueue } = createQueuedMockSupabase()
enqueue({ data: [{ id: 'po-row-1' }] })
enqueue({ data: { vat_registered: true } })
enqueue({ data: null })
const outcome = await processPayoutPaidEvent(
supabase as unknown as SupabaseClient,
CONNECTION,
makePayoutEvent(),
)
expect(outcome.status).toBe('needs_review')
expect(outcome.reason).toBe('arithmetic_mismatch')
})
it('sends non-VAT-registered companies to needs_review (RC on fees)', async () => {
const { supabase, enqueue } = createQueuedMockSupabase()
enqueue({ data: [{ id: 'po-row-1' }] })
enqueue({ data: { vat_registered: false } })
enqueue({ data: null })
const outcome = await processPayoutPaidEvent(
supabase as unknown as SupabaseClient,
CONNECTION,
makePayoutEvent(),
)
expect(outcome.status).toBe('needs_review')
expect(outcome.reason).toBe('not_vat_registered')
})
it('records a locked-period booking failure as needs_review', async () => {
vi.mocked(createJournalEntry).mockRejectedValue(
new Error('Cannot write to locked/closed fiscal period'),
)
const { supabase, enqueue } = createQueuedMockSupabase()
enqueue({ data: [{ id: 'po-row-1' }] })
enqueue({ data: { vat_registered: true } })
enqueue({ data: null })
const outcome = await processPayoutPaidEvent(
supabase as unknown as SupabaseClient,
CONNECTION,
makePayoutEvent(),
)
expect(outcome.status).toBe('needs_review')
expect(outcome.reason).toContain('booking_failed')
})
it('books a zero-fee payout without any reverse-charge lines', async () => {
stubBalanceTxns([
{ type: 'charge', amount: 97450, fee: 0, currency: 'sek' },
{ type: 'payout', amount: -97450, fee: 0, currency: 'sek' },
])
const { supabase, enqueue } = createQueuedMockSupabase()
enqueue({ data: [{ id: 'po-row-1' }] })
enqueue({ data: { vat_registered: true } })
enqueue({ data: null })
const outcome = await processPayoutPaidEvent(
supabase as unknown as SupabaseClient,
CONNECTION,
makePayoutEvent(),
)
expect(outcome.status).toBe('booked')
const input = vi.mocked(createJournalEntry).mock.calls[0][3] as CreateJournalEntryInput
const accounts = input.lines.map((l) => l.account_number)
expect(accounts).toEqual(['1930', '1686'])
})
})