Skip to content

Commit

Permalink
fix(Events): don't run database query if not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
simonknittel committed Feb 1, 2025
1 parent 4d71446 commit f6c2baf
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions app/src/app/api/scrape-discord-events/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,34 +113,33 @@ const updateParticipants = async (

// Collect new participants
for (const userId of discordEventUserIds) {
if (!existingDatabaseParticipantIds.includes(userId)) {
participants.create.push(userId);
}
if (existingDatabaseParticipantIds.includes(userId)) continue;
participants.create.push(userId);
}

// Collect removed participants
for (const userId of existingDatabaseParticipantIds) {
if (!discordEventUserIds.includes(userId)) {
participants.delete.push(userId);
}
if (discordEventUserIds.includes(userId)) continue;
participants.delete.push(userId);
}

// Save to database
await prisma.$transaction([
prisma.discordEventParticipant.deleteMany({
if (participants.delete.length > 0) {
await prisma.discordEventParticipant.deleteMany({
where: {
eventId: discordEvent.id,
discordUserId: {
in: participants.delete,
},
},
}),

prisma.discordEventParticipant.createMany({
});
}
if (participants.create.length > 0) {
await prisma.discordEventParticipant.createMany({
data: participants.create.map((participantId) => ({
eventId: databaseEvent.id,
discordUserId: participantId,
})),
}),
]);
});
}
};

0 comments on commit f6c2baf

Please sign in to comment.