Files
accounted/app/api/bookkeeping/journal-entries/[id]/reverse/route.ts
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
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.
2026-11-10 09:37:22 +01:00

70 lines
2.2 KiB
TypeScript

import { NextResponse } from 'next/server'
import { z } from 'zod'
import { reverseEntry } from '@/lib/bookkeeping/engine'
import { ensureInitialized } from '@/lib/init'
import { withRouteContext } from '@/lib/api/with-route-context'
const ReverseJournalEntrySchema = z
.object({
allow_deep_chain: z.boolean().optional(),
})
.strict()
ensureInitialized()
export const POST = withRouteContext<{ params: Promise<{ id: string }> }>(
'bookkeeping.journal-entry.reverse',
async (request, { supabase, companyId, user }, { params }) => {
const { id } = await params
// Body is optional (existing callers POST with none): the only accepted
// field is the chain-depth guard override from the "Återför ändå" confirm.
// An empty body is the supported no-body case; malformed JSON or a
// non-boolean field is a caller bug and gets a 400 instead of silently
// reversing without the override the caller thought they sent.
let allowDeepChain = false
const rawText = await request.text()
if (rawText.trim()) {
let parsedJson: unknown
try {
parsedJson = JSON.parse(rawText)
} catch {
return NextResponse.json(
{
error: {
code: 'VALIDATION_ERROR',
message: 'Ogiltig JSON i förfrågan.',
message_en: 'Body is not valid JSON.',
},
},
{ status: 400 },
)
}
const parsed = ReverseJournalEntrySchema.safeParse(parsedJson)
if (!parsed.success) {
return NextResponse.json(
{
error: {
code: 'VALIDATION_ERROR',
message: 'Ogiltigt fält i förfrågan.',
message_en: 'Invalid request body.',
details: {
issues: parsed.error.issues.map((i) => ({
field: i.path.join('.'),
message: i.message,
})),
},
},
},
{ status: 400 },
)
}
allowDeepChain = parsed.data.allow_deep_chain === true
}
const reversalEntry = await reverseEntry(supabase, companyId, user.id, id, undefined, {
allowDeepChain,
})
return NextResponse.json({ data: reversalEntry })
},
{ requireWrite: true },
)