Files
accounted/app/api/bookkeeping/fiscal-periods/period-status/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

56 lines
1.8 KiB
TypeScript

import { NextResponse } from 'next/server'
import { withRouteContext } from '@/lib/api/with-route-context'
import { resolvePeriodStatusForDate } from '@/lib/core/bookkeeping/period-service'
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
/**
* GET /api/bookkeeping/fiscal-periods/period-status?date=YYYY-MM-DD
*
* Read-only preview of whether a verifikation with the given entry_date could
* be posted right now (company lock date + period is_closed/locked_at), plus
* the covering period's label so the UI can show "flyttas till <år>" before a
* write is attempted. Mirrors resolvePeriodStatusForDate / the DB triggers.
*/
export const GET = withRouteContext('period.status_for_date', async (request, ctx) => {
const { supabase, companyId } = ctx
const date = new URL(request.url).searchParams.get('date')
if (!date || !/^\d{4}-\d{2}-\d{2}$/.test(date)) {
return NextResponse.json({ error: 'Ogiltigt datum (förväntat ÅÅÅÅ-MM-DD)' }, { status: 400 })
}
try {
const status = await resolvePeriodStatusForDate(supabase, companyId, date)
let period_name: string | null = null
if (status.period_id) {
const { data: period } = await supabase
.from('fiscal_periods')
.select('name')
.eq('id', status.period_id)
.eq('company_id', companyId)
.maybeSingle()
period_name = period?.name ?? null
}
return NextResponse.json({
data: {
status: status.status,
period_id: status.period_id,
lock_date: status.lock_date,
period_name,
},
})
} catch (err) {
return NextResponse.json(
{
error: {
code: 'PERIOD_STATUS_ERROR',
message: err instanceof Error ? getUserErrorMessage(err) : 'Kunde inte hämta periodstatus',
},
},
{ status: 500 }
)
}
})