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.
JavaScript ai-appsai-image-generatorai-saasai-toolsai-video-generatorawesomeawesome-listgenerative-aigenerative-ai-appsimage-generationnextjsnextjs-boilerplateopen-sourceopen-source-aisaas-boilerplatesaas-templateseedance2text-to-imagetext-to-videovideo-generation
Restore: src/lib/services/user.js
Anil Chandra Naidu Matchac23c2b3507d1
diff --git a/src/lib/services/user.js b/src/lib/services/user.jsnew file mode 100644index 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,+ },+ },+ });+ },+};