Files
accounted/app/api/bookkeeping/journal-entries/[id]/no-document-required/route.ts
T
Mattsson e11f70b347 Bug/gh issues fiz (#1103)
* refactor: optimize page loading and data fetching

* fix: resolve recurring production runtime errors

* feat: add MCP company and customer updates

* fix: handle year-end tax adjustments

* feat: harden annual report compliance

* fix: expand invoice logo and font support

* fix: sanitize API route error responses

* fix: sanitize user-facing error messages

* feat: persist onboarding and tax assessment notices

* fix: reduce cloud backup audit churn

* feat: refine invoice editor layout

* fix: show saved tax adjustments in INK2

* fix: complete annual report API mappings

* docs: record operational safeguards and decisions

* fix: harden annual report review findings

* fix: adjust column span for description based on VAT registration

* New css class name
2026-07-21 23:00:15 +02:00

75 lines
2.4 KiB
TypeScript

import { NextResponse } from 'next/server'
import { withRouteContext } from '@/lib/api/with-route-context'
import { z } from 'zod'
import { validateBody } from '@/lib/api/validate'
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
const SetNoDocSchema = z.object({
reason: z.string().trim().max(200).nullable().optional(),
})
export const POST = withRouteContext<{ params: Promise<{ id: string }> }>(
'bookkeeping.journal_entry.no_doc_required.set',
async (request, { supabase, companyId, user }, { params }) => {
const { id } = await params
const result = await validateBody(request, SetNoDocSchema)
if (!result.success) return result.response
const { data: entry, error: entryError } = await supabase
.from('journal_entries')
.select('id')
.eq('id', id)
.eq('company_id', companyId)
.maybeSingle()
if (entryError || !entry) {
return NextResponse.json({ error: 'Verifikationen hittades inte.' }, { status: 404 })
}
const { error } = await supabase
.from('journal_entry_no_doc_required')
.upsert(
{
journal_entry_id: id,
company_id: companyId,
user_id: user.id,
reason: result.data.reason ?? null,
},
{ onConflict: 'journal_entry_id' }
)
if (error) {
return NextResponse.json({ error: getUserErrorMessage(error) }, { status: 400 })
}
return NextResponse.json({ data: { exempted: true } })
},
{ requireWrite: true },
)
export const DELETE = withRouteContext<{ params: Promise<{ id: string }> }>(
'bookkeeping.journal_entry.no_doc_required.unset',
async (_request, { supabase, companyId }, { params }) => {
const { id } = await params
// Authorization is company-scoped, not user-scoped: any non-viewer member
// of the active company may revoke any exemption in that company. The flag
// is a shared bookkeeping artefact (same model as booking_template_library,
// mapping_rules, etc.): exemptions are reviewed as a team. The audit_log
// trigger captures the DELETE with actor_id so accountability is preserved.
const { error } = await supabase
.from('journal_entry_no_doc_required')
.delete()
.eq('journal_entry_id', id)
.eq('company_id', companyId)
if (error) {
return NextResponse.json({ error: getUserErrorMessage(error) }, { status: 400 })
}
return NextResponse.json({ data: { exempted: false } })
},
{ requireWrite: true },
)