mirror of
https://github.com/EdiFarcas/Giveaway-app.git
synced 2026-06-28 19:00:44 +03:00
Entry Update 2
This commit is contained in:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user