first commit

This commit is contained in:
2026-01-17 14:17:42 +05:30
commit 0f194eb9e7
328 changed files with 73544 additions and 0 deletions

View File

@@ -0,0 +1,69 @@
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) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
const url = new URL(request.url)
const page = parseInt(url.searchParams.get('page') || '1')
const limit = parseInt(url.searchParams.get('limit') || '50')
const status = url.searchParams.get('status')
const type = url.searchParams.get('type')
const level = url.searchParams.get('level')
const where: any = { userId: session.user.id }
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: {
fromUser: {
select: {
name: true,
email: true
}
}
},
orderBy: {
createdAt: 'desc'
},
skip: (page - 1) * limit,
take: limit
})
const total = await prisma.commission.count({ where })
return NextResponse.json({
commissions,
pagination: {
page,
limit,
total,
pages: Math.ceil(total / limit)
}
})
} catch (error) {
console.error('Error fetching commissions:', error)
return NextResponse.json(
{ error: 'Failed to fetch commissions' },
{ status: 500 }
)
}
}

View File

@@ -0,0 +1,95 @@
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) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
const userId = session.user.id
// Get total earnings
const totalEarningsResult = await prisma.commission.aggregate({
where: {
userId,
status: { in: ['APPROVED', 'PAID'] }
},
_sum: {
amount: true
}
})
// Get pending amount
const pendingAmountResult = await prisma.commission.aggregate({
where: {
userId,
status: 'PENDING'
},
_sum: {
amount: true
}
})
// Get this month earnings
const now = new Date()
const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1)
const thisMonthResult = await prisma.commission.aggregate({
where: {
userId,
status: { in: ['APPROVED', 'PAID'] },
createdAt: {
gte: startOfMonth
}
},
_sum: {
amount: true
}
})
// Get total commissions count
const totalCommissions = await prisma.commission.count({
where: { userId }
})
// Get earnings by level
const byLevel = await prisma.commission.groupBy({
by: ['level'],
where: {
userId,
status: { in: ['APPROVED', 'PAID'] }
},
_sum: {
amount: true
},
_count: {
id: true
},
orderBy: {
level: 'asc'
}
})
return NextResponse.json({
totalEarnings: totalEarningsResult._sum.amount || 0,
pendingAmount: pendingAmountResult._sum.amount || 0,
thisMonthEarnings: thisMonthResult._sum.amount || 0,
totalCommissions,
byLevel: byLevel.map(item => ({
level: item.level,
amount: item._sum.amount || 0,
count: item._count.id
}))
})
} catch (error) {
console.error('Error fetching commission stats:', error)
return NextResponse.json(
{ error: 'Failed to fetch commission stats' },
{ status: 500 }
)
}
}