first commit
This commit is contained in:
36
app/api/admin/commissions/settings/[id]/route.ts
Normal file
36
app/api/admin/commissions/settings/[id]/route.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { auth } from '@/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
export async function PUT(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user?.id || session.user.role !== 'ADMIN') {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const { id: settingId } = await params
|
||||
const { level, percentage, isActive } = await request.json()
|
||||
|
||||
const setting = await prisma.commissionSettings.update({
|
||||
where: { id: settingId },
|
||||
data: {
|
||||
level,
|
||||
percentage,
|
||||
isActive
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json(setting)
|
||||
} catch (error) {
|
||||
console.error('Error updating commission setting:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to update commission setting' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
41
app/api/admin/commissions/settings/route.ts
Normal file
41
app/api/admin/commissions/settings/route.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { auth } from '@/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user?.id || session.user.role !== 'ADMIN') {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
let settings = await prisma.commissionSettings.findMany({
|
||||
orderBy: { level: 'asc' }
|
||||
})
|
||||
|
||||
// If no settings exist, create default ones
|
||||
if (settings.length === 0) {
|
||||
const defaultSettings = [
|
||||
{ level: 1, percentage: 10, isActive: true },
|
||||
{ level: 2, percentage: 5, isActive: true },
|
||||
{ level: 3, percentage: 3, isActive: true },
|
||||
{ level: 4, percentage: 2, isActive: true },
|
||||
{ level: 5, percentage: 1, isActive: true }
|
||||
]
|
||||
|
||||
await prisma.commissionSettings.createMany({
|
||||
data: defaultSettings
|
||||
})
|
||||
|
||||
settings = await prisma.commissionSettings.findMany({
|
||||
orderBy: { level: 'asc' }
|
||||
})
|
||||
}
|
||||
|
||||
return NextResponse.json({ settings: settings || [] })
|
||||
} catch (error) {
|
||||
console.error('Error fetching commission settings:', error)
|
||||
return NextResponse.json({ settings: [] })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user