acb85edf4a
- Add 8 new Zod schemas (UpdateCustomer, UpdateSupplier, UpdateSupplierInvoice, UpdateAccount, BankUnlink, RunReconciliation, CorrectJournalEntry, EvaluateMappingRules) and wire validateBody() into 24 JSON-body API routes - Remove redundant manual validation checks replaced by Zod - Add comprehensive schema tests (222 tests) - Improve type definitions in types/index.ts with expanded interfaces - Refactor extension types (push-notifications, receipt-ocr) for cleaner imports - Update transaction components (BatchCategorySelector, SwipeCategorizationView, QuickReviewDialog, VatTreatmentSelect) and invoice inbox workspace - Add invoice-inbox utilities and type decoupling tests - Fix NE-bilaga, SRU export, and invoice PDF template type usage - Update CLAUDE.md with expanded architecture documentation Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
75 lines
2.0 KiB
TypeScript
75 lines
2.0 KiB
TypeScript
import { createClient } from '@/lib/supabase/server'
|
|
import { NextResponse } from 'next/server'
|
|
import { validateBody } from '@/lib/api/validate'
|
|
import { CreateSupplierSchema } from '@/lib/api/schemas'
|
|
|
|
export async function GET() {
|
|
const supabase = await createClient()
|
|
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const { data, error } = await supabase
|
|
.from('suppliers')
|
|
.select('*')
|
|
.eq('user_id', user.id)
|
|
.order('name', { ascending: true })
|
|
|
|
if (error) {
|
|
return NextResponse.json({ error: error.message }, { status: 500 })
|
|
}
|
|
|
|
return NextResponse.json({ data })
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
const supabase = await createClient()
|
|
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const result = await validateBody(request, CreateSupplierSchema)
|
|
if (!result.success) return result.response
|
|
const body = result.data
|
|
|
|
const { data, error } = await supabase
|
|
.from('suppliers')
|
|
.insert({
|
|
user_id: user.id,
|
|
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) {
|
|
return NextResponse.json({ error: error.message }, { status: 500 })
|
|
}
|
|
|
|
return NextResponse.json({ data })
|
|
}
|