* 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
91 lines
2.8 KiB
TypeScript
91 lines
2.8 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { ensureInitialized } from '@/lib/init'
|
|
import { withRouteContext } from '@/lib/api/with-route-context'
|
|
import { deleteDocument } from '@/lib/core/documents/document-service'
|
|
import { eventBus } from '@/lib/events'
|
|
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
|
|
|
|
ensureInitialized()
|
|
|
|
/**
|
|
* GET /api/documents/:id
|
|
* Fetch document metadata + signed download URL (60 min expiry)
|
|
*/
|
|
export const GET = withRouteContext<{ params: Promise<{ id: string }> }>(
|
|
'document.get',
|
|
async (_request, { supabase, companyId, user }, { params }) => {
|
|
const { id } = await params
|
|
|
|
// Fetch document record
|
|
const { data: doc, error: docError } = await supabase
|
|
.from('document_attachments')
|
|
.select('*')
|
|
.eq('id', id)
|
|
.eq('company_id', companyId)
|
|
.single()
|
|
|
|
if (docError || !doc) {
|
|
return NextResponse.json({ error: 'Document not found' }, { status: 404 })
|
|
}
|
|
|
|
// Create signed download URL (60 minutes)
|
|
const { data: signedUrl, error: signError } = await supabase.storage
|
|
.from('documents')
|
|
.createSignedUrl(doc.storage_path, 3600)
|
|
|
|
if (signError) {
|
|
return NextResponse.json(
|
|
{ error: `Failed to create download URL: ${getUserErrorMessage(signError)}` },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
|
|
await eventBus.emit({
|
|
type: 'document.accessed',
|
|
payload: {
|
|
document: { id: doc.id, file_name: doc.file_name },
|
|
userId: user.id,
|
|
companyId,
|
|
},
|
|
})
|
|
|
|
return NextResponse.json({
|
|
data: {
|
|
...doc,
|
|
download_url: signedUrl.signedUrl,
|
|
},
|
|
})
|
|
}
|
|
)
|
|
|
|
/**
|
|
* DELETE /api/documents/:id
|
|
* Remove an uploaded document. Only permitted when the document is not yet
|
|
* linked to a journal entry: once linked, it is räkenskapsinformation under
|
|
* BFL 7 kap 2§ and must be retained for 7 years. For linked docs the caller
|
|
* should use POST /api/documents/:id/versions to supersede via a new version.
|
|
*/
|
|
export const DELETE = withRouteContext<{ params: Promise<{ id: string }> }>(
|
|
'document.delete',
|
|
async (_request, { supabase, companyId }, { params }) => {
|
|
const { id } = await params
|
|
|
|
try {
|
|
const result = await deleteDocument(supabase, companyId, id)
|
|
|
|
if (!result.ok) {
|
|
return NextResponse.json({ error: result.message }, { status: result.status })
|
|
}
|
|
|
|
return NextResponse.json({ data: { id: result.document.id, deleted: true } })
|
|
} catch (error) {
|
|
console.error('[documents/DELETE] Failed to delete document:', error)
|
|
return NextResponse.json(
|
|
{ error: error instanceof Error ? getUserErrorMessage(error) : 'Failed to delete document' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
},
|
|
{ requireWrite: true }
|
|
)
|