Entry Update 2

This commit is contained in:
EdiFarcas
2025-05-05 21:42:29 +03:00
parent bcc11ddbc1
commit ad25daea04
6 changed files with 95 additions and 29 deletions
+41 -3
View File
@@ -1,13 +1,13 @@
"use server"
import { db } from "@/lib/db";
async function handleEnterGiveawayClick(giveawayId: string, entryValue: number, userId: string) {
export async function handleEnterGiveawayClick(giveawayId: string, entryValue: number, activeUserCoins: number, userId: string) {
await db.entry.create({
data: {
userId: userId,
giveawayId: giveawayId,
weight: 1,
pastValue: activeUserCoins,
},
});
@@ -17,6 +17,44 @@ async function handleEnterGiveawayClick(giveawayId: string, entryValue: number,
coins: { decrement: entryValue },
},
});
}
export async function entrycount(userId: string, giveawayId: string) {
const entryCount = db.entry.count({
where: {
userId: userId,
giveawayId: giveawayId,
},
});
return entryCount;
}
export default handleEnterGiveawayClick;
export async function usercoins(userId: string, giveawayId: string) {
const user = await db.user.findUnique({
where: { id: userId },
select: { coins: true },
});
const entry = await db.entry.findFirst({
where: {
userId: userId,
giveawayId: giveawayId,
},
orderBy: {
pastValue: 'desc',
},
select: { pastValue: true },
});
if (!user) {
throw new Error("User not found");
}
if (entry) {
if ((entry?.pastValue ?? 0) > user.coins) {
return entry.pastValue;
}
}
return user.coins;
}