81e9dd224e
* 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
96 lines
2.8 KiB
TypeScript
96 lines
2.8 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { eventBus } from '@/lib/events'
|
|
import { ensureInitialized } from '@/lib/init'
|
|
import { validateBody } from '@/lib/api/validate'
|
|
import { CreateSupplierSchema } from '@/lib/api/schemas'
|
|
import { withRouteContext } from '@/lib/api/with-route-context'
|
|
import { errorResponse, errorResponseFromCode } from '@/lib/errors/get-structured-error'
|
|
import type { Supplier } from '@/types'
|
|
|
|
ensureInitialized()
|
|
|
|
export const GET = withRouteContext(
|
|
'supplier.list',
|
|
async (_request, ctx) => {
|
|
const { supabase, companyId, log, requestId } = ctx
|
|
|
|
const { data, error } = await supabase
|
|
.from('suppliers')
|
|
.select('*')
|
|
.eq('company_id', companyId)
|
|
.order('name', { ascending: true })
|
|
|
|
if (error) {
|
|
log.error('supplier list failed', error)
|
|
return errorResponse(error, log, { requestId })
|
|
}
|
|
|
|
return NextResponse.json({ data })
|
|
},
|
|
)
|
|
|
|
export const POST = withRouteContext(
|
|
'supplier.create',
|
|
async (request, ctx) => {
|
|
const { user, supabase, companyId, log, requestId } = ctx
|
|
|
|
const result = await validateBody(request, CreateSupplierSchema, {
|
|
log,
|
|
operation: 'supplier.create',
|
|
})
|
|
if (!result.success) return result.response
|
|
const body = result.data
|
|
|
|
const { data, error } = await supabase
|
|
.from('suppliers')
|
|
.insert({
|
|
user_id: user.id,
|
|
company_id: companyId,
|
|
name: body.name,
|
|
supplier_type: body.supplier_type,
|
|
email: body.email,
|
|
phone: body.phone,
|
|
address_line1: body.address_line1,
|
|
address_line2: body.address_line2,
|
|
postal_code: body.postal_code,
|
|
city: body.city,
|
|
country: body.country || 'SE',
|
|
org_number: body.org_number,
|
|
vat_number: body.vat_number,
|
|
bankgiro: body.bankgiro,
|
|
plusgiro: body.plusgiro,
|
|
bank_account: body.bank_account,
|
|
iban: body.iban,
|
|
bic: body.bic,
|
|
default_expense_account: body.default_expense_account,
|
|
default_payment_terms: body.default_payment_terms || 30,
|
|
default_currency: body.default_currency || 'SEK',
|
|
notes: body.notes,
|
|
})
|
|
.select()
|
|
.single()
|
|
|
|
if (error) {
|
|
if (error.code === '23505') {
|
|
return errorResponseFromCode('SUPPLIER_DUPLICATE_ORG_NUMBER', log, {
|
|
requestId,
|
|
details: { orgNumber: body.org_number },
|
|
})
|
|
}
|
|
log.error('supplier insert failed', error)
|
|
return errorResponseFromCode('SUPPLIER_CREATE_FAILED', log, {
|
|
requestId,
|
|
details: { reason: error.message },
|
|
})
|
|
}
|
|
|
|
await eventBus.emit({
|
|
type: 'supplier.created',
|
|
payload: { supplier: data as Supplier, companyId, userId: user.id },
|
|
})
|
|
|
|
return NextResponse.json({ data })
|
|
},
|
|
{ requireWrite: true },
|
|
)
|