2024-01-04 16:57:32 -05:00
|
|
|
import { db } from "@/lib/db"
|
|
|
|
import { NextResponse } from "next/server";
|
|
|
|
import fetchUser from "@/lib/fetch-user";
|
|
|
|
|
|
|
|
export async function GET(req: Request) {
|
|
|
|
try {
|
|
|
|
const user = await fetchUser(req)
|
|
|
|
if (!user || user.role != "ADMIN") {
|
2024-08-25 17:35:46 -04:00
|
|
|
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
|
2024-01-04 16:57:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const { searchParams } = new URL(req.url)
|
|
|
|
const qn = searchParams.get('qn') as string
|
|
|
|
const id = searchParams.get('id') as string
|
|
|
|
|
|
|
|
if (qn) {
|
|
|
|
const users = await db.user.findMany({
|
|
|
|
where: {
|
|
|
|
name: {
|
|
|
|
contains: qn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return NextResponse.json(users)
|
|
|
|
}
|
|
|
|
if (id) {
|
2024-06-24 18:16:55 -04:00
|
|
|
const users = await db.user.findFirst({
|
2024-01-04 16:57:32 -05:00
|
|
|
where: {
|
|
|
|
id: id
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return NextResponse.json(users)
|
|
|
|
}
|
|
|
|
|
|
|
|
const users = await db.user.findMany();
|
|
|
|
return NextResponse.json(users)
|
|
|
|
} catch (error) {
|
2024-06-24 18:16:55 -04:00
|
|
|
console.log("[USERS]", error);
|
2024-08-25 17:35:46 -04:00
|
|
|
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
2024-01-04 16:57:32 -05:00
|
|
|
}
|
|
|
|
}
|