/** * 岗位字典路由 * 提供岗位的增删改查 */ import { Router } from 'express' import { authMiddleware, AuthRequest } from '../middleware/auth' import prisma from '../lib/prisma' import { z } from 'zod' const router = Router() const createPositionSchema = z.object({ name: z.string().min(1, '岗位名称必填'), departmentId: z.string().nullable().optional(), headcount: z.number().int().min(0).default(0), level: z.string().max(20).optional(), description: z.string().max(200).optional(), }) /** 获取岗位列表 */ router.get('/', authMiddleware, async (req: AuthRequest, res, next) => { try { const positions = await prisma.position.findMany({ where: { orgId: req.user!.orgId! }, orderBy: { createdAt: 'asc' }, include: { department: { select: { id: true, name: true } } }, }) res.json({ success: true, data: positions }) } catch (err) { next(err) } }) /** 创建岗位 */ router.post('/', authMiddleware, async (req: AuthRequest, res, next) => { try { const data = createPositionSchema.parse(req.body) const position = await prisma.position.create({ data: { ...data, orgId: req.user!.orgId!, createdBy: req.user!.id, }, }) res.json({ success: true, data: position }) } catch (err) { next(err) } }) /** 更新岗位 */ router.put('/:id', authMiddleware, async (req: AuthRequest, res, next) => { try { const { id } = req.params const data = createPositionSchema.partial().parse(req.body) const position = await prisma.position.update({ where: { id }, data }) res.json({ success: true, data: position }) } catch (err) { next(err) } }) /** 删除岗位 */ router.delete('/:id', authMiddleware, async (req: AuthRequest, res, next) => { try { const { id } = req.params await prisma.position.delete({ where: { id } }) res.json({ success: true }) } catch (err) { next(err) } }) export default router