fix(api): parse search api url params
This commit is contained in:
parent
e4b3141d7a
commit
2df519fa84
1 changed files with 15 additions and 5 deletions
|
@ -5,8 +5,8 @@ import { z } from 'zod/v4';
|
|||
|
||||
const getSearchUserSchema = z.object({
|
||||
query: z.string().optional().default(''),
|
||||
count: z.int().min(1).max(100).default(10),
|
||||
page: z.int().min(1).default(1),
|
||||
count: z.coerce.number().min(1).max(100).default(10),
|
||||
page: z.coerce.number().min(1).default(1),
|
||||
sort_by: z
|
||||
.enum(['created_at', 'name', 'first_name', 'last_name', 'id'])
|
||||
.optional()
|
||||
|
@ -109,7 +109,7 @@ export const GET = auth(async function GET(req) {
|
|||
{ status: 404 },
|
||||
);
|
||||
|
||||
const dataRaw = new URL(req.url).searchParams;
|
||||
const dataRaw = Object.fromEntries(new URL(req.url).searchParams);
|
||||
const data = await getSearchUserSchema.safeParseAsync(dataRaw);
|
||||
if (!data.success) {
|
||||
return NextResponse.json(
|
||||
|
@ -142,13 +142,23 @@ export const GET = auth(async function GET(req) {
|
|||
first_name: true,
|
||||
last_name: true,
|
||||
timezone: true,
|
||||
image: true,
|
||||
image: true
|
||||
},
|
||||
});
|
||||
|
||||
const userCount = await prisma.user.count({
|
||||
where: {
|
||||
OR: [
|
||||
{ name: { contains: query } },
|
||||
{ first_name: { contains: query } },
|
||||
{ last_name: { contains: query } },
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
users: dbUsers,
|
||||
count: dbUsers.length,
|
||||
count: userCount,
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue