Files
accounted/app/api/vat/validate/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

35 lines
1.3 KiB
TypeScript

import { NextResponse } from 'next/server'
import { withRouteContext } from '@/lib/api/with-route-context'
import { validateBody } from '@/lib/api/validate'
import { ValidateVatNumberSchema } from '@/lib/api/schemas'
import { validateVatNumber } from '@/lib/vat/vies-client'
import { guardSandbox } from '@/lib/sandbox/guard'
export const POST = withRouteContext('vat.validate', async (request, { supabase, companyId }) => {
// VIES is a live external call to the EU Commission: block in the sandbox
// so the demo can't generate background traffic against it.
const blocked = await guardSandbox(supabase, companyId)
if (blocked) return blocked
const result = await validateBody(request, ValidateVatNumberSchema)
if (!result.success) return result.response
const { vat_number, customer_id } = result.data
const validation = await validateVatNumber(vat_number)
// Update customer record if customer_id provided and VAT is valid
if (customer_id && validation.valid) {
await supabase
.from('customers')
.update({
vat_number: validation.vat_number,
vat_number_validated: true,
vat_number_validated_at: new Date().toISOString(),
})
.eq('id', customer_id)
.eq('company_id', companyId)
}
return NextResponse.json(validation)
})