import { Router, Response, NextFunction } from 'express' import prisma from '../lib/prisma' import { authMiddleware, AuthRequest } from '../middleware/auth' import { getDashboardData } from '../services/risk.service' import { z } from 'zod' const router = Router() router.get('/', authMiddleware, async (req: AuthRequest, res, next) => { try { const data = await getDashboardData(req.user!.orgId) res.json({ success: true, data }) } catch (err) { next(err) } }) // 标记待办为已完成 router.patch('/todos/:id/resolve', authMiddleware, async (req: AuthRequest, res: Response, next: NextFunction) => { try { const item = await prisma.riskItem.updateMany({ where: { id: req.params.id, orgId: req.user!.orgId, status: 'PENDING' }, data: { status: 'RESOLVED', resolvedAt: new Date(), resolvedBy: req.user!.id }, }) if (item.count === 0) { return res.status(404).json({ success: false, error: { code: 'NOT_FOUND', message: '待办不存在或已处理' } }) } res.json({ success: true }) } catch (err) { next(err) } }) // 忽略待办 router.patch('/todos/:id/ignore', authMiddleware, async (req: AuthRequest, res: Response, next: NextFunction) => { try { const item = await prisma.riskItem.updateMany({ where: { id: req.params.id, orgId: req.user!.orgId, status: 'PENDING' }, data: { status: 'IGNORED', resolvedAt: new Date(), resolvedBy: req.user!.id }, }) if (item.count === 0) { return res.status(404).json({ success: false, error: { code: 'NOT_FOUND', message: '待办不存在或已处理' } }) } res.json({ success: true }) } catch (err) { next(err) } }) // 批量标记待办为已完成 router.patch('/todos/batch-resolve', authMiddleware, async (req: AuthRequest, res: Response, next: NextFunction) => { try { const schema = z.object({ ids: z.array(z.string()) }) const { ids } = schema.parse(req.body) const result = await prisma.riskItem.updateMany({ where: { id: { in: ids }, orgId: req.user!.orgId, status: 'PENDING' }, data: { status: 'RESOLVED', resolvedAt: new Date(), resolvedBy: req.user!.id }, }) res.json({ success: true, data: { count: result.count } }) } catch (err) { next(err) } }) // 批量忽略待办 router.patch('/todos/batch-ignore', authMiddleware, async (req: AuthRequest, res: Response, next: NextFunction) => { try { const schema = z.object({ ids: z.array(z.string()) }) const { ids } = schema.parse(req.body) const result = await prisma.riskItem.updateMany({ where: { id: { in: ids }, orgId: req.user!.orgId, status: 'PENDING' }, data: { status: 'IGNORED', resolvedAt: new Date(), resolvedBy: req.user!.id }, }) res.json({ success: true, data: { count: result.count } }) } catch (err) { next(err) } }) export default router