import { http, HttpResponse, delay } from 'msw'; import { mockDepts } from '../data'; import type { MockDept } from '../data'; // Mutable in-memory store let depts = [...mockDepts]; let nextId = Math.max(...depts.map((d) => d.id)) + 1; export const deptHandlers = [ // List (returns all depts as flat array) http.get('/admin-api/system/dept/list', async ({ request }) => { await delay(300); const url = new URL(request.url); const name = url.searchParams.get('name') || ''; const status = url.searchParams.get('status'); let filtered = [...depts]; if (name) { filtered = filtered.filter((d) => d.name.includes(name)); } if (status !== null && status !== '') { filtered = filtered.filter((d) => d.status === Number(status)); } return HttpResponse.json({ code: 0, data: filtered, message: 'ok', }); }), // Simple list (id + name only) http.get('/admin-api/system/dept/simple-list', async () => { await delay(200); const simpleDepts = depts.map((d) => ({ id: d.id, name: d.name, parentId: d.parentId, })); return HttpResponse.json({ code: 0, data: simpleDepts, message: 'ok', }); }), // Get by ID http.get('/admin-api/system/dept/get', async ({ request }) => { await delay(200); const url = new URL(request.url); const id = Number(url.searchParams.get('id')); const dept = depts.find((d) => d.id === id); if (!dept) { return HttpResponse.json( { code: 1, data: null, message: '部门不存在' }, ); } return HttpResponse.json({ code: 0, data: dept, message: 'ok', }); }), // Create http.post('/admin-api/system/dept/create', async ({ request }) => { await delay(300); const body = (await request.json()) as Record; const newDept: MockDept = { id: nextId++, name: body.name as string, parentId: (body.parentId as number) ?? 0, status: (body.status as number) ?? 0, sort: (body.sort as number) ?? 0, leaderUserId: (body.leaderUserId as number) ?? 0, phone: (body.phone as string) || '', email: (body.email as string) || '', createTime: new Date().toISOString().replace('T', ' ').slice(0, 19), }; depts.push(newDept); return HttpResponse.json({ code: 0, data: newDept.id, message: 'ok' }); }), // Update http.put('/admin-api/system/dept/update', async ({ request }) => { await delay(300); const body = (await request.json()) as Record; const index = depts.findIndex((d) => d.id === body.id); if (index === -1) { return HttpResponse.json( { code: 1, data: null, message: '部门不存在' }, ); } // Prevent setting parent to self or descendant if (body.parentId !== undefined) { const parentId = body.parentId as number; if (parentId === body.id) { return HttpResponse.json( { code: 1, data: null, message: '上级部门不能是自己' }, ); } // Check if the parent is a descendant let current = parentId; while (current !== 0) { const parent = depts.find((d) => d.id === current); if (!parent) break; if (parent.parentId === body.id) { return HttpResponse.json( { code: 1, data: null, message: '上级部门不能是自己的子部门' }, ); } current = parent.parentId; } } depts[index] = { ...depts[index], ...(body.name !== undefined && { name: body.name as string }), ...(body.parentId !== undefined && { parentId: body.parentId as number }), ...(body.status !== undefined && { status: body.status as number }), ...(body.sort !== undefined && { sort: body.sort as number }), ...(body.leaderUserId !== undefined && { leaderUserId: body.leaderUserId as number }), ...(body.phone !== undefined && { phone: body.phone as string }), ...(body.email !== undefined && { email: body.email as string }), }; return HttpResponse.json({ code: 0, data: true, message: 'ok' }); }), // Delete http.delete('/admin-api/system/dept/delete', async ({ request }) => { await delay(200); const url = new URL(request.url); const id = Number(url.searchParams.get('id')); // Check if dept has children const hasChildren = depts.some((d) => d.parentId === id); if (hasChildren) { return HttpResponse.json( { code: 1, data: null, message: '存在子部门,不允许删除' }, ); } depts = depts.filter((d) => d.id !== id); return HttpResponse.json({ code: 0, data: true, message: 'ok' }); }), ];