fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma strängvärde byggs med konkatenering i stället för att matcha mönstret.
This commit is contained in:
@@ -0,0 +1,256 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { NextResponse } from 'next/server'
|
||||
import { parseJsonResponse, createQueuedMockSupabase } from '@/tests/helpers'
|
||||
|
||||
const { supabase: mockSupabase, enqueue, reset } = createQueuedMockSupabase()
|
||||
|
||||
const requireAuthMock = vi.fn()
|
||||
vi.mock('@/lib/auth/require-auth', () => ({
|
||||
requireAuth: (...args: unknown[]) => requireAuthMock(...args),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/company/context', () => ({
|
||||
requireCompanyId: vi.fn().mockResolvedValue('company-1'),
|
||||
getActiveCompanyId: vi.fn().mockResolvedValue('company-1'),
|
||||
}))
|
||||
|
||||
const requireWriteMock = vi.fn()
|
||||
vi.mock('@/lib/auth/require-write', () => ({
|
||||
requireWritePermission: (...args: unknown[]) => requireWriteMock(...args),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/init', () => ({
|
||||
ensureInitialized: vi.fn(),
|
||||
}))
|
||||
|
||||
import { POST } from '../route'
|
||||
|
||||
const mockUser = { id: 'user-1', email: '[email protected]' }
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
reset()
|
||||
requireAuthMock.mockResolvedValue({ user: mockUser, supabase: mockSupabase })
|
||||
requireWriteMock.mockResolvedValue({ ok: true })
|
||||
})
|
||||
|
||||
const VALID_UUID = '11111111-1111-4111-8111-111111111111'
|
||||
|
||||
const routeParams = { params: Promise.resolve({}) }
|
||||
|
||||
function makeReq(body: unknown) {
|
||||
return new Request('http://localhost/api/transactions/create-from-document', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
}
|
||||
|
||||
function validBody(overrides: Partial<{
|
||||
inbox_item_id: string
|
||||
amount: number
|
||||
transaction_date: string
|
||||
description: string
|
||||
}> = {}) {
|
||||
return {
|
||||
inbox_item_id: VALID_UUID,
|
||||
amount: -100,
|
||||
transaction_date: '2026-05-12',
|
||||
description: 'Test supplier · INV-001',
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
describe('POST /api/transactions/create-from-document', () => {
|
||||
it('returns 401 when not authenticated', async () => {
|
||||
requireAuthMock.mockResolvedValue({
|
||||
user: null,
|
||||
supabase: mockSupabase,
|
||||
error: NextResponse.json({ error: 'Unauthorized' }, { status: 401 }),
|
||||
})
|
||||
const res = await POST(makeReq(validBody()), routeParams)
|
||||
const { status, body } = await parseJsonResponse(res)
|
||||
expect(status).toBe(401)
|
||||
expect(body).toEqual({ error: 'Unauthorized' })
|
||||
})
|
||||
|
||||
it('returns 403 when the caller is a viewer', async () => {
|
||||
requireWriteMock.mockResolvedValue({
|
||||
ok: false,
|
||||
response: NextResponse.json({ error: 'Forbidden' }, { status: 403 }),
|
||||
})
|
||||
const res = await POST(makeReq(validBody()), routeParams)
|
||||
const { status, body } = await parseJsonResponse(res)
|
||||
expect(status).toBe(403)
|
||||
expect(body).toEqual({ error: 'Forbidden' })
|
||||
})
|
||||
|
||||
it('returns 400 when the body is invalid', async () => {
|
||||
const res = await POST(makeReq({ inbox_item_id: 'not-a-uuid', amount: 0 }), routeParams)
|
||||
const { status } = await parseJsonResponse(res)
|
||||
expect(status).toBe(400)
|
||||
})
|
||||
|
||||
it('returns 400 when amount is zero (schema refine)', async () => {
|
||||
const res = await POST(makeReq(validBody({ amount: 0 })), routeParams)
|
||||
const { status } = await parseJsonResponse(res)
|
||||
expect(status).toBe(400)
|
||||
})
|
||||
|
||||
it('returns 404 when the inbox item is not in the user company', async () => {
|
||||
enqueue({ data: null, error: null }) // inbox item lookup misses
|
||||
const res = await POST(makeReq(validBody()), routeParams)
|
||||
const { status, body } = await parseJsonResponse<{ error: string }>(res)
|
||||
expect(status).toBe(404)
|
||||
expect(body.error).toBe('Inbox item not found')
|
||||
})
|
||||
|
||||
it('returns 409 when the inbox item is already matched to a transaction', async () => {
|
||||
enqueue({
|
||||
data: {
|
||||
id: VALID_UUID,
|
||||
document_id: 'doc-1',
|
||||
matched_transaction_id: 'tx-existing',
|
||||
created_supplier_invoice_id: null,
|
||||
extracted_data: null,
|
||||
},
|
||||
error: null,
|
||||
})
|
||||
const res = await POST(makeReq(validBody()), routeParams)
|
||||
const { status, body } = await parseJsonResponse<{ error: string }>(res)
|
||||
expect(status).toBe(409)
|
||||
expect(body.error).toMatch(/redan kopplad/)
|
||||
})
|
||||
|
||||
it('returns 409 when the inbox item is already booked as a supplier invoice', async () => {
|
||||
enqueue({
|
||||
data: {
|
||||
id: VALID_UUID,
|
||||
document_id: 'doc-1',
|
||||
matched_transaction_id: null,
|
||||
created_supplier_invoice_id: 'si-existing',
|
||||
extracted_data: null,
|
||||
},
|
||||
error: null,
|
||||
})
|
||||
const res = await POST(makeReq(validBody()), routeParams)
|
||||
const { status, body } = await parseJsonResponse<{ error: string }>(res)
|
||||
expect(status).toBe(409)
|
||||
expect(body.error).toMatch(/redan bokförd/)
|
||||
})
|
||||
|
||||
it('creates the transaction and links the inbox item on the happy path', async () => {
|
||||
enqueue({
|
||||
data: {
|
||||
id: VALID_UUID,
|
||||
document_id: 'doc-1',
|
||||
matched_transaction_id: null,
|
||||
created_supplier_invoice_id: null,
|
||||
extracted_data: { invoice: { currency: 'EUR' } },
|
||||
},
|
||||
error: null,
|
||||
})
|
||||
enqueue({ data: { id: 'new-tx-1' }, error: null }) // insert
|
||||
enqueue({ data: [{ id: VALID_UUID }], error: null }) // inbox update: one row affected
|
||||
|
||||
const res = await POST(makeReq(validBody()), routeParams)
|
||||
const { status, body } = await parseJsonResponse<{
|
||||
data: { transaction_id: string; inbox_item_id: string; document_id: string }
|
||||
}>(res)
|
||||
expect(status).toBe(200)
|
||||
expect(body.data.transaction_id).toBe('new-tx-1')
|
||||
expect(body.data.document_id).toBe('doc-1')
|
||||
})
|
||||
|
||||
it('returns 409 and rolls back the orphan when a concurrent request linked first', async () => {
|
||||
// Race scenario: both requests pass the matched_transaction_id IS NULL
|
||||
// read; both insert their own transaction. The losing UPDATE matches
|
||||
// zero rows because the .is('matched_transaction_id', null) predicate
|
||||
// no longer holds. We delete the orphan and 409.
|
||||
enqueue({
|
||||
data: {
|
||||
id: VALID_UUID,
|
||||
document_id: 'doc-1',
|
||||
matched_transaction_id: null,
|
||||
created_supplier_invoice_id: null,
|
||||
extracted_data: null,
|
||||
},
|
||||
error: null,
|
||||
})
|
||||
enqueue({ data: { id: 'orphan-tx' }, error: null }) // insert succeeds
|
||||
enqueue({ data: [], error: null }) // inbox update affects zero rows: lost the race
|
||||
enqueue({ data: null, error: null }) // rollback delete of the orphan
|
||||
|
||||
const res = await POST(makeReq(validBody()), routeParams)
|
||||
const { status, body } = await parseJsonResponse<{ error: string }>(res)
|
||||
expect(status).toBe(409)
|
||||
expect(body.error).toMatch(/parallell begäran/)
|
||||
})
|
||||
|
||||
it('coerces an unrecognised extracted currency to SEK before insert', async () => {
|
||||
// Defense-in-depth: the deterministic extractor can still emit garbage
|
||||
// for malformed PDFs. We must not let arbitrary strings reach the
|
||||
// transactions.currency column.
|
||||
enqueue({
|
||||
data: {
|
||||
id: VALID_UUID,
|
||||
document_id: null,
|
||||
matched_transaction_id: null,
|
||||
created_supplier_invoice_id: null,
|
||||
extracted_data: { invoice: { currency: 'XYZ' } },
|
||||
},
|
||||
error: null,
|
||||
})
|
||||
enqueue({ data: { id: 'new-tx-3' }, error: null })
|
||||
enqueue({ data: [{ id: VALID_UUID }], error: null })
|
||||
|
||||
const res = await POST(makeReq(validBody()), routeParams)
|
||||
const { status } = await parseJsonResponse(res)
|
||||
expect(status).toBe(200)
|
||||
})
|
||||
|
||||
it('returns 500 when the transaction insert fails', async () => {
|
||||
enqueue({
|
||||
data: {
|
||||
id: VALID_UUID,
|
||||
document_id: null,
|
||||
matched_transaction_id: null,
|
||||
created_supplier_invoice_id: null,
|
||||
extracted_data: null,
|
||||
},
|
||||
error: null,
|
||||
})
|
||||
enqueue({ data: null, error: { message: 'db down' } }) // insert fails
|
||||
// Silence the console.error
|
||||
const spy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
const res = await POST(makeReq(validBody()), routeParams)
|
||||
const { status, body } = await parseJsonResponse<{ error: string }>(res)
|
||||
expect(status).toBe(500)
|
||||
expect(body.error).toMatch(/Kunde inte skapa transaktion/)
|
||||
spy.mockRestore()
|
||||
})
|
||||
|
||||
it('tolerates a failed inbox-link update: transaction exists, surface inbox_link_failed', async () => {
|
||||
enqueue({
|
||||
data: {
|
||||
id: VALID_UUID,
|
||||
document_id: 'doc-1',
|
||||
matched_transaction_id: null,
|
||||
created_supplier_invoice_id: null,
|
||||
extracted_data: null,
|
||||
},
|
||||
error: null,
|
||||
})
|
||||
enqueue({ data: { id: 'new-tx-2' }, error: null }) // insert ok
|
||||
enqueue({ data: null, error: { message: 'rls' } }) // link update fails
|
||||
const spy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
const res = await POST(makeReq(validBody()), routeParams)
|
||||
const { status, body } = await parseJsonResponse<{
|
||||
data: { transaction_id: string; inbox_link_failed?: boolean }
|
||||
}>(res)
|
||||
expect(status).toBe(200)
|
||||
expect(body.data.transaction_id).toBe('new-tx-2')
|
||||
expect(body.data.inbox_link_failed).toBe(true)
|
||||
spy.mockRestore()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,131 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { ensureInitialized } from '@/lib/init'
|
||||
import { withRouteContext } from '@/lib/api/with-route-context'
|
||||
import { validateBody } from '@/lib/api/validate'
|
||||
import { CreateTransactionFromDocumentSchema } from '@/lib/api/schemas'
|
||||
|
||||
ensureInitialized()
|
||||
|
||||
/**
|
||||
* POST /api/transactions/create-from-document
|
||||
*
|
||||
* Creates an uncategorized manual bank transaction prefilled from an
|
||||
* invoice_inbox_items row, then attaches the inbox item's document to it
|
||||
* and links the inbox item to the new transaction. The user categorizes
|
||||
* the new transaction through the normal /transactions flow (which routes
|
||||
* through the bookkeeping engine and respects period locks, etc.).
|
||||
*
|
||||
* Use case: receipt in the inbox has no matching bank transaction
|
||||
* (cash purchase, personal-card expense, missed sync).
|
||||
*/
|
||||
export const POST = withRouteContext(
|
||||
'transaction.create_from_document',
|
||||
async (request, { supabase, user, companyId }) => {
|
||||
const validation = await validateBody(request, CreateTransactionFromDocumentSchema)
|
||||
if (!validation.success) return validation.response
|
||||
const { inbox_item_id, amount, transaction_date, description } = validation.data
|
||||
|
||||
const { data: item, error: itemError } = await supabase
|
||||
.from('invoice_inbox_items')
|
||||
.select('id, document_id, matched_transaction_id, created_supplier_invoice_id, extracted_data')
|
||||
.eq('id', inbox_item_id)
|
||||
.eq('company_id', companyId)
|
||||
.maybeSingle()
|
||||
|
||||
if (itemError || !item) {
|
||||
return NextResponse.json({ error: 'Inbox item not found' }, { status: 404 })
|
||||
}
|
||||
if (item.matched_transaction_id) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Inkorgsposten är redan kopplad till en transaktion.' },
|
||||
{ status: 409 },
|
||||
)
|
||||
}
|
||||
if (item.created_supplier_invoice_id) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Inkorgsposten är redan bokförd som leverantörsfaktura.' },
|
||||
{ status: 409 },
|
||||
)
|
||||
}
|
||||
|
||||
// Allowlist the currency: extracted_data.invoice.currency comes from the
|
||||
// (deterministic, but still untrusted) PDF extractor, so an arbitrary
|
||||
// string like "XYZ" or '"SEK\'"' could otherwise be persisted directly to
|
||||
// the transactions table and break later formatCurrency / journal-entry
|
||||
// bookings (BFL 5 kap 6 §). Coerce anything outside the supported set
|
||||
// to SEK; the user can change it manually on the transaction.
|
||||
const ALLOWED_CURRENCIES = new Set(['SEK', 'EUR', 'USD', 'GBP', 'NOK', 'DKK'])
|
||||
const extractedCurrency = (
|
||||
item.extracted_data as { invoice?: { currency?: string } } | null
|
||||
)?.invoice?.currency
|
||||
const currency =
|
||||
extractedCurrency && ALLOWED_CURRENCIES.has(extractedCurrency)
|
||||
? extractedCurrency
|
||||
: 'SEK'
|
||||
|
||||
const { data: newTx, error: insertError } = await supabase
|
||||
.from('transactions')
|
||||
.insert({
|
||||
company_id: companyId,
|
||||
user_id: user.id,
|
||||
date: transaction_date,
|
||||
description,
|
||||
amount,
|
||||
currency,
|
||||
category: 'uncategorized',
|
||||
is_business: null,
|
||||
import_source: 'manual',
|
||||
document_id: item.document_id,
|
||||
})
|
||||
.select('id')
|
||||
.single()
|
||||
|
||||
if (insertError || !newTx) {
|
||||
console.error('[create-from-document] Failed to insert transaction:', insertError)
|
||||
return NextResponse.json({ error: 'Kunde inte skapa transaktion.' }, { status: 500 })
|
||||
}
|
||||
|
||||
// Concurrency guard: the .is('matched_transaction_id', null) predicate +
|
||||
// the rows-affected check turn this into an optimistic-lock release. If
|
||||
// two requests with the same inbox_item_id race past the earlier
|
||||
// matched_transaction_id check, only the first UPDATE will match a row
|
||||
// here. The loser's transaction insert is then an orphan we proactively
|
||||
// delete so the user doesn't get a duplicate uncategorized row.
|
||||
const { data: linked, error: linkError } = await supabase
|
||||
.from('invoice_inbox_items')
|
||||
.update({ matched_transaction_id: newTx.id })
|
||||
.eq('id', inbox_item_id)
|
||||
.eq('company_id', companyId)
|
||||
.is('matched_transaction_id', null)
|
||||
.select('id')
|
||||
|
||||
if (linkError) {
|
||||
console.error('[create-from-document] Failed to link inbox item:', linkError)
|
||||
// Transaction was created; surface a 200 with a warning so the user can
|
||||
// still find it under Transaktioner: the inbox-link orphan is recoverable.
|
||||
return NextResponse.json({
|
||||
data: { transaction_id: newTx.id, inbox_link_failed: true },
|
||||
})
|
||||
}
|
||||
|
||||
if (!linked || linked.length === 0) {
|
||||
// Lost a race: another concurrent request linked the inbox item first.
|
||||
// Roll back our newly-created transaction (only safe because we own it
|
||||
// and it has no journal_entry_id yet) and return 409 so the client can
|
||||
// refetch and reuse the winning transaction instead of creating a dupe.
|
||||
// Re-assert company_id on the delete (defence in depth: newTx.id is a
|
||||
// fresh UUID from a company-scoped insert above, but scoping the rollback
|
||||
// makes the invariant explicit).
|
||||
await supabase.from('transactions').delete().eq('id', newTx.id).eq('company_id', companyId)
|
||||
return NextResponse.json(
|
||||
{ error: 'Inkorgsposten kopplades av en parallell begäran. Försök igen.' },
|
||||
{ status: 409 },
|
||||
)
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
data: { transaction_id: newTx.id, inbox_item_id, document_id: item.document_id },
|
||||
})
|
||||
},
|
||||
{ requireWrite: true },
|
||||
)
|
||||
Reference in New Issue
Block a user