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
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { NextResponse } from 'next/server'
|
||||
import {
|
||||
createMockRequest,
|
||||
createQueuedMockSupabase,
|
||||
parseJsonResponse,
|
||||
} from '@/tests/helpers'
|
||||
|
||||
const { supabase, enqueue, enqueueMany, reset } = createQueuedMockSupabase()
|
||||
const requireAuthMock = vi.fn()
|
||||
const requireWriteMock = vi.fn()
|
||||
|
||||
vi.mock('@/lib/auth/require-auth', () => ({
|
||||
requireAuth: (...args: unknown[]) => requireAuthMock(...args),
|
||||
}))
|
||||
vi.mock('@/lib/company/context', () => ({
|
||||
getActiveCompanyId: vi.fn().mockResolvedValue('company-1'),
|
||||
}))
|
||||
vi.mock('@/lib/auth/require-write', () => ({
|
||||
requireWritePermission: (...args: unknown[]) => requireWriteMock(...args),
|
||||
}))
|
||||
|
||||
import { GET, PATCH } from '../route'
|
||||
|
||||
describe('/api/onboarding/state', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
reset()
|
||||
requireAuthMock.mockResolvedValue({
|
||||
user: { id: 'user-1' },
|
||||
supabase,
|
||||
error: null,
|
||||
})
|
||||
requireWriteMock.mockResolvedValue({ ok: true })
|
||||
})
|
||||
|
||||
it('returns 401 when the user is not authenticated', async () => {
|
||||
requireAuthMock.mockResolvedValue({
|
||||
user: null,
|
||||
supabase,
|
||||
error: NextResponse.json({ error: 'Unauthorized' }, { status: 401 }),
|
||||
})
|
||||
|
||||
const response = await GET(createMockRequest('/api/onboarding/state'), {})
|
||||
expect(response.status).toBe(401)
|
||||
})
|
||||
|
||||
it('returns the persisted company setup state', async () => {
|
||||
enqueue({
|
||||
data: {
|
||||
initial_setup_path: 'migration',
|
||||
initial_setup_completed_at: null,
|
||||
initial_setup_dismissed_at: null,
|
||||
},
|
||||
})
|
||||
|
||||
const { status, body } = await parseJsonResponse<{
|
||||
data: { path: string; completedAt: string | null; dismissedAt: string | null }
|
||||
}>(await GET(createMockRequest('/api/onboarding/state'), {}))
|
||||
|
||||
expect(status).toBe(200)
|
||||
expect(body.data).toEqual({ path: 'migration', completedAt: null, dismissedAt: null })
|
||||
})
|
||||
|
||||
it('returns 400 for an empty update', async () => {
|
||||
const response = await PATCH(createMockRequest('/api/onboarding/state', {
|
||||
method: 'PATCH',
|
||||
body: {},
|
||||
}), {})
|
||||
|
||||
expect(response.status).toBe(400)
|
||||
})
|
||||
|
||||
it('returns 404 when company settings do not exist', async () => {
|
||||
enqueue({ data: null, error: null })
|
||||
const response = await PATCH(createMockRequest('/api/onboarding/state', {
|
||||
method: 'PATCH',
|
||||
body: { path: 'bank' },
|
||||
}), {})
|
||||
|
||||
expect(response.status).toBe(404)
|
||||
})
|
||||
|
||||
it('persists a selected path and clears dismissal', async () => {
|
||||
enqueueMany([
|
||||
{
|
||||
data: {
|
||||
initial_setup_path: null,
|
||||
initial_setup_completed_at: null,
|
||||
initial_setup_dismissed_at: null,
|
||||
},
|
||||
},
|
||||
{
|
||||
data: {
|
||||
initial_setup_path: 'bank',
|
||||
initial_setup_completed_at: null,
|
||||
initial_setup_dismissed_at: null,
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
const { status, body } = await parseJsonResponse<{
|
||||
data: { path: string; completedAt: string | null; dismissedAt: string | null }
|
||||
}>(await PATCH(createMockRequest('/api/onboarding/state', {
|
||||
method: 'PATCH',
|
||||
body: { path: 'bank' },
|
||||
}), {}))
|
||||
|
||||
expect(status).toBe(200)
|
||||
expect(body.data).toEqual({ path: 'bank', completedAt: null, dismissedAt: null })
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,118 @@
|
||||
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 },
|
||||
)
|
||||
Reference in New Issue
Block a user