fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma strängvärde byggs med konkatenering i stället för att matcha mönstret.
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import * as XLSX from 'xlsx'
|
||||
import { createMockRequest, parseJsonResponse, createQueuedMockSupabase } from '@/tests/helpers'
|
||||
import { detectArticleColumns } from '@/lib/import/articles/column-detector'
|
||||
|
||||
const { supabase: mockSupabase, enqueue, reset } = createQueuedMockSupabase()
|
||||
|
||||
vi.mock('@/lib/supabase/server', () => ({
|
||||
createClient: () => Promise.resolve(mockSupabase),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/company/context', () => ({
|
||||
requireCompanyId: vi.fn().mockResolvedValue('company-1'),
|
||||
getActiveCompanyId: vi.fn().mockResolvedValue('company-1'),
|
||||
}))
|
||||
|
||||
const mockFetchAllRows = vi.fn()
|
||||
vi.mock('@/lib/supabase/fetch-all', () => ({
|
||||
fetchAllRows: (...a: unknown[]) => mockFetchAllRows(...a),
|
||||
}))
|
||||
|
||||
import { GET } from '../route'
|
||||
|
||||
const mockUser = { id: 'user-1', email: 'test@test.se' }
|
||||
|
||||
const ARTICLE = {
|
||||
id: 'a1',
|
||||
article_number: '100',
|
||||
name: 'Webdesign',
|
||||
name_en: 'Web design',
|
||||
type: 'tjanst',
|
||||
unit: 'st',
|
||||
price_excl_vat: 1200,
|
||||
vat_rate: 25,
|
||||
currency: 'EUR',
|
||||
revenue_account: '3001',
|
||||
cost_price: 400,
|
||||
ean: '7350000000001',
|
||||
housework_type: null,
|
||||
notes: 'Kommentar med åäö',
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
reset()
|
||||
mockSupabase.auth.getUser.mockResolvedValue({ data: { user: mockUser } })
|
||||
mockFetchAllRows.mockResolvedValue([ARTICLE])
|
||||
})
|
||||
|
||||
describe('GET /api/export/articles', () => {
|
||||
it('returns 401 when unauthenticated', async () => {
|
||||
mockSupabase.auth.getUser.mockResolvedValue({ data: { user: null } })
|
||||
const res = await GET(createMockRequest('/api/export/articles'))
|
||||
const { status } = await parseJsonResponse(res)
|
||||
expect(status).toBe(401)
|
||||
})
|
||||
|
||||
it('returns an xlsx workbook whose headers round-trip through the importer', async () => {
|
||||
enqueue({ data: { company_name: 'Acme AB' } })
|
||||
|
||||
const res = await GET(createMockRequest('/api/export/articles'))
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
expect(res.headers.get('Content-Type')).toContain('spreadsheetml')
|
||||
const disposition = res.headers.get('Content-Disposition') || ''
|
||||
expect(disposition).toContain('attachment')
|
||||
expect(disposition).toContain('artiklar-acme-ab')
|
||||
expect(disposition).toContain('.xlsx')
|
||||
|
||||
const buf = Buffer.from(await res.arrayBuffer())
|
||||
expect(buf.length).toBeGreaterThan(0)
|
||||
|
||||
const wb = XLSX.read(new Uint8Array(buf), { type: 'array' })
|
||||
const sheet = wb.Sheets[wb.SheetNames[0]]
|
||||
const rows = XLSX.utils.sheet_to_json<string[]>(sheet, { header: 1 })
|
||||
const headers = (rows[0] as string[]).map(String)
|
||||
// Round-trip: the exported headers must re-detect with high confidence.
|
||||
const detected = detectArticleColumns(headers)
|
||||
expect(detected.confidence).toBeGreaterThanOrEqual(0.8)
|
||||
expect(detected.name_col).toBeGreaterThanOrEqual(0)
|
||||
expect(detected.price_col).not.toBeNull()
|
||||
expect(detected.vat_rate_col).not.toBeNull()
|
||||
expect(detected.revenue_account_col).not.toBeNull()
|
||||
|
||||
// Non-SEK prices export their currency; the price column must still map
|
||||
// to Försäljningspris, not be swallowed by the new Valuta column.
|
||||
const valutaIdx = headers.indexOf('Valuta')
|
||||
expect(valutaIdx).toBeGreaterThanOrEqual(0)
|
||||
expect((rows[1] as string[])[valutaIdx]).toBe('EUR')
|
||||
expect(headers[detected.price_col as number]).toBe('Försäljningspris')
|
||||
})
|
||||
|
||||
it('returns a UTF-8 BOM CSV when format=csv', async () => {
|
||||
enqueue({ data: { company_name: 'Acme AB' } })
|
||||
|
||||
const res = await GET(createMockRequest('/api/export/articles', { searchParams: { format: 'csv' } }))
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
expect(res.headers.get('Content-Type')).toContain('text/csv')
|
||||
expect(res.headers.get('Content-Disposition')).toContain('.csv')
|
||||
|
||||
const buf = Buffer.from(await res.arrayBuffer())
|
||||
// UTF-8 BOM
|
||||
expect([buf[0], buf[1], buf[2]]).toEqual([0xef, 0xbb, 0xbf])
|
||||
expect(buf.toString('utf-8')).toContain('Webdesign')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,98 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { withRouteContext } from '@/lib/api/with-route-context'
|
||||
import { errorResponse } from '@/lib/errors/get-structured-error'
|
||||
import { fetchAllRows } from '@/lib/supabase/fetch-all'
|
||||
import { textColumn, decimalColumn, integerColumn } from '@/lib/reports/xlsx-export'
|
||||
import { buildRegisterExport, parseExportFormat, todayIso } from '@/lib/export/register-export'
|
||||
import type { Article } from '@/types'
|
||||
|
||||
/**
|
||||
* GET /api/export/articles[?format=csv][&include_inactive=1]
|
||||
*
|
||||
* Downloads the article register as xlsx (default) or csv. Read-only: viewers
|
||||
* may export. Column headers match the article importer's detector keywords so
|
||||
* the file round-trips (export → edit → re-import), including Valuta.
|
||||
*/
|
||||
export const GET = withRouteContext(
|
||||
'article.export',
|
||||
async (request, ctx) => {
|
||||
const { supabase, companyId, log, requestId } = ctx
|
||||
|
||||
const url = new URL(request.url)
|
||||
const format = parseExportFormat(url.searchParams.get('format'))
|
||||
const includeInactive = url.searchParams.get('include_inactive') === '1'
|
||||
|
||||
try {
|
||||
const { data: companyRow } = await supabase
|
||||
.from('company_settings')
|
||||
.select('company_name')
|
||||
.eq('company_id', companyId)
|
||||
.single()
|
||||
|
||||
const articles = (await fetchAllRows(({ from, to }) => {
|
||||
let query = supabase
|
||||
.from('articles')
|
||||
.select('*')
|
||||
.eq('company_id', companyId)
|
||||
if (!includeInactive) query = query.eq('active', true)
|
||||
return query.order('name', { ascending: true }).range(from, to)
|
||||
})) as unknown as Article[]
|
||||
|
||||
const { buffer, contentType, filename } = buildRegisterExport(
|
||||
[
|
||||
{
|
||||
name: 'Artiklar',
|
||||
columns: [
|
||||
textColumn('Artikelnummer'),
|
||||
textColumn('Benämning'),
|
||||
textColumn('Benämning (engelska)'),
|
||||
textColumn('Typ'),
|
||||
textColumn('Enhet'),
|
||||
// Prices are decimal (not the kr-suffixed currency format):
|
||||
// articles can carry a non-SEK currency, exported in Valuta.
|
||||
decimalColumn('Försäljningspris'),
|
||||
textColumn('Valuta'),
|
||||
integerColumn('Moms %'),
|
||||
textColumn('Försäljningskonto'),
|
||||
decimalColumn('Inköpspris'),
|
||||
textColumn('EAN'),
|
||||
textColumn('ROT/RUT'),
|
||||
textColumn('Anteckning'),
|
||||
],
|
||||
rows: articles,
|
||||
mapRow: (a) => [
|
||||
a.article_number,
|
||||
a.name,
|
||||
a.name_en,
|
||||
a.type,
|
||||
a.unit,
|
||||
a.price_excl_vat,
|
||||
a.currency,
|
||||
a.vat_rate,
|
||||
a.revenue_account,
|
||||
a.cost_price,
|
||||
a.ean,
|
||||
a.housework_type,
|
||||
a.notes,
|
||||
],
|
||||
},
|
||||
],
|
||||
{ format, slug: 'artiklar', companyName: companyRow?.company_name ?? '', date: todayIso() },
|
||||
)
|
||||
|
||||
// Audit trail: who exported what, when (sensitive bulk register download).
|
||||
log.info('register exported', { entity: 'articles', format, rowCount: articles.length })
|
||||
|
||||
return new NextResponse(new Uint8Array(buffer), {
|
||||
headers: {
|
||||
'Content-Type': contentType,
|
||||
'Content-Disposition': `attachment; filename="${filename}"`,
|
||||
'Cache-Control': 'no-store',
|
||||
},
|
||||
})
|
||||
} catch (err) {
|
||||
log.error('article export failed', err as Error)
|
||||
return errorResponse(err, log, { requestId })
|
||||
}
|
||||
},
|
||||
)
|
||||
@@ -0,0 +1,153 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import * as XLSX from 'xlsx'
|
||||
import { createMockRequest, parseJsonResponse, createQueuedMockSupabase } from '@/tests/helpers'
|
||||
|
||||
const { supabase: mockSupabase, enqueue, reset } = createQueuedMockSupabase()
|
||||
|
||||
vi.mock('@/lib/supabase/server', () => ({
|
||||
createClient: () => Promise.resolve(mockSupabase),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/company/context', () => ({
|
||||
requireCompanyId: vi.fn().mockResolvedValue('company-1'),
|
||||
getActiveCompanyId: vi.fn().mockResolvedValue('company-1'),
|
||||
}))
|
||||
|
||||
const mockFetchAllRows = vi.fn()
|
||||
vi.mock('@/lib/supabase/fetch-all', () => ({
|
||||
fetchAllRows: (...a: unknown[]) => mockFetchAllRows(...a),
|
||||
}))
|
||||
|
||||
import { GET } from '../route'
|
||||
import { encryptPersonnummer } from '@/lib/salary/personnummer'
|
||||
import { UNDECRYPTABLE_PERSONAL_NUMBER_MASK } from '@/lib/customers/protect-personal-number'
|
||||
|
||||
const mockUser = { id: 'user-1', email: 'test@test.se' }
|
||||
|
||||
const CUSTOMER = {
|
||||
id: 'c1',
|
||||
name: 'Acme AB',
|
||||
customer_type: 'swedish_business',
|
||||
org_number: '5560217780',
|
||||
personal_number: null,
|
||||
email: 'kontakt@acme.se',
|
||||
phone: '0701234567',
|
||||
address_line1: 'Storgatan 1',
|
||||
address_line2: null,
|
||||
postal_code: '11122',
|
||||
city: 'Göteborg',
|
||||
country: 'Sweden',
|
||||
vat_number: 'SE556021778001',
|
||||
default_payment_terms: 30,
|
||||
notes: null,
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
reset()
|
||||
mockSupabase.auth.getUser.mockResolvedValue({ data: { user: mockUser } })
|
||||
mockFetchAllRows.mockResolvedValue([CUSTOMER])
|
||||
})
|
||||
|
||||
describe('GET /api/export/customers', () => {
|
||||
it('returns 401 when unauthenticated', async () => {
|
||||
mockSupabase.auth.getUser.mockResolvedValue({ data: { user: null } })
|
||||
const res = await GET(createMockRequest('/api/export/customers'), { params: Promise.resolve({}) })
|
||||
const { status } = await parseJsonResponse(res)
|
||||
expect(status).toBe(401)
|
||||
})
|
||||
|
||||
it('returns an xlsx customer register', async () => {
|
||||
enqueue({ data: { company_name: 'Acme AB' } })
|
||||
const res = await GET(createMockRequest('/api/export/customers'), { params: Promise.resolve({}) })
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
expect(res.headers.get('Content-Type')).toContain('spreadsheetml')
|
||||
expect(res.headers.get('Content-Disposition')).toContain('kunder-')
|
||||
|
||||
const buf = Buffer.from(await res.arrayBuffer())
|
||||
const wb = XLSX.read(new Uint8Array(buf), { type: 'array' })
|
||||
const sheet = wb.Sheets[wb.SheetNames[0]]
|
||||
const rows = XLSX.utils.sheet_to_json<string[]>(sheet, { header: 1 })
|
||||
expect((rows[0] as string[])[0]).toBe('Namn')
|
||||
expect((rows[1] as string[])).toContain('Acme AB')
|
||||
})
|
||||
|
||||
it('returns a CSV with BOM when format=csv', async () => {
|
||||
enqueue({ data: { company_name: 'Acme AB' } })
|
||||
const res = await GET(createMockRequest('/api/export/customers', { searchParams: { format: 'csv' } }), { params: Promise.resolve({}) })
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
expect(res.headers.get('Content-Type')).toContain('text/csv')
|
||||
const buf = Buffer.from(await res.arrayBuffer())
|
||||
expect([buf[0], buf[1], buf[2]]).toEqual([0xef, 0xbb, 0xbf])
|
||||
expect(buf.toString('utf-8')).toContain('Göteborg')
|
||||
})
|
||||
|
||||
it('masks an encrypted personal_number: never ciphertext, never the full number', async () => {
|
||||
// customers.personal_number holds AES-256-GCM ciphertext (migration
|
||||
// 20260726110000). The export must show what the UI shows a member:
|
||||
// '********-1234', not the hex blob and not the decrypted personnummer.
|
||||
const stored = encryptPersonnummer('19900101-1234') // synthetic
|
||||
mockFetchAllRows.mockResolvedValue([
|
||||
{
|
||||
...CUSTOMER,
|
||||
id: 'c2',
|
||||
name: 'Anna Andersson',
|
||||
customer_type: 'individual',
|
||||
org_number: null,
|
||||
vat_number: null,
|
||||
personal_number: stored,
|
||||
},
|
||||
])
|
||||
enqueue({ data: { company_name: 'Acme AB' } })
|
||||
|
||||
const res = await GET(createMockRequest('/api/export/customers'), { params: Promise.resolve({}) })
|
||||
expect(res.status).toBe(200)
|
||||
|
||||
const buf = Buffer.from(await res.arrayBuffer())
|
||||
const wb = XLSX.read(new Uint8Array(buf), { type: 'array' })
|
||||
const sheet = wb.Sheets[wb.SheetNames[0]]
|
||||
const rows = XLSX.utils.sheet_to_json<string[]>(sheet, { header: 1 })
|
||||
const serialized = JSON.stringify(rows)
|
||||
|
||||
expect(rows[1] as string[]).toContain('********-1234')
|
||||
// Neither the ciphertext nor the birthdate half of the number may appear.
|
||||
expect(serialized).not.toContain(stored)
|
||||
expect(serialized).not.toContain('19900101')
|
||||
})
|
||||
|
||||
it('falls back to a placeholder mask when the stored value cannot be decrypted', async () => {
|
||||
// Hex of the shape the DB CHECK accepts, but with an auth tag that can
|
||||
// never verify. One bad row must neither leak nor abort the export.
|
||||
const garbage = 'ab'.repeat(40)
|
||||
mockFetchAllRows.mockResolvedValue([
|
||||
{
|
||||
...CUSTOMER,
|
||||
id: 'c3',
|
||||
name: 'Berit Bengtsson',
|
||||
customer_type: 'individual',
|
||||
org_number: null,
|
||||
vat_number: null,
|
||||
personal_number: garbage,
|
||||
},
|
||||
])
|
||||
enqueue({ data: { company_name: 'Acme AB' } })
|
||||
|
||||
const res = await GET(createMockRequest('/api/export/customers', { searchParams: { format: 'csv' } }), { params: Promise.resolve({}) })
|
||||
expect(res.status).toBe(200)
|
||||
|
||||
const text = Buffer.from(await res.arrayBuffer()).toString('utf-8')
|
||||
expect(text).toContain(UNDECRYPTABLE_PERSONAL_NUMBER_MASK)
|
||||
expect(text).not.toContain(garbage)
|
||||
})
|
||||
|
||||
it('keeps the raw org number for business customers', async () => {
|
||||
// Org numbers are public registry data: they export untouched, and the
|
||||
// masking branch only engages when org_number is absent.
|
||||
enqueue({ data: { company_name: 'Acme AB' } })
|
||||
const res = await GET(createMockRequest('/api/export/customers', { searchParams: { format: 'csv' } }), { params: Promise.resolve({}) })
|
||||
const text = Buffer.from(await res.arrayBuffer()).toString('utf-8')
|
||||
expect(text).toContain('5560217780')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,101 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { withRouteContext } from '@/lib/api/with-route-context'
|
||||
import { errorResponse } from '@/lib/errors/get-structured-error'
|
||||
import { fetchAllRows } from '@/lib/supabase/fetch-all'
|
||||
import { textColumn, integerColumn } from '@/lib/reports/xlsx-export'
|
||||
import { buildRegisterExport, parseExportFormat, todayIso } from '@/lib/export/register-export'
|
||||
import { maskStoredCustomerPersonalNumber } from '@/lib/customers/protect-personal-number'
|
||||
import type { Customer } from '@/types'
|
||||
|
||||
/**
|
||||
* GET /api/export/customers[?format=csv]
|
||||
*
|
||||
* Downloads the customer register as xlsx (default) or csv. Read-only: viewers
|
||||
* may export. Headers match the customer importer's detector keywords so files
|
||||
* round-trip.
|
||||
*/
|
||||
export const GET = withRouteContext(
|
||||
'customer.export',
|
||||
async (request, ctx) => {
|
||||
const { supabase, companyId, log, requestId } = ctx
|
||||
|
||||
const format = parseExportFormat(new URL(request.url).searchParams.get('format'))
|
||||
|
||||
try {
|
||||
const { data: companyRow } = await supabase
|
||||
.from('company_settings')
|
||||
.select('company_name')
|
||||
.eq('company_id', companyId)
|
||||
.single()
|
||||
|
||||
const customers = (await fetchAllRows(({ from, to }) =>
|
||||
supabase
|
||||
.from('customers')
|
||||
.select('*')
|
||||
.eq('company_id', companyId)
|
||||
.order('name', { ascending: true })
|
||||
.range(from, to),
|
||||
)) as unknown as Customer[]
|
||||
|
||||
const { buffer, contentType, filename } = buildRegisterExport(
|
||||
[
|
||||
{
|
||||
name: 'Kunder',
|
||||
columns: [
|
||||
textColumn('Namn'),
|
||||
textColumn('Org-/personnummer'),
|
||||
textColumn('Kundtyp'),
|
||||
textColumn('E-post'),
|
||||
textColumn('Telefon'),
|
||||
textColumn('Adress'),
|
||||
textColumn('Adressrad 2'),
|
||||
textColumn('Postnummer'),
|
||||
textColumn('Ort'),
|
||||
textColumn('Land'),
|
||||
textColumn('VAT-nummer'),
|
||||
integerColumn('Betalningsvillkor'),
|
||||
textColumn('Anteckning'),
|
||||
],
|
||||
rows: customers,
|
||||
mapRow: (c) => [
|
||||
c.name,
|
||||
// personal_number holds AES-256-GCM ciphertext (migration
|
||||
// 20260726110000). Decrypt-and-mask exactly like the customer
|
||||
// read surfaces: the export shows a member the same
|
||||
// '********-1234' the UI shows, never the ciphertext and never
|
||||
// the full personnummer (GDPR Art. 5(1)(f) data minimization on
|
||||
// a file that leaves the system). Decrypt failures fall back to
|
||||
// a placeholder mask instead of aborting the export.
|
||||
c.org_number ?? maskStoredCustomerPersonalNumber(c.personal_number),
|
||||
c.customer_type,
|
||||
c.email,
|
||||
c.phone,
|
||||
c.address_line1,
|
||||
c.address_line2,
|
||||
c.postal_code,
|
||||
c.city,
|
||||
c.country,
|
||||
c.vat_number,
|
||||
c.default_payment_terms,
|
||||
c.notes,
|
||||
],
|
||||
},
|
||||
],
|
||||
{ format, slug: 'kunder', companyName: companyRow?.company_name ?? '', date: todayIso() },
|
||||
)
|
||||
|
||||
log.info('register exported', { entity: 'customers', format, rowCount: customers.length })
|
||||
|
||||
return new NextResponse(new Uint8Array(buffer), {
|
||||
headers: {
|
||||
'Content-Type': contentType,
|
||||
'Content-Disposition': `attachment; filename="${filename}"`,
|
||||
'Cache-Control': 'no-store',
|
||||
},
|
||||
})
|
||||
} catch (err) {
|
||||
log.error('customer export failed', err as Error)
|
||||
return errorResponse(err, log, { requestId })
|
||||
}
|
||||
},
|
||||
)
|
||||
@@ -0,0 +1,79 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import * as XLSX from 'xlsx'
|
||||
import { createMockRequest, parseJsonResponse, createQueuedMockSupabase } from '@/tests/helpers'
|
||||
|
||||
const { supabase: mockSupabase, enqueue, reset } = createQueuedMockSupabase()
|
||||
|
||||
vi.mock('@/lib/supabase/server', () => ({
|
||||
createClient: () => Promise.resolve(mockSupabase),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/company/context', () => ({
|
||||
requireCompanyId: vi.fn().mockResolvedValue('company-1'),
|
||||
getActiveCompanyId: vi.fn().mockResolvedValue('company-1'),
|
||||
}))
|
||||
|
||||
const mockFetchAllRows = vi.fn()
|
||||
vi.mock('@/lib/supabase/fetch-all', () => ({
|
||||
fetchAllRows: (...a: unknown[]) => mockFetchAllRows(...a),
|
||||
}))
|
||||
|
||||
import { GET } from '../route'
|
||||
|
||||
const mockUser = { id: 'user-1', email: 'test@test.se' }
|
||||
|
||||
const SUPPLIER = {
|
||||
id: 's1',
|
||||
name: 'Leverantör AB',
|
||||
supplier_type: 'swedish_business',
|
||||
org_number: '5560217780',
|
||||
vat_number: 'SE556021778001',
|
||||
email: 'faktura@lev.se',
|
||||
phone: null,
|
||||
address_line1: null,
|
||||
address_line2: null,
|
||||
postal_code: null,
|
||||
city: 'Malmö',
|
||||
country: 'Sweden',
|
||||
bankgiro: '5050-1055',
|
||||
plusgiro: null,
|
||||
bank_account: null,
|
||||
iban: null,
|
||||
bic: null,
|
||||
default_payment_terms: 30,
|
||||
default_currency: 'SEK',
|
||||
notes: null,
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
reset()
|
||||
mockSupabase.auth.getUser.mockResolvedValue({ data: { user: mockUser } })
|
||||
mockFetchAllRows.mockResolvedValue([SUPPLIER])
|
||||
})
|
||||
|
||||
describe('GET /api/export/suppliers', () => {
|
||||
it('returns 401 when unauthenticated', async () => {
|
||||
mockSupabase.auth.getUser.mockResolvedValue({ data: { user: null } })
|
||||
const res = await GET(createMockRequest('/api/export/suppliers'))
|
||||
const { status } = await parseJsonResponse(res)
|
||||
expect(status).toBe(401)
|
||||
})
|
||||
|
||||
it('returns an xlsx supplier register with banking columns', async () => {
|
||||
enqueue({ data: { company_name: 'Acme AB' } })
|
||||
const res = await GET(createMockRequest('/api/export/suppliers'))
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
expect(res.headers.get('Content-Disposition')).toContain('leverantorer-')
|
||||
|
||||
const buf = Buffer.from(await res.arrayBuffer())
|
||||
const wb = XLSX.read(new Uint8Array(buf), { type: 'array' })
|
||||
const sheet = wb.Sheets[wb.SheetNames[0]]
|
||||
const rows = XLSX.utils.sheet_to_json<string[]>(sheet, { header: 1 })
|
||||
const headers = (rows[0] as string[]).map(String)
|
||||
expect(headers).toContain('Bankgiro')
|
||||
expect(headers).toContain('Valuta')
|
||||
expect((rows[1] as string[])).toContain('Leverantör AB')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,105 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { withRouteContext } from '@/lib/api/with-route-context'
|
||||
import { errorResponse } from '@/lib/errors/get-structured-error'
|
||||
import { fetchAllRows } from '@/lib/supabase/fetch-all'
|
||||
import { textColumn, integerColumn } from '@/lib/reports/xlsx-export'
|
||||
import { buildRegisterExport, parseExportFormat, todayIso } from '@/lib/export/register-export'
|
||||
import type { Supplier } from '@/types'
|
||||
|
||||
/**
|
||||
* GET /api/export/suppliers[?format=csv]
|
||||
*
|
||||
* Downloads the supplier register as xlsx (default) or csv. Read-only: viewers
|
||||
* may export. Headers match the supplier importer's detector keywords so files
|
||||
* round-trip.
|
||||
*/
|
||||
export const GET = withRouteContext(
|
||||
'supplier.export',
|
||||
async (request, ctx) => {
|
||||
const { supabase, companyId, log, requestId } = ctx
|
||||
|
||||
const format = parseExportFormat(new URL(request.url).searchParams.get('format'))
|
||||
|
||||
try {
|
||||
const { data: companyRow } = await supabase
|
||||
.from('company_settings')
|
||||
.select('company_name')
|
||||
.eq('company_id', companyId)
|
||||
.single()
|
||||
|
||||
const suppliers = (await fetchAllRows(({ from, to }) =>
|
||||
supabase
|
||||
.from('suppliers')
|
||||
.select('*')
|
||||
.eq('company_id', companyId)
|
||||
.order('name', { ascending: true })
|
||||
.range(from, to),
|
||||
)) as unknown as Supplier[]
|
||||
|
||||
const { buffer, contentType, filename } = buildRegisterExport(
|
||||
[
|
||||
{
|
||||
name: 'Leverantörer',
|
||||
columns: [
|
||||
textColumn('Namn'),
|
||||
textColumn('Org-/personnummer'),
|
||||
textColumn('Leverantörstyp'),
|
||||
textColumn('E-post'),
|
||||
textColumn('Telefon'),
|
||||
textColumn('Adress'),
|
||||
textColumn('Adressrad 2'),
|
||||
textColumn('Postnummer'),
|
||||
textColumn('Ort'),
|
||||
textColumn('Land'),
|
||||
textColumn('VAT-nummer'),
|
||||
textColumn('Bankgiro'),
|
||||
textColumn('Plusgiro'),
|
||||
textColumn('Bankkonto'),
|
||||
textColumn('IBAN'),
|
||||
textColumn('BIC'),
|
||||
integerColumn('Betalningsvillkor'),
|
||||
textColumn('Valuta'),
|
||||
textColumn('Anteckning'),
|
||||
],
|
||||
rows: suppliers,
|
||||
mapRow: (s) => [
|
||||
s.name,
|
||||
s.org_number,
|
||||
s.supplier_type,
|
||||
s.email,
|
||||
s.phone,
|
||||
s.address_line1,
|
||||
s.address_line2,
|
||||
s.postal_code,
|
||||
s.city,
|
||||
s.country,
|
||||
s.vat_number,
|
||||
s.bankgiro,
|
||||
s.plusgiro,
|
||||
s.bank_account,
|
||||
s.iban,
|
||||
s.bic,
|
||||
s.default_payment_terms,
|
||||
s.default_currency,
|
||||
s.notes,
|
||||
],
|
||||
},
|
||||
],
|
||||
{ format, slug: 'leverantorer', companyName: companyRow?.company_name ?? '', date: todayIso() },
|
||||
)
|
||||
|
||||
log.info('register exported', { entity: 'suppliers', format, rowCount: suppliers.length })
|
||||
|
||||
return new NextResponse(new Uint8Array(buffer), {
|
||||
headers: {
|
||||
'Content-Type': contentType,
|
||||
'Content-Disposition': `attachment; filename="${filename}"`,
|
||||
'Cache-Control': 'no-store',
|
||||
},
|
||||
})
|
||||
} catch (err) {
|
||||
log.error('supplier export failed', err as Error)
|
||||
return errorResponse(err, log, { requestId })
|
||||
}
|
||||
},
|
||||
)
|
||||
Reference in New Issue
Block a user