import { Router, Request, Response, NextFunction } from 'express'; import { z } from 'zod'; import { WarmupStatus } from '../lib/prisma'; import type { WarmupLogWithRelations } from '../models/types'; import { prisma } from '../lib/prisma'; import { stripPassword } from '../lib/inboxGuard'; const router = Router(); const querySchema = z.object({ status: z.nativeEnum(WarmupStatus).optional(), inboxId: z.string().optional(), page: z.coerce.number().int().positive().default(1), limit: z.coerce.number().int().positive().max(100).default(20), }); router.get('/', async (req: Request, res: Response, next: NextFunction) => { try { const { status, inboxId, page, limit } = querySchema.parse(req.query); const skip = (page - 1) * limit; const where = { ...(status ? { status } : {}), ...(inboxId ? { OR: [{ senderId: inboxId }, { receiverId: inboxId }] } : {}), }; const [logs, total] = await Promise.all([ prisma.warmupLog.findMany({ where, include: { sender: true, receiver: true }, orderBy: { createdAt: 'desc' }, skip, take: limit, }), prisma.warmupLog.count({ where }), ]); res.json({ data: logs.map((log: WarmupLogWithRelations) => ({ ...log, sender: stripPassword(log.sender), receiver: stripPassword(log.receiver), })), pagination: { page, limit, total, totalPages: Math.ceil(total / limit), }, }); } catch (err) { next(err); } }); export default router;