Skip to content

50+ open-source generative AI apps you can clone, deploy, and monetize — image generators, video tools, virtual try-ons, AI SaaS templates, and platform integrations. One-click Vercel deploy on every template.

Restore: src/lib/services/user.js

Anil Chandra Naidu Matchac23c2b3507d1
diff --git a/src/lib/services/user.js b/src/lib/services/user.js
new file mode 100644
index 0000000..1ecfd4d
--- /dev/null
+++ b/src/lib/services/user.js
@@ -0,0 +1,42 @@
+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,
+ },
+ },
+ });
+ },
+};