Add/csv import options (#420)
* feat(import): add customer and supplier parsing functionality - Implemented customer file parsing in `lib/import/customers/parser.ts` with support for Excel and CSV formats. - Created types for detected customer columns and parsed customer rows in `lib/import/customers/types.ts`. - Added tests for customer classification logic in `lib/import/shared/__tests__/classify.test.ts`. - Developed classification functions for customers and suppliers in `lib/import/shared/classify.ts`. - Introduced shared column utility functions in `lib/import/shared/column-utils.ts`. - Implemented supplier file parsing in `lib/import/suppliers/parser.ts` with validation for various fields. - Created types for detected supplier columns and parsed supplier rows in `lib/import/suppliers/types.ts`. - Added tests for supplier column detection and parsing in `lib/import/suppliers/__tests__/column-detector.test.ts` and `lib/import/suppliers/__tests__/parser.test.ts`. * fix(labels): update 'Svenskt företag' to 'Svenskt företag eller organisation' for clarity * feat(import): refactor encoding handling for Swedish files and add tests for character preservation * feat(recapt): implement clearRecaptIdentity function and integrate into logout flow * feat(bookkeeping): implement copy functionality and next voucher sequence retrieval * feat(import): enhance customer and supplier import functionality with normalization and event handling
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
|
||||
const mockFrom = vi.fn()
|
||||
const mockAuth = vi.fn()
|
||||
|
||||
vi.mock('@/lib/supabase/server', () => ({
|
||||
createClient: vi.fn().mockResolvedValue({
|
||||
from: (...args: unknown[]) => mockFrom(...args),
|
||||
auth: { getUser: () => mockAuth() },
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/company/context', () => ({
|
||||
getActiveCompanyId: vi.fn().mockResolvedValue('company-1'),
|
||||
requireCompanyId: vi.fn().mockResolvedValue('company-1'),
|
||||
}))
|
||||
|
||||
import { GET } from '../route'
|
||||
|
||||
interface ChainResult {
|
||||
data?: unknown
|
||||
error?: unknown
|
||||
}
|
||||
|
||||
function mockChain(result: ChainResult) {
|
||||
const chain: Record<string, unknown> = {}
|
||||
for (const m of ['select', 'eq', 'lte', 'gte']) {
|
||||
chain[m] = vi.fn().mockReturnValue(chain)
|
||||
}
|
||||
chain.maybeSingle = vi.fn().mockResolvedValue(result)
|
||||
return chain
|
||||
}
|
||||
|
||||
function mkReq() {
|
||||
return new Request('http://localhost/api/bookkeeping/voucher-sequences/next')
|
||||
}
|
||||
|
||||
function mkParams() {
|
||||
return { params: Promise.resolve({}) }
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
describe('GET /api/bookkeeping/voucher-sequences/next', () => {
|
||||
it('returns 401 when not authenticated', async () => {
|
||||
mockAuth.mockResolvedValue({ data: { user: null } })
|
||||
|
||||
const response = await GET(mkReq(), mkParams())
|
||||
const body = await response.json()
|
||||
|
||||
expect(response.status).toBe(401)
|
||||
expect(body.error).toBe('Unauthorized')
|
||||
})
|
||||
|
||||
it('returns last_number + 1 when sequence exists', async () => {
|
||||
mockAuth.mockResolvedValue({ data: { user: { id: 'user-1' } } })
|
||||
|
||||
mockFrom.mockImplementation((table: string) => {
|
||||
if (table === 'fiscal_periods') {
|
||||
return mockChain({ data: { id: 'period-1' }, error: null })
|
||||
}
|
||||
if (table === 'company_settings') {
|
||||
return mockChain({ data: { default_voucher_series: 'A' }, error: null })
|
||||
}
|
||||
if (table === 'voucher_sequences') {
|
||||
return mockChain({ data: { last_number: 57 }, error: null })
|
||||
}
|
||||
throw new Error(`Unexpected table: ${table}`)
|
||||
})
|
||||
|
||||
const response = await GET(mkReq(), mkParams())
|
||||
const body = await response.json()
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(body.data).toEqual({ next: 58, series: 'A', fiscal_period_id: 'period-1' })
|
||||
})
|
||||
|
||||
it('returns 1 when no sequence row exists yet', async () => {
|
||||
mockAuth.mockResolvedValue({ data: { user: { id: 'user-1' } } })
|
||||
|
||||
mockFrom.mockImplementation((table: string) => {
|
||||
if (table === 'fiscal_periods') {
|
||||
return mockChain({ data: { id: 'period-1' }, error: null })
|
||||
}
|
||||
if (table === 'company_settings') {
|
||||
return mockChain({ data: null, error: null })
|
||||
}
|
||||
if (table === 'voucher_sequences') {
|
||||
return mockChain({ data: null, error: null })
|
||||
}
|
||||
throw new Error(`Unexpected table: ${table}`)
|
||||
})
|
||||
|
||||
const response = await GET(mkReq(), mkParams())
|
||||
const body = await response.json()
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(body.data).toEqual({ next: 1, series: 'A', fiscal_period_id: 'period-1' })
|
||||
})
|
||||
|
||||
it('returns next: null when no fiscal period covers today', async () => {
|
||||
mockAuth.mockResolvedValue({ data: { user: { id: 'user-1' } } })
|
||||
|
||||
mockFrom.mockImplementation((table: string) => {
|
||||
if (table === 'fiscal_periods') {
|
||||
return mockChain({ data: null, error: null })
|
||||
}
|
||||
if (table === 'company_settings') {
|
||||
return mockChain({ data: { default_voucher_series: 'V' }, error: null })
|
||||
}
|
||||
throw new Error(`Unexpected table: ${table}`)
|
||||
})
|
||||
|
||||
const response = await GET(mkReq(), mkParams())
|
||||
const body = await response.json()
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(body.data).toEqual({ next: null, series: 'V', fiscal_period_id: null })
|
||||
})
|
||||
|
||||
it('honors a non-default voucher series from company settings', async () => {
|
||||
mockAuth.mockResolvedValue({ data: { user: { id: 'user-1' } } })
|
||||
|
||||
mockFrom.mockImplementation((table: string) => {
|
||||
if (table === 'fiscal_periods') {
|
||||
return mockChain({ data: { id: 'period-2' }, error: null })
|
||||
}
|
||||
if (table === 'company_settings') {
|
||||
return mockChain({ data: { default_voucher_series: 'V' }, error: null })
|
||||
}
|
||||
if (table === 'voucher_sequences') {
|
||||
return mockChain({ data: { last_number: 12 }, error: null })
|
||||
}
|
||||
throw new Error(`Unexpected table: ${table}`)
|
||||
})
|
||||
|
||||
const response = await GET(mkReq(), mkParams())
|
||||
const body = await response.json()
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(body.data).toEqual({ next: 13, series: 'V', fiscal_period_id: 'period-2' })
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,62 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { withRouteContext } from '@/lib/api/with-route-context'
|
||||
import { errorResponse } from '@/lib/errors/get-structured-error'
|
||||
|
||||
export const GET = withRouteContext(
|
||||
'voucher_sequence.next',
|
||||
async (_request, ctx) => {
|
||||
const { supabase, companyId, log, requestId } = ctx
|
||||
|
||||
const today = new Date().toISOString().split('T')[0]
|
||||
|
||||
const [{ data: period, error: periodError }, { data: settings, error: settingsError }] =
|
||||
await Promise.all([
|
||||
supabase
|
||||
.from('fiscal_periods')
|
||||
.select('id')
|
||||
.eq('company_id', companyId)
|
||||
.lte('period_start', today)
|
||||
.gte('period_end', today)
|
||||
.maybeSingle(),
|
||||
supabase
|
||||
.from('company_settings')
|
||||
.select('default_voucher_series')
|
||||
.eq('company_id', companyId)
|
||||
.maybeSingle(),
|
||||
])
|
||||
|
||||
if (periodError) {
|
||||
log.error('fiscal_periods lookup failed', periodError)
|
||||
return errorResponse(periodError, log, { requestId })
|
||||
}
|
||||
if (settingsError) {
|
||||
log.error('company_settings lookup failed', settingsError)
|
||||
return errorResponse(settingsError, log, { requestId })
|
||||
}
|
||||
|
||||
const series = settings?.default_voucher_series || 'A'
|
||||
|
||||
if (!period) {
|
||||
return NextResponse.json({ data: { next: null, series, fiscal_period_id: null } })
|
||||
}
|
||||
|
||||
const { data: sequence, error: sequenceError } = await supabase
|
||||
.from('voucher_sequences')
|
||||
.select('last_number')
|
||||
.eq('company_id', companyId)
|
||||
.eq('fiscal_period_id', period.id)
|
||||
.eq('voucher_series', series)
|
||||
.maybeSingle()
|
||||
|
||||
if (sequenceError) {
|
||||
log.error('voucher_sequences lookup failed', sequenceError)
|
||||
return errorResponse(sequenceError, log, { requestId })
|
||||
}
|
||||
|
||||
const next = (sequence?.last_number ?? 0) + 1
|
||||
|
||||
return NextResponse.json({
|
||||
data: { next, series, fiscal_period_id: period.id },
|
||||
})
|
||||
},
|
||||
)
|
||||
Reference in New Issue
Block a user