first commit
This commit is contained in:
66
app/api/admin/payouts/[id]/route.ts
Normal file
66
app/api/admin/payouts/[id]/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 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: payoutId } = await params
|
||||
const { status, adminNotes } = await request.json()
|
||||
|
||||
// Validate status
|
||||
if (!['APPROVED', 'REJECTED', 'PAID'].includes(status)) {
|
||||
return NextResponse.json({ error: 'Invalid status' }, { status: 400 })
|
||||
}
|
||||
|
||||
const result = await prisma.$transaction(async (tx) => {
|
||||
// Get the payout
|
||||
const payout = await tx.payout.findUnique({
|
||||
where: { id: payoutId },
|
||||
include: { user: true }
|
||||
})
|
||||
|
||||
if (!payout) {
|
||||
throw new Error('Payout not found')
|
||||
}
|
||||
|
||||
// Update payout
|
||||
const updatedPayout = await tx.payout.update({
|
||||
where: { id: payoutId },
|
||||
data: {
|
||||
status,
|
||||
adminNotes,
|
||||
updatedAt: new Date()
|
||||
}
|
||||
})
|
||||
|
||||
// If rejecting, restore the amount to user's wallet
|
||||
if (status === 'REJECTED') {
|
||||
await tx.wallet.update({
|
||||
where: { userId: payout.userId },
|
||||
data: {
|
||||
balance: { increment: payout.amount }
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return updatedPayout
|
||||
})
|
||||
|
||||
return NextResponse.json(result)
|
||||
} catch (error) {
|
||||
console.error('Error updating payout:', error)
|
||||
return NextResponse.json(
|
||||
{ error: error instanceof Error ? error.message : 'Failed to update payout' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
59
app/api/admin/payouts/route.ts
Normal file
59
app/api/admin/payouts/route.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
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 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
|
||||
}
|
||||
|
||||
const payouts = await prisma.payout.findMany({
|
||||
where,
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true
|
||||
}
|
||||
}
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc'
|
||||
},
|
||||
skip: (page - 1) * limit,
|
||||
take: limit
|
||||
})
|
||||
|
||||
const total = await prisma.payout.count({ where })
|
||||
|
||||
return NextResponse.json({
|
||||
payouts,
|
||||
pagination: {
|
||||
page,
|
||||
limit,
|
||||
total,
|
||||
pages: Math.ceil(total / limit)
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error fetching admin payouts:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch payouts' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
45
app/api/admin/payouts/stats/route.ts
Normal file
45
app/api/admin/payouts/stats/route.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
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 requests
|
||||
const totalRequests = await prisma.payout.count()
|
||||
|
||||
// Get amounts by status
|
||||
const [pendingResult, approvedResult, paidResult] = await Promise.all([
|
||||
prisma.payout.aggregate({
|
||||
where: { status: 'PENDING' },
|
||||
_sum: { amount: true }
|
||||
}),
|
||||
prisma.payout.aggregate({
|
||||
where: { status: 'APPROVED' },
|
||||
_sum: { amount: true }
|
||||
}),
|
||||
prisma.payout.aggregate({
|
||||
where: { status: 'PAID' },
|
||||
_sum: { amount: true }
|
||||
})
|
||||
])
|
||||
|
||||
return NextResponse.json({
|
||||
totalRequests,
|
||||
pendingAmount: pendingResult._sum.amount || 0,
|
||||
approvedAmount: approvedResult._sum.amount || 0,
|
||||
paidAmount: paidResult._sum.amount || 0
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error fetching payout stats:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch payout stats' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user