10.3 사용자 검색/정지/권한 변경 o
This commit is contained in:
26
src/app/api/admin/users/[id]/roles/route.ts
Normal file
26
src/app/api/admin/users/[id]/roles/route.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export async function PATCH(req: Request, context: { params: Promise<{ id: string }> }) {
|
||||
const { id } = await context.params;
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const roles = Array.isArray(body?.roles) ? (body.roles as string[]) : [];
|
||||
const all = await prisma.role.findMany({ select: { roleId: true, name: true } });
|
||||
const wanted = new Set(roles);
|
||||
// 현재 매핑
|
||||
const existing = await prisma.userRole.findMany({ where: { userId: id } });
|
||||
const toKeep = new Set(
|
||||
existing.filter((ur) => all.find((r) => r.roleId === ur.roleId && wanted.has(r.name))).map((ur) => ur.roleId)
|
||||
);
|
||||
// 삭제
|
||||
await prisma.userRole.deleteMany({ where: { userId: id, roleId: { notIn: Array.from(toKeep) } } });
|
||||
// 추가
|
||||
for (const r of all) {
|
||||
if (wanted.has(r.name) && !toKeep.has(r.roleId)) {
|
||||
await prisma.userRole.create({ data: { userId: id, roleId: r.roleId } });
|
||||
}
|
||||
}
|
||||
return NextResponse.json({ ok: true });
|
||||
}
|
||||
|
||||
|
||||
13
src/app/api/admin/users/[id]/status/route.ts
Normal file
13
src/app/api/admin/users/[id]/status/route.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export async function PATCH(req: Request, context: { params: Promise<{ id: string }> }) {
|
||||
const { id } = await context.params;
|
||||
const body = await req.json().catch(() => ({}));
|
||||
const status = body?.status as "active" | "suspended" | "withdrawn" | undefined;
|
||||
if (!status) return NextResponse.json({ error: "invalid status" }, { status: 400 });
|
||||
const user = await prisma.user.update({ where: { userId: id }, data: { status } });
|
||||
return NextResponse.json({ user });
|
||||
}
|
||||
|
||||
|
||||
37
src/app/api/admin/users/route.ts
Normal file
37
src/app/api/admin/users/route.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
export async function GET(req: Request) {
|
||||
const { searchParams } = new URL(req.url);
|
||||
const q = searchParams.get("q") || "";
|
||||
const users = await prisma.user.findMany({
|
||||
where: q
|
||||
? {
|
||||
OR: [
|
||||
{ nickname: { contains: q } },
|
||||
{ phone: { contains: q } },
|
||||
{ name: { contains: q } },
|
||||
],
|
||||
}
|
||||
: {},
|
||||
orderBy: { createdAt: "desc" },
|
||||
select: {
|
||||
userId: true,
|
||||
nickname: true,
|
||||
name: true,
|
||||
phone: true,
|
||||
status: true,
|
||||
authLevel: true,
|
||||
createdAt: true,
|
||||
userRoles: { select: { role: { select: { name: true } } } },
|
||||
},
|
||||
take: 100,
|
||||
});
|
||||
const items = users.map((u) => ({
|
||||
...u,
|
||||
roles: u.userRoles.map((r) => r.role.name),
|
||||
}));
|
||||
return NextResponse.json({ users: items });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user