03b569d708
- Remove all sector-specific extensions (construction, ecommerce, export, hotel, restaurant, tech) — only general-purpose extensions remain - Move NE-bilaga and SRU export from extensions to core reports (lib/reports/) - Move moms-box-mapping from extensions/export/shared to lib/vat/ - Replace per-extension API routes with catch-all dispatcher (app/api/extensions/ext/[...path]/route.ts) - Add manifest.json for each extension with metadata, env vars, and deps - Add api-routes.ts pattern for extension-defined API endpoints - Add code generation scripts (generate-extension-registry, create-extension) - Add extensions.config.json for opt-in extension loading - Add extensions.schema.json for config validation - Add email service interface with noop default (lib/email/service.ts) - Add CI workflow (core-build.yml) to verify core builds with zero extensions - Add migration 045: expand account_type CHECK for untaxed_reserves - Update CLAUDE.md with comprehensive extension system documentation - Update all report engines and bookkeeping services for new imports - Clean up extensions.schema.json to only list existing extensions Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
156 lines
3.6 KiB
TypeScript
156 lines
3.6 KiB
TypeScript
import { createClient } from '@/lib/supabase/server'
|
|
import { NextResponse } from 'next/server'
|
|
import { saveMappings } from '@/lib/import/sie-import'
|
|
import type { AccountMapping } from '@/lib/import/types'
|
|
|
|
/**
|
|
* GET /api/import/sie/mappings
|
|
* Get all saved account mappings for the user
|
|
*/
|
|
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('sie_account_mappings')
|
|
.select('*')
|
|
.eq('user_id', user.id)
|
|
.order('source_account')
|
|
|
|
if (error) {
|
|
return NextResponse.json({ error: error.message }, { status: 500 })
|
|
}
|
|
|
|
return NextResponse.json({ data })
|
|
}
|
|
|
|
/**
|
|
* POST /api/import/sie/mappings
|
|
* Save account mappings (bulk upsert)
|
|
*/
|
|
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 body = await request.json()
|
|
const mappings: AccountMapping[] = body.mappings
|
|
|
|
if (!mappings || !Array.isArray(mappings)) {
|
|
return NextResponse.json({ error: 'Invalid mappings data' }, { status: 400 })
|
|
}
|
|
|
|
try {
|
|
await saveMappings(supabase, user.id, mappings)
|
|
return NextResponse.json({ success: true })
|
|
} catch (error) {
|
|
return NextResponse.json(
|
|
{ error: error instanceof Error ? error.message : 'Failed to save mappings' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* PUT /api/import/sie/mappings
|
|
* Update a single mapping
|
|
*/
|
|
export async function PUT(request: Request) {
|
|
const supabase = await createClient()
|
|
|
|
const {
|
|
data: { user },
|
|
} = await supabase.auth.getUser()
|
|
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const body = await request.json()
|
|
const { sourceAccount, targetAccount } = body
|
|
|
|
if (!sourceAccount || !targetAccount) {
|
|
return NextResponse.json(
|
|
{ error: 'sourceAccount and targetAccount are required' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const { data, error } = await supabase
|
|
.from('sie_account_mappings')
|
|
.upsert({
|
|
user_id: user.id,
|
|
source_account: sourceAccount,
|
|
target_account: targetAccount,
|
|
confidence: 1.0,
|
|
match_type: 'manual',
|
|
}, {
|
|
onConflict: 'user_id,source_account',
|
|
})
|
|
.select()
|
|
.single()
|
|
|
|
if (error) {
|
|
return NextResponse.json({ error: error.message }, { status: 500 })
|
|
}
|
|
|
|
return NextResponse.json({ data })
|
|
}
|
|
|
|
/**
|
|
* DELETE /api/import/sie/mappings
|
|
* Delete a specific mapping or all mappings
|
|
*/
|
|
export async function DELETE(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 sourceAccount = searchParams.get('sourceAccount')
|
|
|
|
if (sourceAccount) {
|
|
// Delete specific mapping
|
|
const { error } = await supabase
|
|
.from('sie_account_mappings')
|
|
.delete()
|
|
.eq('user_id', user.id)
|
|
.eq('source_account', sourceAccount)
|
|
|
|
if (error) {
|
|
return NextResponse.json({ error: error.message }, { status: 500 })
|
|
}
|
|
} else {
|
|
// Delete all mappings
|
|
const { error } = await supabase
|
|
.from('sie_account_mappings')
|
|
.delete()
|
|
.eq('user_id', user.id)
|
|
|
|
if (error) {
|
|
return NextResponse.json({ error: error.message }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
return NextResponse.json({ success: true })
|
|
}
|