Spaces:
Paused
Paused
Download src/lib/credits.ts from veltrixcode/retest: direct link, hf CLI and curl.
- Browser
- Download file 1.56 kB
-
https://huggingface.co/spaces/veltrixcode/retest/resolve/main/src/lib/credits.ts
- Command line
-
hf download hf://spaces/veltrixcode/retest/src/lib/credits.ts
-
curl -L -o credits.ts https://huggingface.co/spaces/veltrixcode/retest/resolve/main/src/lib/credits.ts
1.56 kB
| import { prisma } from "@/lib/prisma" | |
| export async function checkAndResetDailyCredits(userId: string) { | |
| const user = await prisma.user.findUnique({ | |
| where: { id: userId }, | |
| select: { credits: true, lastDailyCredit: true }, | |
| }) | |
| if (!user) return null | |
| const now = new Date() | |
| const lastReset = user.lastDailyCredit ? new Date(user.lastDailyCredit) : new Date(0) | |
| // Check if it's a different day (simple 24h check) | |
| const oneDay = 24 * 60 * 60 * 1000 | |
| // Or strictly check if "today" is different from "last reset day" | |
| // But 24h window is safer for "daily". | |
| // User prompt: "free 50 credits daily". | |
| // Approaches: | |
| // 1. Reset at midnight UTC. | |
| // 2. Reset 24h after last reset. | |
| // I'll go with 24h after last reset for simplicity of logic here, or reset if (now - last > 24h). | |
| if (now.getTime() - lastReset.getTime() > oneDay) { | |
| const updatedUser = await prisma.user.update({ | |
| where: { id: userId }, | |
| data: { | |
| credits: 100, | |
| lastDailyCredit: now, | |
| }, | |
| }) | |
| return updatedUser.credits | |
| } | |
| return user.credits | |
| } | |
| export async function deductCredits(userId: string, amount: number) { | |
| const currentCredits = await checkAndResetDailyCredits(userId) | |
| if (currentCredits === null || currentCredits < amount) { | |
| return false | |
| } | |
| await prisma.user.update({ | |
| where: { id: userId }, | |
| data: { | |
| credits: { decrement: amount } | |
| } | |
| }) | |
| return true | |
| } | |