diff --git a/src/controllers/admin/user.controller.ts b/src/controllers/admin/user.controller.ts index 88c83a41..9234ae1f 100644 --- a/src/controllers/admin/user.controller.ts +++ b/src/controllers/admin/user.controller.ts @@ -1,26 +1,35 @@ import type { Request, Response } from 'express' -import { getAllUsers } from '../../services/admin/user.service' import type Profile from '../../entities/profile.entity' import { ProfileTypes } from '../../enums' -import type { ApiResponse } from '../../types' +import { getAllUsers } from '../../services/admin/user.service' +import type { PaginatedApiResponse } from '../../types' export const getAllUsersHandler = async ( req: Request, res: Response -): Promise> => { +): Promise>> => { try { const user = req.user as Profile + const pageNumber = parseInt(req.query.pageNumber as string) + const pageSize = parseInt(req.query.pageSize as string) + if (user.type !== ProfileTypes.ADMIN) { return res.status(403).json({ message: 'Only Admins are allowed' }) } - const users = await getAllUsers() - if (users?.length === 0) { - return res.status(404).json({ message: 'No users available' }) - } + const { items, message, statusCode, totalItemCount } = await getAllUsers({ + pageNumber, + pageSize + }) - return res.status(200).json({ profiles: users }) + return res.status(statusCode).json({ + items, + message, + pageNumber, + pageSize, + totalItemCount + }) } catch (err) { console.error('Error executing query', err) return res.status(500).json({ error: err }) diff --git a/src/routes/admin/user/user.route.ts b/src/routes/admin/user/user.route.ts index 2af5a34b..c264a0db 100644 --- a/src/routes/admin/user/user.route.ts +++ b/src/routes/admin/user/user.route.ts @@ -1,9 +1,15 @@ import express from 'express' import { getAllUsersHandler } from '../../../controllers/admin/user.controller' import { requireAuth } from '../../../controllers/auth.controller' +import { requestQueryValidator } from '../../../middlewares/requestValidator' +import { paginationSchema } from '../../../schemas/common/pagination-request.schema' const userRouter = express.Router() -userRouter.get('/', requireAuth, getAllUsersHandler) +userRouter.get( + '/', + [requireAuth, requestQueryValidator(paginationSchema)], + getAllUsersHandler +) export default userRouter diff --git a/src/services/admin/user.service.ts b/src/services/admin/user.service.ts index 1ccaa270..4aab806d 100644 --- a/src/services/admin/user.service.ts +++ b/src/services/admin/user.service.ts @@ -1,8 +1,25 @@ import { dataSource } from '../../configs/dbConfig' import Profile from '../../entities/profile.entity' +import { type PaginatedApiResponse } from '../../types' -export const getAllUsers = async (): Promise => { +export const getAllUsers = async ({ + pageNumber, + pageSize +}: { + pageNumber: number + pageSize: number +}): Promise> => { const profileRepository = dataSource.getRepository(Profile) - const allUsers = await profileRepository.find() - return allUsers + const [users, count] = await profileRepository.findAndCount({ + skip: (pageNumber - 1) * pageSize, + take: pageSize + }) + return { + pageNumber, + pageSize, + totalItemCount: count, + items: users, + statusCode: 200, + message: 'Users retrieved successfully' + } }