Files
accounted/app/api/onboarding/state/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

119 lines
3.7 KiB
TypeScript

import { NextResponse } from 'next/server'
import { withRouteContext } from '@/lib/api/with-route-context'
import { validateBody } from '@/lib/api/validate'
import { UpdateInitialSetupStateSchema } from '@/lib/api/schemas'
import { errorResponseFromCode } from '@/lib/errors/get-structured-error'
import { getErrorMessage } from '@/lib/errors/get-error-message'
const INITIAL_SETUP_SELECT =
'initial_setup_path, initial_setup_completed_at, initial_setup_dismissed_at' as const
function toResponse(data: {
initial_setup_path: string | null
initial_setup_completed_at: string | null
initial_setup_dismissed_at: string | null
}) {
return {
path: data.initial_setup_path,
completedAt: data.initial_setup_completed_at,
dismissedAt: data.initial_setup_dismissed_at,
}
}
export const GET = withRouteContext(
'onboarding-state.get',
async (_request, { supabase, companyId, log, requestId }) => {
const { data, error } = await supabase
.from('company_settings')
.select(INITIAL_SETUP_SELECT)
.eq('company_id', companyId)
.maybeSingle()
if (error) {
log.error('initial setup state lookup failed', error)
return errorResponseFromCode('INTERNAL_ERROR', log, {
requestId,
details: { reason: getErrorMessage(error) },
})
}
if (!data) return errorResponseFromCode('NOT_FOUND', log, { requestId })
return NextResponse.json({ data: toResponse(data) })
},
)
export const PATCH = withRouteContext(
'onboarding-state.update',
async (request, { supabase, companyId, log, requestId }) => {
const validation = await validateBody(request, UpdateInitialSetupStateSchema, {
log,
operation: 'onboarding-state.update',
})
if (!validation.success) return validation.response
const body = validation.data
const { data: existing, error: lookupError } = await supabase
.from('company_settings')
.select(INITIAL_SETUP_SELECT)
.eq('company_id', companyId)
.maybeSingle()
if (lookupError) {
log.error('initial setup state lookup failed', lookupError)
return errorResponseFromCode('INTERNAL_ERROR', log, {
requestId,
details: { reason: getErrorMessage(lookupError) },
})
}
if (!existing) return errorResponseFromCode('NOT_FOUND', log, { requestId })
const effectivePath = body.path !== undefined ? body.path : existing.initial_setup_path
if (body.completed === true && !effectivePath) {
return NextResponse.json(
{
error: 'Validation failed',
type: 'validation_error',
errors: [{
field: 'completed',
message: 'Välj först hur du vill komma igång',
code: 'custom',
}],
},
{ status: 400 },
)
}
const now = new Date().toISOString()
const update: Record<string, unknown> = {}
if (body.path !== undefined) {
update.initial_setup_path = body.path
update.initial_setup_completed_at = body.path === 'fresh' ? now : null
update.initial_setup_dismissed_at = null
}
if (body.completed !== undefined) {
update.initial_setup_completed_at = body.completed ? now : null
}
if (body.dismissed !== undefined) {
update.initial_setup_dismissed_at = body.dismissed ? now : null
}
const { data, error } = await supabase
.from('company_settings')
.update(update)
.eq('company_id', companyId)
.select(INITIAL_SETUP_SELECT)
.single()
if (error) {
log.error('initial setup state update failed', error)
return errorResponseFromCode('INTERNAL_ERROR', log, {
requestId,
details: { reason: getErrorMessage(error) },
})
}
return NextResponse.json({ data: toResponse(data) })
},
{ requireWrite: true },
)