diff --git a/prisma/schema.prisma b/prisma/schema.prisma index e877d02..93a2f6e 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -161,6 +161,8 @@ model Payment { createdAt DateTime @default(now()) @map("created_at") user User @relation(fields: [userId], references: [id], onDelete: Cascade) + plan Plan? @relation(fields: [planId], references: [id], onDelete: SetNull) + course Course? @relation(fields: [courseId], references: [id], onDelete: SetNull) @@map("payments") } @@ -174,6 +176,18 @@ model WebhookLog { @@map("webhook_logs") } +model Notification { + id String @id @default(uuid()) + userId String @map("user_id") // Can be 'ALL' for system wide + title String + message String @db.Text + type String @default("info") // 'info', 'success', 'warning', 'error' + read Boolean @default(false) + createdAt DateTime @default(now()) @map("created_at") + + @@map("notifications") +} + model ModuleActivity { id String @id @default(uuid()) moduleId String @unique @map("module_id") diff --git a/server.ts b/server.ts index 8f594e0..0c11300 100644 --- a/server.ts +++ b/server.ts @@ -508,7 +508,7 @@ app.post('/api/courses/:id/unlock', authenticateToken, checkoutRateLimiter, asyn pixData = await AsaasService.getPixQrCode(asaasPayment.id); } - const newPayment = await prisma.subscriptionPayment.create({ + const newPayment = await prisma.payment.create({ data: { id: asaasPayment.id, userId: userId, @@ -628,7 +628,7 @@ app.get('/api/subscription/status', authenticateToken, async (req: Authenticated return; } - const payments = await prisma.subscriptionPayment.findMany({ + const payments = await prisma.payment.findMany({ where: { userId }, orderBy: { createdAt: 'desc' } }); @@ -731,7 +731,7 @@ app.post('/api/subscription/subscribe', authenticateToken, async (req: Authentic asaasDueDate = payment.dueDate || asaasDueDate; } - const newPayment = await prisma.subscriptionPayment.create({ + const newPayment = await prisma.payment.create({ data: { id: asaasPaymentId, userId: userId, @@ -791,7 +791,7 @@ app.get('/api/admin/dashboard', authenticateToken, requireAdmin, async (req: Aut const activeUsersIds = users.filter(u => u.subscriptionStatus === 'ACTIVE').map(u => u.id); - const allPayments = await prisma.subscriptionPayment.findMany({ + const allPayments = await prisma.payment.findMany({ include: { user: true, course: true, @@ -883,7 +883,7 @@ app.get('/api/admin/dashboard', authenticateToken, requireAdmin, async (req: Aut }; }); - const webhookLogs = await prisma.notification.findMany({ + const webhookLogs = await prisma.webhookLog.findMany({ orderBy: { createdAt: 'desc' }, take: 15 }); @@ -1416,11 +1416,11 @@ app.post('/api/webhooks/asaas', async (req: Request, res: Response) => { isRefunded ? 'REFUNDED' : event === 'PAYMENT_RESTORED' ? 'PENDING' : null; - let existingPayment = await prisma.subscriptionPayment.findUnique({ where: { id: payment.id } }); + let existingPayment = await prisma.payment.findUnique({ where: { id: payment.id } }); if (existingPayment) { if (newStatus) { - await prisma.subscriptionPayment.update({ + await prisma.payment.update({ where: { id: payment.id }, data: { status: newStatus as any, @@ -1429,12 +1429,12 @@ app.post('/api/webhooks/asaas', async (req: Request, res: Response) => { }); } } else if (!isRefunded) { - const lastPayment = await prisma.subscriptionPayment.findFirst({ + const lastPayment = await prisma.payment.findFirst({ where: { userId: user.id }, orderBy: { createdAt: 'desc' } }); - await prisma.subscriptionPayment.create({ + await prisma.payment.create({ data: { id: payment.id, userId: user.id,