first commit
This commit is contained in:
37
app/api/admin/commissions/[id]/route.ts
Normal file
37
app/api/admin/commissions/[id]/route.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { auth } from '@/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
export async function PATCH(
|
||||
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: commissionId } = await params
|
||||
const { status } = await request.json()
|
||||
|
||||
// Validate status
|
||||
if (!['PENDING', 'APPROVED', 'PAID', 'CANCELLED'].includes(status)) {
|
||||
return NextResponse.json({ error: 'Invalid status' }, { status: 400 })
|
||||
}
|
||||
|
||||
const commission = await prisma.commission.update({
|
||||
where: { id: commissionId },
|
||||
data: { status }
|
||||
})
|
||||
|
||||
return NextResponse.json(commission)
|
||||
} catch (error) {
|
||||
console.error('Error updating commission:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to update commission' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
77
app/api/admin/commissions/route.ts
Normal file
77
app/api/admin/commissions/route.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { auth } from '@/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user?.id || session.user.role !== 'ADMIN') {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const url = new URL(request.url)
|
||||
const status = url.searchParams.get('status')
|
||||
const type = url.searchParams.get('type')
|
||||
const level = url.searchParams.get('level')
|
||||
const page = parseInt(url.searchParams.get('page') || '1')
|
||||
const limit = parseInt(url.searchParams.get('limit') || '50')
|
||||
|
||||
const where: any = {}
|
||||
|
||||
if (status && status !== 'all') {
|
||||
where.status = status
|
||||
}
|
||||
|
||||
if (type && type !== 'all') {
|
||||
where.type = type
|
||||
}
|
||||
|
||||
if (level && level !== 'all') {
|
||||
where.level = parseInt(level)
|
||||
}
|
||||
|
||||
const commissions = await prisma.commission.findMany({
|
||||
where,
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true
|
||||
}
|
||||
},
|
||||
fromUser: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true
|
||||
}
|
||||
}
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc'
|
||||
},
|
||||
skip: (page - 1) * limit,
|
||||
take: limit
|
||||
})
|
||||
|
||||
const total = await prisma.commission.count({ where })
|
||||
|
||||
return NextResponse.json({
|
||||
commissions: commissions || [],
|
||||
pagination: {
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
pages: Math.ceil(total / limit)
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error fetching admin commissions:', error)
|
||||
return NextResponse.json({
|
||||
commissions: [],
|
||||
pagination: { page: 1, limit: 50, total: 0, pages: 0 }
|
||||
})
|
||||
}
|
||||
}
|
||||
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: [] })
|
||||
}
|
||||
}
|
||||
66
app/api/admin/commissions/stats/route.ts
Normal file
66
app/api/admin/commissions/stats/route.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
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 })
|
||||
}
|
||||
|
||||
// Get total commissions count
|
||||
const totalCommissions = await prisma.commission.count()
|
||||
|
||||
// Get amounts by status
|
||||
const [pendingResult, approvedResult, paidResult] = await Promise.all([
|
||||
prisma.commission.aggregate({
|
||||
where: { status: 'PENDING' },
|
||||
_sum: { amount: true }
|
||||
}),
|
||||
prisma.commission.aggregate({
|
||||
where: { status: 'APPROVED' },
|
||||
_sum: { amount: true }
|
||||
}),
|
||||
prisma.commission.aggregate({
|
||||
where: { status: 'PAID' },
|
||||
_sum: { amount: true }
|
||||
})
|
||||
])
|
||||
|
||||
// Get this month commissions
|
||||
const startOfMonth = new Date()
|
||||
startOfMonth.setDate(1)
|
||||
startOfMonth.setHours(0, 0, 0, 0)
|
||||
|
||||
const thisMonthCommissions = await prisma.commission.count({
|
||||
where: {
|
||||
createdAt: { gte: startOfMonth }
|
||||
}
|
||||
})
|
||||
|
||||
// Calculate average commission
|
||||
const totalAmount = (pendingResult._sum.amount || 0) + (approvedResult._sum.amount || 0) + (paidResult._sum.amount || 0)
|
||||
const averageCommission = totalCommissions > 0 ? totalAmount / totalCommissions : 0
|
||||
|
||||
return NextResponse.json({
|
||||
totalCommissions: totalCommissions || 0,
|
||||
pendingAmount: pendingResult._sum.amount || 0,
|
||||
approvedAmount: approvedResult._sum.amount || 0,
|
||||
paidAmount: paidResult._sum.amount || 0,
|
||||
thisMonthCommissions: thisMonthCommissions || 0,
|
||||
averageCommission: averageCommission || 0
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error fetching commission stats:', error)
|
||||
return NextResponse.json({
|
||||
totalCommissions: 0,
|
||||
pendingAmount: 0,
|
||||
approvedAmount: 0,
|
||||
paidAmount: 0,
|
||||
thisMonthCommissions: 0,
|
||||
averageCommission: 0
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user