Files
accounted/app/api/settings/booking-templates/import/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

80 lines
2.3 KiB
TypeScript

import { NextResponse } from 'next/server'
import { withRouteContext } from '@/lib/api/with-route-context'
import { z } from 'zod'
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
const ImportLineSchema = z.object({
account: z.string().regex(/^\d{4}$/),
label: z.string().min(1),
side: z.enum(['debit', 'credit']),
type: z.enum(['business', 'vat', 'settlement']),
ratio: z.number().min(0).max(10).optional(),
vat_rate: z.number().min(0).max(1).optional(),
})
const ImportTemplateSchema = z.object({
name: z.string().min(1).max(200),
description: z.string().max(2000).default(''),
category: z.enum([
'eu_trade', 'tax_account', 'private_transfer',
'salary', 'representation', 'year_end',
'vat', 'financial', 'other',
]).default('other'),
entity_type: z.enum(['all', 'enskild_firma', 'aktiebolag']).default('all'),
lines: z.array(ImportLineSchema).min(2),
})
const ImportPayloadSchema = z.object({
version: z.number(),
templates: z.array(ImportTemplateSchema).min(1).max(100),
})
/**
* POST /api/settings/booking-templates/import
* Import templates from JSON (exported from another company).
* Creates company-scoped templates for the active company.
*/
export const POST = withRouteContext(
'booking_template.import',
async (request, ctx) => {
const { supabase, user, companyId } = ctx
let body: unknown
try {
body = await request.json()
} catch {
return NextResponse.json({ error: 'Invalid JSON' }, { status: 400 })
}
const parsed = ImportPayloadSchema.safeParse(body)
if (!parsed.success) {
return NextResponse.json(
{ error: 'Invalid import format', details: parsed.error.issues },
{ status: 400 },
)
}
const rows = parsed.data.templates.map((t) => ({
company_id: companyId,
team_id: null,
created_by: user.id,
name: t.name,
description: t.description,
category: t.category,
entity_type: t.entity_type,
lines: t.lines,
is_system: false,
}))
const { data, error } = await supabase
.from('booking_template_library')
.insert(rows)
.select()
if (error) return NextResponse.json({ error: getUserErrorMessage(error) }, { status: 500 })
return NextResponse.json({ data, imported: data?.length ?? 0 }, { status: 201 })
},
{ requireWrite: true },
)