Skip to content

Commit

Permalink
chore: updated lucky winner route
Browse files Browse the repository at this point in the history
  • Loading branch information
hemantwasthere committed Oct 28, 2024
1 parent 1a526da commit 050619b
Showing 1 changed file with 71 additions and 41 deletions.
112 changes: 71 additions & 41 deletions src/app/api/raffle/luckyWinner/route.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,107 @@
import { NextResponse } from 'next/server';

import { db } from '@/db';
import { NextResponse } from 'next/server';

export const dynamic = 'force-dynamic'; // static by default, unless reading the request

export async function GET() {
try {
const raffleParticipantsCount = await db.raffle.count({
// Fetch all raffle participants
const raffleParticipants = await db.raffle.findMany({
where: {
isRaffleParticipant: true,
sharedOnX: true,
activeDeposits: true,
OR: [
{ isRaffleParticipant: true },
{ sharedOnX: true },
{ activeDeposits: true },
],
},
});

if (raffleParticipantsCount === 0) {
if (raffleParticipants.length === 0) {
return NextResponse.json({
success: false,
message: 'No raffle participants found',
});
}

let randomRaffleParticipant;
// Group participants by ticket count
const threeTicketParticipants: any = [];
const twoTicketParticipants: any = [];
const oneTicketParticipants: any = [];

raffleParticipants.forEach((participant) => {
let ticketCount = 0;

if (participant.isRaffleParticipant) ticketCount += 1;
if (participant.sharedOnX) ticketCount += 1;
if (participant.activeDeposits) ticketCount += 1;

// Add the participant to the corresponding group based on their ticket count
if (ticketCount === 3) {
threeTicketParticipants.push(participant);
} else if (ticketCount === 2) {
twoTicketParticipants.push(participant);
} else if (ticketCount === 1) {
oneTicketParticipants.push(participant);
}
});

let selectedParticipant;
let foundValidParticipant = false;

// Keep searching until a valid participant is found
while (!foundValidParticipant) {
// Select a random raffle participant
randomRaffleParticipant = await db.raffle.findFirst({
where: {
isRaffleParticipant: true,
sharedOnX: true,
activeDeposits: true,
},
orderBy: {
createdAt: 'asc',
},
take: 1,
skip: Math.floor(Math.random() * raffleParticipantsCount),
});
// Attempt to select a valid participant, prioritizing higher ticket groups
const groups = [
threeTicketParticipants,
twoTicketParticipants,
oneTicketParticipants,
];

if (!randomRaffleParticipant) {
return NextResponse.json({
success: false,
message: 'No raffle participants found',
});
for (const group of groups) {
if (group.length === 0) {
continue; // Move to the next group if the current one is empty
}

// Check if the selected participant is already a lucky winner
const existingWinner = await db.luckyWinner.findFirst({
where: {
raffleId: randomRaffleParticipant.raffleId,
},
});
// Keep searching within the current group until a valid participant is found
while (!foundValidParticipant && group.length > 0) {
// Randomly select a participant from the current group
const randomIndex = Math.floor(Math.random() * group.length);
selectedParticipant = group[randomIndex];

// Check if the selected participant is already a lucky winner
const existingWinner = await db.luckyWinner.findFirst({
where: {
raffleId: selectedParticipant.raffleId,
},
});

// If not already a winner, break the loop
if (!existingWinner) {
foundValidParticipant = true;
break;
} else {
// If the selected participant is already a winner, remove them from the group
group.splice(randomIndex, 1);
}
}

// If not already a winner, break the loop
if (!existingWinner) {
foundValidParticipant = true;
// If a valid participant has been found, exit the loop
if (foundValidParticipant) {
break;
}
}

if (!randomRaffleParticipant) {
// If no valid participant was found after checking all groups
if (!foundValidParticipant) {
return NextResponse.json({
success: false,
message: 'No raffle participants found',
message: 'No eligible raffle participants found',
});
}

// Add the selected user to the LuckyWinner table
const newLuckyWinner = await db.luckyWinner.create({
data: {
userId: randomRaffleParticipant.userId,
raffleId: randomRaffleParticipant.raffleId,
userId: selectedParticipant.userId,
raffleId: selectedParticipant.raffleId,
},
});

Expand Down

0 comments on commit 050619b

Please sign in to comment.