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({
|
const getSearchUserSchema = z.object({
|
||||||
query: z.string().optional().default(''),
|
query: z.string().optional().default(''),
|
||||||
count: z.int().min(1).max(100).default(10),
|
count: z.coerce.number().min(1).max(100).default(10),
|
||||||
page: z.int().min(1).default(1),
|
page: z.coerce.number().min(1).default(1),
|
||||||
sort_by: z
|
sort_by: z
|
||||||
.enum(['created_at', 'name', 'first_name', 'last_name', 'id'])
|
.enum(['created_at', 'name', 'first_name', 'last_name', 'id'])
|
||||||
.optional()
|
.optional()
|
||||||
|
@ -109,7 +109,7 @@ export const GET = auth(async function GET(req) {
|
||||||
{ status: 404 },
|
{ status: 404 },
|
||||||
);
|
);
|
||||||
|
|
||||||
const dataRaw = new URL(req.url).searchParams;
|
const dataRaw = Object.fromEntries(new URL(req.url).searchParams);
|
||||||
const data = await getSearchUserSchema.safeParseAsync(dataRaw);
|
const data = await getSearchUserSchema.safeParseAsync(dataRaw);
|
||||||
if (!data.success) {
|
if (!data.success) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
|
@ -142,13 +142,23 @@ export const GET = auth(async function GET(req) {
|
||||||
first_name: true,
|
first_name: true,
|
||||||
last_name: true,
|
last_name: true,
|
||||||
timezone: 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({
|
return NextResponse.json({
|
||||||
success: true,
|
success: true,
|
||||||
users: dbUsers,
|
users: dbUsers,
|
||||||
count: dbUsers.length,
|
count: userCount,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue