* feat(dimensions): show the value's name after picking, and let an unused custom dimension be deleted (#2219) Two things from the same Discord report, both in bookkeeping from the transaction view: 1. After picking a kostnadsställe the field showed only the code ("1"). DimensionCombobox now writes the value's full name under the field once a code is committed, exactly as AccountCombobox does for the account name (looked up in the full registry so an archived code stays readable). The input text itself stays the code: the blur/revert logic keys on it. 2. A self-created dimension could not be removed at all: the DB already allowed it (enforce_dimension_registry_guards lets a non-system dimension go when no posted/reversed line carries its number, and the value retention trigger fires on the cascade), but no route or UI asked. New DELETE /api/dimensions/[id]: 400 DIMENSION_SYSTEM_DELETE for kostnadsställe/projekt, the guard's own Swedish P0001 verbatim as 409 DIMENSION_REFERENCED, 404, and a happy path; the register gets a quiet "Ta bort dimension" link for the active custom dimension behind a DestructiveConfirmDialog. Keys added to sv and en. Closes #2219 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VnConrmMCxJRQ5kfiPPWyy * test: satisfy the TypeScript ratchet for two test files main inherited from #2247 and #2242 accounts-route.test.ts built SyncResult literals without the requestedFromDate / historyNarrowed fields #2247 added (vitest does not typecheck, so it passed locally); fiscal-periods route.test.ts got two more one-argument POST(req) calls from #2242 in a file already at its ratchet baseline. Both files now typecheck; the ratchet runs clean. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VnConrmMCxJRQ5kfiPPWyy --------- Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com> Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
198 lines
7.0 KiB
TypeScript
198 lines
7.0 KiB
TypeScript
/**
|
|
* Tests for PATCH /api/dimensions/[id] (update dimension) and
|
|
* DELETE /api/dimensions/[id] (remove an unused custom dimension, #2219).
|
|
*
|
|
* Covers: 401, empty-body validation (400), 404, the is_system name-lock
|
|
* ("Systemdimensioner kan inte döpas om", 400 DIMENSION_SYSTEM_RENAME),
|
|
* archiving a system dimension (allowed), and the happy rename of a custom
|
|
* dimension.
|
|
*/
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import { NextResponse } from 'next/server'
|
|
import {
|
|
createQueuedMockSupabase,
|
|
createMockRequest,
|
|
createMockRouteParams,
|
|
parseJsonResponse,
|
|
} from '@/tests/helpers'
|
|
|
|
const { supabase, enqueue, reset } = createQueuedMockSupabase()
|
|
|
|
const requireAuthMock = vi.fn()
|
|
vi.mock('@/lib/auth/require-auth', () => ({
|
|
requireAuth: (...args: unknown[]) => requireAuthMock(...args),
|
|
}))
|
|
|
|
vi.mock('@/lib/company/context', () => ({
|
|
getActiveCompanyId: vi.fn().mockResolvedValue('company-1'),
|
|
requireCompanyId: vi.fn().mockResolvedValue('company-1'),
|
|
}))
|
|
|
|
const requireWriteMock = vi.fn()
|
|
vi.mock('@/lib/auth/require-write', () => ({
|
|
requireWritePermission: (...args: unknown[]) => requireWriteMock(...args),
|
|
}))
|
|
|
|
vi.mock('@/lib/init', () => ({ ensureInitialized: vi.fn() }))
|
|
|
|
import { PATCH, DELETE } from '../[id]/route'
|
|
|
|
const params = () => createMockRouteParams({ id: 'dim-1' })
|
|
|
|
describe('PATCH /api/dimensions/[id]', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
reset()
|
|
requireAuthMock.mockResolvedValue({ user: { id: 'user-1' }, supabase })
|
|
requireWriteMock.mockResolvedValue({ ok: true })
|
|
})
|
|
|
|
it('returns 401 when not authenticated', async () => {
|
|
requireAuthMock.mockResolvedValue({
|
|
error: NextResponse.json({ error: 'Unauthorized' }, { status: 401 }),
|
|
})
|
|
|
|
const request = createMockRequest('/api/dimensions/dim-1', {
|
|
method: 'PATCH',
|
|
body: { name: 'Avdelning' },
|
|
})
|
|
const response = await PATCH(request, params())
|
|
|
|
expect(response.status).toBe(401)
|
|
})
|
|
|
|
it('rejects an empty body with 400', async () => {
|
|
const request = createMockRequest('/api/dimensions/dim-1', { method: 'PATCH', body: {} })
|
|
const response = await PATCH(request, params())
|
|
|
|
expect(response.status).toBe(400)
|
|
})
|
|
|
|
it('returns 404 when the dimension does not belong to the company', async () => {
|
|
enqueue({ data: null }) // maybeSingle fetch
|
|
|
|
const request = createMockRequest('/api/dimensions/dim-1', {
|
|
method: 'PATCH',
|
|
body: { name: 'Avdelning' },
|
|
})
|
|
const response = await PATCH(request, params())
|
|
const { status, body } = await parseJsonResponse<{ error: { code: string } }>(response)
|
|
|
|
expect(status).toBe(404)
|
|
expect(body.error.code).toBe('DIMENSION_NOT_FOUND')
|
|
})
|
|
|
|
it('rejects renaming a system dimension with 400 DIMENSION_SYSTEM_RENAME', async () => {
|
|
enqueue({ data: { id: 'dim-1', name: 'Kostnadsställe', is_system: true } })
|
|
|
|
const request = createMockRequest('/api/dimensions/dim-1', {
|
|
method: 'PATCH',
|
|
body: { name: 'Avdelning' },
|
|
})
|
|
const response = await PATCH(request, params())
|
|
const { status, body } = await parseJsonResponse<{ error: { code: string; message: string } }>(response)
|
|
|
|
expect(status).toBe(400)
|
|
expect(body.error.code).toBe('DIMENSION_SYSTEM_RENAME')
|
|
expect(body.error.message).toBe('Systemdimensioner kan inte döpas om.')
|
|
})
|
|
|
|
it('allows archiving a system dimension (is_active only, no rename)', async () => {
|
|
enqueue({ data: { id: 'dim-1', name: 'Kostnadsställe', is_system: true } })
|
|
enqueue({
|
|
data: {
|
|
id: 'dim-1', sie_dim_no: 1, name: 'Kostnadsställe',
|
|
resets_annually: true, is_system: true, is_active: false, sort_order: 10,
|
|
},
|
|
})
|
|
|
|
const request = createMockRequest('/api/dimensions/dim-1', {
|
|
method: 'PATCH',
|
|
body: { is_active: false },
|
|
})
|
|
const response = await PATCH(request, params())
|
|
const { status, body } = await parseJsonResponse<{ data: { is_active: boolean } }>(response)
|
|
|
|
expect(status).toBe(200)
|
|
expect(body.data.is_active).toBe(false)
|
|
})
|
|
|
|
it('renames a non-system dimension (happy path)', async () => {
|
|
enqueue({ data: { id: 'dim-1', name: 'Dimension 7', is_system: false } })
|
|
enqueue({
|
|
data: {
|
|
id: 'dim-1', sie_dim_no: 7, name: 'Avdelning',
|
|
resets_annually: true, is_system: false, is_active: true, sort_order: 30,
|
|
},
|
|
})
|
|
|
|
const request = createMockRequest('/api/dimensions/dim-1', {
|
|
method: 'PATCH',
|
|
body: { name: 'Avdelning', sort_order: 30 },
|
|
})
|
|
const response = await PATCH(request, params())
|
|
const { status, body } = await parseJsonResponse<{ data: { name: string; sort_order: number } }>(response)
|
|
|
|
expect(status).toBe(200)
|
|
expect(body.data.name).toBe('Avdelning')
|
|
expect(body.data.sort_order).toBe(30)
|
|
})
|
|
})
|
|
|
|
describe('DELETE /api/dimensions/[id]', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
reset()
|
|
requireAuthMock.mockResolvedValue({ user: { id: 'user-1' }, supabase })
|
|
requireWriteMock.mockResolvedValue({ ok: true })
|
|
})
|
|
|
|
const del = () => DELETE(createMockRequest('/api/dimensions/dim-1', { method: 'DELETE' }), params())
|
|
|
|
it('returns 401 when not authenticated', async () => {
|
|
requireAuthMock.mockResolvedValue({
|
|
error: NextResponse.json({ error: 'Unauthorized' }, { status: 401 }),
|
|
})
|
|
const response = await del()
|
|
expect(response.status).toBe(401)
|
|
})
|
|
|
|
it('returns 404 when the dimension does not belong to the company', async () => {
|
|
enqueue({ data: null })
|
|
const { status, body } = await parseJsonResponse<{ error: { code: string } }>(await del())
|
|
expect(status).toBe(404)
|
|
expect(body.error.code).toBe('DIMENSION_NOT_FOUND')
|
|
})
|
|
|
|
it('refuses a system dimension with 400 DIMENSION_SYSTEM_DELETE before touching the DB', async () => {
|
|
enqueue({ data: { id: 'dim-1', name: 'Kostnadsställe', is_system: true } })
|
|
const { status, body } = await parseJsonResponse<{ error: { code: string; message: string } }>(await del())
|
|
expect(status).toBe(400)
|
|
expect(body.error.code).toBe('DIMENSION_SYSTEM_DELETE')
|
|
expect(body.error.message).toContain('avaktivera')
|
|
})
|
|
|
|
it('surfaces the DB guard verbatim as 409 DIMENSION_REFERENCED when posted lines carry the number', async () => {
|
|
enqueue({ data: { id: 'dim-1', name: 'Avdelning', is_system: false } })
|
|
enqueue({
|
|
data: null,
|
|
error: {
|
|
code: 'P0001',
|
|
message: 'Dimensionen 7 (Avdelning) används på bokförda verifikat och kan inte tas bort — avaktivera den istället.',
|
|
},
|
|
})
|
|
const { status, body } = await parseJsonResponse<{ error: { code: string; message: string } }>(await del())
|
|
expect(status).toBe(409)
|
|
expect(body.error.code).toBe('DIMENSION_REFERENCED')
|
|
expect(body.error.message).toContain('Avdelning')
|
|
})
|
|
|
|
it('deletes an unused custom dimension (happy path)', async () => {
|
|
enqueue({ data: { id: 'dim-1', name: 'Avdelning', is_system: false } })
|
|
enqueue({ data: [{ id: 'dim-1' }] })
|
|
const { status, body } = await parseJsonResponse<{ success: boolean }>(await del())
|
|
expect(status).toBe(200)
|
|
expect(body.success).toBe(true)
|
|
})
|
|
})
|