|
| 1 | +'use server'; |
| 2 | +import prisma from '@/db'; |
| 3 | +import { getServerSession } from 'next-auth'; |
| 4 | +import { authOptions } from '@/lib/auth'; |
| 5 | +import { ROLES } from '../types'; |
| 6 | + |
| 7 | +export type Bounty = { |
| 8 | + id: string; |
| 9 | + prLink: string; |
| 10 | + paymentMethod: string; |
| 11 | + status: string; |
| 12 | + createdAt: Date; |
| 13 | + updatedAt: Date; |
| 14 | + amount: number; |
| 15 | + userId: string; |
| 16 | + user: { |
| 17 | + id: string; |
| 18 | + name: string | null; |
| 19 | + }; |
| 20 | +}; |
| 21 | + |
| 22 | +type BountyResponse = { |
| 23 | + bounties?: Bounty[]; |
| 24 | + totalINRBounties?: number; |
| 25 | + totalSOLBounties?: number; |
| 26 | + error?: string; |
| 27 | +}; |
| 28 | + |
| 29 | +export async function getBounties(): Promise<BountyResponse> { |
| 30 | + const session = await getServerSession(authOptions); |
| 31 | + |
| 32 | + if (!session || !session.user || session.user.role !== ROLES.ADMIN) { |
| 33 | + return { error: 'Unauthorized or insufficient permissions' }; |
| 34 | + } |
| 35 | + |
| 36 | + try { |
| 37 | + const bounties = await prisma.bountySubmission.findMany({ |
| 38 | + select: { |
| 39 | + id: true, |
| 40 | + prLink: true, |
| 41 | + paymentMethod: true, |
| 42 | + status: true, |
| 43 | + createdAt: true, |
| 44 | + updatedAt: true, |
| 45 | + amount: true, |
| 46 | + userId: true, |
| 47 | + user: { |
| 48 | + select: { |
| 49 | + id: true, |
| 50 | + name: true, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + let totalINRBounties = 0; |
| 57 | + let totalSOLBounties = 0; |
| 58 | + |
| 59 | + bounties.forEach((bounty) => { |
| 60 | + if (bounty.paymentMethod.includes('@')) { |
| 61 | + totalINRBounties += bounty.amount || 0; |
| 62 | + } else { |
| 63 | + totalSOLBounties += bounty.amount || 0; |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + return { bounties, totalINRBounties, totalSOLBounties }; |
| 68 | + } catch (e) { |
| 69 | + return { error: 'An error occurred while approving the bounty.' }; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +export async function deleteBounty(bountyId: string) { |
| 74 | + const session = await getServerSession(authOptions); |
| 75 | + |
| 76 | + if (!session || !session.user || session.user.role !== ROLES.ADMIN) { |
| 77 | + return { error: 'Unauthorized or insufficient permissions' }; |
| 78 | + } |
| 79 | + try { |
| 80 | + const deleteBounty = await prisma.bountySubmission.delete({ |
| 81 | + where: { id: bountyId }, |
| 82 | + }); |
| 83 | + return { success: !!deleteBounty }; |
| 84 | + } catch (e) { |
| 85 | + return { error: 'An error occurred while approving the bounty.' }; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +export async function confirmBounty(bountyId: string, amount: number) { |
| 90 | + const session = await getServerSession(authOptions); |
| 91 | + |
| 92 | + if (!session || !session.user || session.user.role !== ROLES.ADMIN) { |
| 93 | + return { error: 'Unauthorized or insufficient permissions' }; |
| 94 | + } |
| 95 | + |
| 96 | + try { |
| 97 | + const updatedBounty = await prisma.bountySubmission.update({ |
| 98 | + where: { id: bountyId }, |
| 99 | + data: { status: 'confirmed', amount }, |
| 100 | + }); |
| 101 | + |
| 102 | + if (updatedBounty) { |
| 103 | + return { success: true }; |
| 104 | + } |
| 105 | + return { error: 'Failed to update bounty.' }; |
| 106 | + } catch (e) { |
| 107 | + return { error: 'An error occurred while approving the bounty.' }; |
| 108 | + } |
| 109 | +} |
0 commit comments