import { Injectable } from '@nestjs/common'; import { PrismaService } from '../prisma/prisma.service'; @Injectable() export class AdminUsersService { constructor(private readonly prisma: PrismaService) {} async findAll(search?: string) { return this.prisma.user.findMany({ where: search ? { OR: [ { email: { contains: search, mode: 'insensitive' } }, { name: { contains: search, mode: 'insensitive' } }, ], } : undefined, select: { id: true, email: true, name: true, role: true, emailVerified: true, createdAt: true, lastLoginAt: true, suspendedAt: true, _count: { select: { wallets: true, transactions: true, }, }, }, orderBy: { createdAt: 'desc' }, }); } async findOne(id: string) { return this.prisma.user.findUnique({ where: { id }, include: { subscriptions: { include: { plan: true, }, }, _count: { select: { wallets: true, transactions: true, payments: true, }, }, }, }); } async updateRole(id: string, role: string) { return this.prisma.user.update({ where: { id }, data: { role }, }); } async suspend(id: string, reason: string) { return this.prisma.user.update({ where: { id }, data: { suspendedAt: new Date(), suspendedReason: reason, }, }); } async unsuspend(id: string) { return this.prisma.user.update({ where: { id }, data: { suspendedAt: null, suspendedReason: null, }, }); } async grantProAccess(userId: string, planSlug: string, durationDays: number) { const plan = await this.prisma.plan.findUnique({ where: { slug: planSlug }, }); if (!plan) { throw new Error('Plan not found'); } const now = new Date(); const endDate = new Date(now); endDate.setDate(endDate.getDate() + durationDays); // Check if user already has a subscription const existing = await this.prisma.subscription.findUnique({ where: { userId }, }); if (existing) { // Update existing subscription return this.prisma.subscription.update({ where: { userId }, data: { planId: plan.id, status: 'active', startDate: now, endDate, }, }); } else { // Create new subscription return this.prisma.subscription.create({ data: { userId, planId: plan.id, status: 'active', startDate: now, endDate, }, }); } } async getStats() { const totalUsers = await this.prisma.user.count(); const activeSubscriptions = await this.prisma.subscription.count({ where: { status: 'active' }, }); const suspendedUsers = await this.prisma.user.count({ where: { suspendedAt: { not: null } }, }); return { totalUsers, activeSubscriptions, suspendedUsers, }; } }