mirror of
https://github.com/EdiFarcas/Giveaway-app.git
synced 2026-06-22 07:00:57 +03:00
60 lines
1.3 KiB
TypeScript
60 lines
1.3 KiB
TypeScript
"use server"
|
|
import { db } from "@/lib/db";
|
|
|
|
export async function handleEnterGiveawayClick(giveawayId: string, entryValue: number, activeUserCoins: number, userId: string) {
|
|
|
|
await db.entry.create({
|
|
data: {
|
|
userId: userId,
|
|
giveawayId: giveawayId,
|
|
pastValue: activeUserCoins,
|
|
},
|
|
});
|
|
|
|
await db.user.update({
|
|
where: { id: userId },
|
|
data: {
|
|
coins: { decrement: entryValue },
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function entrycount(userId: string, giveawayId: string) {
|
|
const entryCount = db.entry.count({
|
|
where: {
|
|
userId: userId,
|
|
giveawayId: giveawayId,
|
|
},
|
|
});
|
|
return entryCount;
|
|
}
|
|
|
|
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;
|
|
} |