Files
accounted/app/api/extensions/[sector]/[slug]/settings/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

66 lines
1.8 KiB
TypeScript

import { NextResponse } from 'next/server'
import { withRouteContext } from '@/lib/api/with-route-context'
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
export const GET = withRouteContext<{ params: Promise<{ sector: string; slug: string }> }>(
'extension.settings.get',
async (_request, { supabase, companyId }, { params }) => {
const { sector, slug } = await params
const extensionId = `${sector}/${slug}`
const { data } = await supabase
.from('extension_data')
.select('value')
.eq('company_id', companyId)
.eq('extension_id', extensionId)
.eq('key', 'settings')
.single()
return NextResponse.json({ data: data?.value ?? {} })
},
)
export const PATCH = withRouteContext<{ params: Promise<{ sector: string; slug: string }> }>(
'extension.settings.update',
async (request, { supabase, user, companyId }, { params }) => {
const { sector, slug } = await params
const body = await request.json()
const extensionId = `${sector}/${slug}`
// Get existing settings and merge
const { data: existing } = await supabase
.from('extension_data')
.select('value')
.eq('company_id', companyId)
.eq('extension_id', extensionId)
.eq('key', 'settings')
.single()
const mergedSettings = { ...(existing?.value ?? {}), ...body }
const { data, error } = await supabase
.from('extension_data')
.upsert(
{
user_id: user.id,
company_id: companyId,
extension_id: extensionId,
key: 'settings',
value: mergedSettings,
},
{ onConflict: 'user_id,extension_id,key' }
)
.select()
.single()
if (error) {
return NextResponse.json({ error: getUserErrorMessage(error) }, { status: 500 })
}
return NextResponse.json({ data: data.value })
},
{ requireWrite: true },
)