Files
accounted/app/api/settings/booking-templates/import/route.ts
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

68 lines
2.1 KiB
TypeScript

import { BookingTemplateLineSchema, BookingTemplateCategorySchema, BookingTemplateEntityTypeSchema } from '@/lib/bookkeeping/booking-template-schemas'
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 ImportTemplateSchema = z.object({
name: z.string().min(1).max(200),
description: z.string().max(2000).default(''),
category: BookingTemplateCategorySchema.default('other'),
entity_type: BookingTemplateEntityTypeSchema.default('all'),
lines: z.array(BookingTemplateLineSchema).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 },
)