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>
80 lines
2.3 KiB
TypeScript
80 lines
2.3 KiB
TypeScript
import { createClient } from '@/lib/supabase/server'
|
|
import { fetchAllRows } from '@/lib/supabase/fetch-all'
|
|
import { NextResponse } from 'next/server'
|
|
import { validateBody } from '@/lib/api/validate'
|
|
import { CreateAccountSchema } from '@/lib/api/schemas'
|
|
|
|
export async function GET(request: Request) {
|
|
const supabase = await createClient()
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const { searchParams } = new URL(request.url)
|
|
const accountClass = searchParams.get('class')
|
|
const activeOnly = searchParams.get('active') !== 'false'
|
|
|
|
try {
|
|
const data = await fetchAllRows(({ from, to }) => {
|
|
let query = supabase
|
|
.from('chart_of_accounts')
|
|
.select('*')
|
|
.eq('user_id', user.id)
|
|
.order('sort_order')
|
|
|
|
if (activeOnly) {
|
|
query = query.eq('is_active', true)
|
|
}
|
|
|
|
if (accountClass) {
|
|
query = query.eq('account_class', parseInt(accountClass))
|
|
}
|
|
|
|
return query.range(from, to)
|
|
})
|
|
|
|
return NextResponse.json({ data })
|
|
} catch (error) {
|
|
return NextResponse.json({ error: error instanceof Error ? error.message : 'Failed to fetch accounts' }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
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 validation = await validateBody(request, CreateAccountSchema)
|
|
if (!validation.success) return validation.response
|
|
const body = validation.data
|
|
|
|
const { data, error } = await supabase
|
|
.from('chart_of_accounts')
|
|
.insert({
|
|
user_id: user.id,
|
|
account_number: body.account_number,
|
|
account_name: body.account_name,
|
|
account_class: parseInt(body.account_number[0]),
|
|
account_group: body.account_number.substring(0, 2),
|
|
account_type: body.account_type,
|
|
normal_balance: body.normal_balance,
|
|
plan_type: body.plan_type || 'k1',
|
|
is_system_account: false,
|
|
description: body.description || null,
|
|
sort_order: parseInt(body.account_number),
|
|
})
|
|
.select()
|
|
.single()
|
|
|
|
if (error) {
|
|
return NextResponse.json({ error: error.message }, { status: 500 })
|
|
}
|
|
|
|
return NextResponse.json({ data })
|
|
}
|