Skip to content

Commit

Permalink
update user routes with pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
dsmabulage committed Aug 18, 2024
1 parent 0548d5b commit 77351b6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
25 changes: 17 additions & 8 deletions src/controllers/admin/user.controller.ts
Original file line number Diff line number Diff line change
@@ -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<ApiResponse<Profile>> => {
): Promise<Response<PaginatedApiResponse<Profile>>> => {
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 })
Expand Down
8 changes: 7 additions & 1 deletion src/routes/admin/user/user.route.ts
Original file line number Diff line number Diff line change
@@ -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
23 changes: 20 additions & 3 deletions src/services/admin/user.service.ts
Original file line number Diff line number Diff line change
@@ -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<Profile[] | undefined> => {
export const getAllUsers = async ({
pageNumber,
pageSize
}: {
pageNumber: number
pageSize: number
}): Promise<PaginatedApiResponse<Profile>> => {
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'
}
}

0 comments on commit 77351b6

Please sign in to comment.