* 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
51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { withRouteContext } from '@/lib/api/with-route-context'
|
|
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
|
|
|
|
/**
|
|
* GET /api/documents/counts?journal_entry_ids=id1,id2,...
|
|
* Returns attachment counts per journal entry ID.
|
|
* Max 50 IDs per request.
|
|
*/
|
|
export const GET = withRouteContext('document.counts', async (request, ctx) => {
|
|
const { supabase, companyId } = ctx
|
|
|
|
const { searchParams } = new URL(request.url)
|
|
const idsParam = searchParams.get('journal_entry_ids')
|
|
|
|
if (!idsParam) {
|
|
return NextResponse.json({ error: 'journal_entry_ids is required' }, { status: 400 })
|
|
}
|
|
|
|
const ids = idsParam.split(',').filter(Boolean)
|
|
|
|
if (ids.length === 0) {
|
|
return NextResponse.json({ data: {} })
|
|
}
|
|
|
|
if (ids.length > 50) {
|
|
return NextResponse.json({ error: 'Maximum 50 IDs per request' }, { status: 400 })
|
|
}
|
|
|
|
const { data, error } = await supabase
|
|
.from('document_attachments')
|
|
.select('journal_entry_id')
|
|
.eq('company_id', companyId)
|
|
.eq('is_current_version', true)
|
|
.in('journal_entry_id', ids)
|
|
|
|
if (error) {
|
|
return NextResponse.json({ error: getUserErrorMessage(error) }, { status: 500 })
|
|
}
|
|
|
|
// Group and count by journal_entry_id
|
|
const counts: Record<string, number> = {}
|
|
for (const row of data || []) {
|
|
if (row.journal_entry_id) {
|
|
counts[row.journal_entry_id] = (counts[row.journal_entry_id] || 0) + 1
|
|
}
|
|
}
|
|
|
|
return NextResponse.json({ data: counts })
|
|
})
|