import { prisma } from './prisma'; export class UserAdminService { static async softDeleteUser(organizationId: string, userId: string): Promise { const result = await prisma.user.updateMany({ where: { id: userId, organizationId, deletedAt: null }, data: { deletedAt: new Date() }, }); return result.count > 0; } static async softDeleteEnrollment(organizationId: string, enrollmentId: string): Promise { const result = await prisma.enrollment.updateMany({ where: { id: enrollmentId, organizationId, deletedAt: null }, data: { deletedAt: new Date() }, }); return result.count > 0; } static async listUsers(organizationId: string, page: number, limit: number) { const [users, total] = await Promise.all([ prisma.user.findMany({ where: { organizationId, deletedAt: null }, orderBy: { createdAt: 'desc' }, skip: (page - 1) * limit, take: limit, include: { enrollments: { include: { track: true }, orderBy: { startedAt: 'desc' }, take: 1 }, _count: { select: { enrollments: true, responses: true } } } }), prisma.user.count({ where: { organizationId, deletedAt: null } }) ]); return { users, total, page, limit }; } static async getUserMessages(organizationId: string, userId: string) { const [messages, user] = await Promise.all([ prisma.message.findMany({ where: { userId, organizationId }, orderBy: { createdAt: 'asc' }, }), prisma.user.findFirst({ where: { id: userId, organizationId }, select: { id: true, name: true, phone: true } }) ]); if (!user) return null; return { user, messages }; } }