e11f70b347
* 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
128 lines
4.9 KiB
TypeScript
128 lines
4.9 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { z } from 'zod'
|
|
import type { SupabaseClient } from '@supabase/supabase-js'
|
|
import { withRouteContext } from '@/lib/api/with-route-context'
|
|
import { validateBody } from '@/lib/api/validate'
|
|
import { errorResponse, errorResponseFromCode } from '@/lib/errors/get-structured-error'
|
|
import { buildCanonicalAnnualReport } from '@/lib/bokslut/arsredovisning/model'
|
|
import { getAnnualReportCapabilities } from '@/lib/bokslut/arsredovisning/capabilities'
|
|
import { upsertAnnualReportProfile } from '@/lib/bokslut/arsredovisning/profile-service'
|
|
|
|
const NullableBoolean = z.boolean().nullable()
|
|
|
|
const PatchSchema = z
|
|
.object({
|
|
is_public_limited_company: NullableBoolean.optional(),
|
|
is_in_liquidation: NullableBoolean.optional(),
|
|
securities_traded_on_regulated_market: NullableBoolean.optional(),
|
|
is_parent_company: NullableBoolean.optional(),
|
|
parent_group_size: z.enum(['none', 'small', 'large']).nullable().optional(),
|
|
prepares_consolidated_accounts: NullableBoolean.optional(),
|
|
has_foreign_branch: NullableBoolean.optional(),
|
|
has_crypto_assets: NullableBoolean.optional(),
|
|
has_share_based_payments: NullableBoolean.optional(),
|
|
has_convertible_debt: NullableBoolean.optional(),
|
|
building_revenue_share_pct: z.number().min(0).max(100).nullable().optional(),
|
|
has_material_deferred_tax: NullableBoolean.optional(),
|
|
reporting_currency: z.enum(['SEK', 'EUR']).optional(),
|
|
auditor_report_required: NullableBoolean.optional(),
|
|
auditor_report_included: z.boolean().optional(),
|
|
dividend_prudence_confirmed: NullableBoolean.optional(),
|
|
narrative_confirmed: z.boolean().optional(),
|
|
k2_assessment_confirmed: z.boolean().optional(),
|
|
signer_roster_confirmed: z.boolean().optional(),
|
|
})
|
|
.strict()
|
|
.refine((value) => Object.keys(value).length > 0, { message: 'At least one field is required' })
|
|
|
|
async function periodExists(
|
|
supabase: SupabaseClient,
|
|
companyId: string,
|
|
fiscalPeriodId: string,
|
|
): Promise<boolean> {
|
|
const { data } = await supabase
|
|
.from('fiscal_periods')
|
|
.select('id')
|
|
.eq('id', fiscalPeriodId)
|
|
.eq('company_id', companyId)
|
|
.maybeSingle()
|
|
return Boolean(data)
|
|
}
|
|
|
|
function responseData(model: Awaited<ReturnType<typeof buildCanonicalAnnualReport>>) {
|
|
return {
|
|
profile: model.profile,
|
|
disclosures: model.disclosures,
|
|
eligibility: model.eligibility,
|
|
validation: model.validation,
|
|
capabilities: getAnnualReportCapabilities(model.report.accounting_framework, model.eligibility),
|
|
report_summary: {
|
|
proposed_dividend: model.report.forvaltningsberattelse.proposed_dividend,
|
|
distributable_equity:
|
|
model.report.forvaltningsberattelse.resultatdisposition_amounts.total,
|
|
},
|
|
}
|
|
}
|
|
|
|
export const GET = withRouteContext(
|
|
'period.arsredovisning_compliance_get',
|
|
async (_request, ctx, { params }: { params: Promise<{ id: string }> }) => {
|
|
const { id } = await params
|
|
const { supabase, companyId, log, requestId } = ctx
|
|
try {
|
|
if (!(await periodExists(supabase, companyId, id))) {
|
|
return errorResponseFromCode('PERIOD_NOT_FOUND', log, { requestId })
|
|
}
|
|
const model = await buildCanonicalAnnualReport(supabase, companyId, id, {
|
|
stage: 'draft',
|
|
includeIxbrl: false,
|
|
})
|
|
return NextResponse.json({ data: responseData(model) })
|
|
} catch (err) {
|
|
return errorResponse(err, log, { requestId })
|
|
}
|
|
},
|
|
)
|
|
|
|
export const PATCH = withRouteContext(
|
|
'period.arsredovisning_compliance_patch',
|
|
async (request, ctx, { params }: { params: Promise<{ id: string }> }) => {
|
|
const { id } = await params
|
|
const { user, supabase, companyId, log, requestId } = ctx
|
|
const validation = await validateBody(request, PatchSchema)
|
|
if (!validation.success) return validation.response
|
|
try {
|
|
if (!(await periodExists(supabase, companyId, id))) {
|
|
return errorResponseFromCode('PERIOD_NOT_FOUND', log, { requestId })
|
|
}
|
|
const {
|
|
narrative_confirmed,
|
|
k2_assessment_confirmed,
|
|
signer_roster_confirmed,
|
|
...profileFields
|
|
} = validation.data
|
|
const now = new Date().toISOString()
|
|
await upsertAnnualReportProfile(supabase, companyId, user.id, id, {
|
|
...profileFields,
|
|
...(narrative_confirmed !== undefined
|
|
? { narrative_confirmed_at: narrative_confirmed ? now : null }
|
|
: {}),
|
|
...(k2_assessment_confirmed !== undefined
|
|
? { k2_assessment_confirmed_at: k2_assessment_confirmed ? now : null }
|
|
: {}),
|
|
...(signer_roster_confirmed !== undefined
|
|
? { signer_roster_confirmed_at: signer_roster_confirmed ? now : null }
|
|
: {}),
|
|
})
|
|
const model = await buildCanonicalAnnualReport(supabase, companyId, id, {
|
|
stage: 'draft',
|
|
includeIxbrl: false,
|
|
})
|
|
return NextResponse.json({ data: responseData(model) })
|
|
} catch (err) {
|
|
return errorResponse(err, log, { requestId })
|
|
}
|
|
},
|
|
{ requireWrite: true },
|
|
)
|