feat(import): detect and import the article register's Valuta column (#1183)

Fixes #1167. The register export gained a Valuta column in #1166 but
the importer ignored it, so re-imported non-SEK articles silently
became SEK, breaking the export -> edit -> re-import round-trip.

- Column detector recognizes valuta/valutakod/currency (claimed before
  generic columns; no keyword collision with Momskod).
- Parser normalizes to upper-case ISO shape, drops malformed codes
  with a file-level warning, and carries currency per row.
- Execute route validates codes lazily against the currencies table
  (FK stays the backstop when the reference read fails), imports valid
  codes, defaults absent to SEK, and in merge mode only overwrites
  when the file explicitly carries a valid currency.
- Edit step shows a muted currency marker next to non-SEK prices;
  manual column mapping offers Valuta.
- Export docblock caveat removed.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Jakob Wennberg
2026-07-25 12:59:23 +02:00
committed by GitHub
co-authored by Claude Fable 5
parent aead2bc1d1
commit 36a1df4f6b
10 changed files with 180 additions and 8 deletions
@@ -180,3 +180,60 @@ describe('POST /api/import/articles/execute', () => {
expect(body.data.warnings[0]).toContain('3999')
})
})
describe('POST /api/import/articles/execute currency handling', () => {
beforeEach(() => {
vi.clearAllMocks()
reset()
mockSupabase.auth.getUser.mockResolvedValue({ data: { user: mockUser } })
mockFetchAllRows.mockResolvedValue([])
mockCheckRevenueAccount.mockResolvedValue('ok')
mockEnsureArticleNumber.mockResolvedValue('AUTO-1')
})
it('imports a valid Valuta code and defaults missing to SEK', async () => {
// First queued result: lazy currencies reference read.
enqueue({ data: [{ code: 'SEK' }, { code: 'EUR' }, { code: 'USD' }] })
enqueue({ data: { id: 'a1', name: 'EU-tjanst', article_number: 'A-1', currency: 'EUR' } })
enqueue({ data: { id: 'a2', name: 'Svensk tjanst', article_number: 'A-2', currency: 'SEK' } })
const res = await POST(makeRequest({
rows: [
row({ name: 'EU-tjanst', article_number: 'A-1', currency: 'EUR' }),
row({ row_index: 3, name: 'Svensk tjanst', article_number: 'A-2', currency: null }),
],
update_duplicates: false,
}))
const { status, body } = await parseJsonResponse<{ data: { created: number; warnings: string[] } }>(res)
expect(status).toBe(200)
expect(body.data.created).toBe(2)
expect(body.data.warnings).toEqual([])
const inserts = mockSupabase.from.mock.calls.filter((c: unknown[]) => c[0] === 'articles')
expect(inserts.length).toBeGreaterThan(0)
})
it('drops a code missing from the currencies table with a warning', async () => {
enqueue({ data: [{ code: 'SEK' }, { code: 'EUR' }] })
enqueue({ data: { id: 'a1', name: 'X', article_number: 'A-1', currency: 'SEK' } })
const res = await POST(makeRequest({
rows: [row({ article_number: 'A-1', currency: 'XXX' })],
update_duplicates: false,
}))
const { status, body } = await parseJsonResponse<{ data: { warnings: string[] } }>(res)
expect(status).toBe(200)
expect(body.data.warnings.some((w) => w.includes('XXX'))).toBe(true)
})
it('rejects a malformed currency shape at validation', async () => {
const res = await POST(makeRequest({
rows: [row({ currency: 'EURO' })],
update_duplicates: false,
}))
const { status } = await parseJsonResponse(res)
expect(status).toBe(400)
})
})