@@ -16,7 +16,7 @@ const withPage = z.object({
|
||||
|
||||
export const withPagination: NextMiddleware = async (req, _, next) => {
|
||||
const { page, take } = withPage.parse(req.query);
|
||||
const skip = page * take || 0;
|
||||
const skip = (page - 1) * take;
|
||||
req.pagination = {
|
||||
take,
|
||||
skip,
|
||||
|
||||
@@ -39,9 +39,12 @@ async function getHandler(req: NextApiRequest) {
|
||||
const where: Prisma.UserWhereInput = {};
|
||||
// If user is not ADMIN, return only his data.
|
||||
if (!isAdmin) where.id = userId;
|
||||
const data = await prisma.user.findMany({ where, take, skip });
|
||||
const [total, data] = await prisma.$transaction([
|
||||
prisma.user.count(),
|
||||
prisma.user.findMany({ where, take, skip }),
|
||||
]);
|
||||
const users = schemaUsersReadPublic.parse(data);
|
||||
return { users };
|
||||
return { users, total };
|
||||
}
|
||||
|
||||
export default withMiddleware("pagination")(defaultResponder(getHandler));
|
||||
|
||||
Reference in New Issue
Block a user