Download src/lib/services/user.js from NeoPy/hugging-c-ai: direct link, hf CLI and curl.
- Browser
- Download file 924 Bytes
-
https://huggingface.co/NeoPy/hugging-c-ai/resolve/main/src/lib/services/user.js
- Command line
-
hf download hf://NeoPy/hugging-c-ai/src/lib/services/user.js
-
curl -L -o user.js https://huggingface.co/NeoPy/hugging-c-ai/resolve/main/src/lib/services/user.js
924 Bytes
| import { prisma } from "../prisma"; | |
| export const UserService = { | |
| async getCredits(userId) { | |
| const user = await prisma.user.findUnique({ | |
| where: { id: userId }, | |
| select: { credits: true }, | |
| }); | |
| return user ? user.credits : 0; | |
| }, | |
| async addCredits(userId, amount) { | |
| if (amount <= 0) return; | |
| return await prisma.user.update({ | |
| where: { id: userId }, | |
| data: { | |
| credits: { | |
| increment: amount, | |
| }, | |
| }, | |
| }); | |
| }, | |
| async deductCredits(userId, amount) { | |
| if (amount <= 0) return; | |
| // Check if the user has enough credits | |
| const currentCredits = await this.getCredits(userId); | |
| if (currentCredits < amount) { | |
| throw new Error("Insufficient credits available"); | |
| } | |
| return await prisma.user.update({ | |
| where: { id: userId }, | |
| data: { | |
| credits: { | |
| decrement: amount, | |
| }, | |
| }, | |
| }); | |
| }, | |
| }; | |