Skip to content

Commit

Permalink
update mentor routes with pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
dsmabulage committed Aug 18, 2024
1 parent 245baa5 commit 0548d5b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 27 deletions.
31 changes: 22 additions & 9 deletions src/controllers/admin/mentor.controller.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import type { Request, Response } from 'express'
import type Mentor from '../../entities/mentor.entity'
import type Profile from '../../entities/profile.entity'
import { MentorApplicationStatus, ProfileTypes } from '../../enums'
import {
findAllMentorEmails,
getAllMentors,
updateMentorStatus,
getMentor
getMentor,
updateMentorStatus
} from '../../services/admin/mentor.service'
import { MentorApplicationStatus, ProfileTypes } from '../../enums'
import type Profile from '../../entities/profile.entity'
import type Mentor from '../../entities/mentor.entity'
import type { ApiResponse } from '../../types'
import {
searchMentorsByQuery,
updateAvailability
} from '../../services/mentor.service'
import type { ApiResponse, PaginatedApiResponse } from '../../types'

export const mentorStatusHandler = async (
req: Request,
Expand Down Expand Up @@ -51,8 +51,11 @@ export const mentorStatusHandler = async (
export const getAllMentorsByStatus = async (
req: Request,
res: Response
): Promise<ApiResponse<Mentor>> => {
): Promise<ApiResponse<PaginatedApiResponse<Mentor>>> => {
try {
const pageNumber = parseInt(req.query.pageNumber as string)
const pageSize = parseInt(req.query.pageSize as string)

const user = req.user as Profile
const status: MentorApplicationStatus | undefined = req.query.status as
| MentorApplicationStatus
Expand All @@ -66,8 +69,18 @@ export const getAllMentorsByStatus = async (
return res.status(400).json({ message: 'Please provide a valid status' })
}

const { mentors, statusCode, message } = await getAllMentors(status)
return res.status(statusCode).json({ mentors, message })
const { items, totalItemCount, statusCode, message } = await getAllMentors({
status,
pageNumber,
pageSize
})
return res.status(statusCode).json({
pageNumber,
pageSize,
totalItemCount,
items,
message
})
} catch (err) {
if (err instanceof Error) {
console.error('Error executing query', err)
Expand Down
8 changes: 6 additions & 2 deletions src/routes/admin/mentor/mentor.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
searchMentorsSchema,
updateMentorAvailabilitySchema
} from '../../../schemas/admin/admin.mentor-routes.schema'
import { paginationSchema } from '../../../schemas/common/pagination-request.schema'

const mentorRouter = express.Router()

Expand All @@ -27,17 +28,20 @@ mentorRouter.put(
[requireAuth, requestBodyValidator(mentorStatusSchema)],
mentorStatusHandler
)
mentorRouter.get('/:mentorId', requireAuth, mentorDetailsHandler)
mentorRouter.get(
'/',
[requireAuth, requestQueryValidator(getAllMentorsByStatusSchema)],
[
requireAuth,
requestQueryValidator(getAllMentorsByStatusSchema.merge(paginationSchema))
],
getAllMentorsByStatus
)
mentorRouter.get(
'/emails',
[requireAuth, requestQueryValidator(getAllMentorEmailsSchema)],
getAllMentorEmails
)
mentorRouter.get('/:mentorId', requireAuth, mentorDetailsHandler)
mentorRouter.put(
'/:mentorId/availability',
[requireAuth, requestBodyValidator(updateMentorAvailabilitySchema)],
Expand Down
33 changes: 17 additions & 16 deletions src/services/admin/mentor.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { dataSource } from '../../configs/dbConfig'
import Mentor from '../../entities/mentor.entity'
import type { MentorApplicationStatus } from '../../enums'
import { type PaginatedApiResponse } from '../../types'
import { getEmailContent } from '../../utils'
import { sendEmail } from './email.service'

Expand Down Expand Up @@ -53,31 +54,31 @@ export const updateMentorStatus = async (
}
}

export const getAllMentors = async (
export const getAllMentors = async ({
status,
pageNumber,
pageSize
}: {
status: MentorApplicationStatus | undefined
): Promise<{
statusCode: number
mentors?: Mentor[]
message: string
}> => {
pageNumber: number
pageSize: number
}): Promise<PaginatedApiResponse<Mentor>> => {
try {
const mentorRepository = dataSource.getRepository(Mentor)

const mentors: Mentor[] = await mentorRepository.find({
const [mentors, count] = await mentorRepository.findAndCount({
where: status ? { state: status } : {},
relations: ['profile', 'category']
relations: ['profile', 'category'],
skip: (pageNumber - 1) * pageSize,
take: pageSize
})

if (!mentors) {
return {
statusCode: 404,
message: 'Mentors not found'
}
}

return {
statusCode: 200,
mentors,
pageNumber,
pageSize,
items: mentors,
totalItemCount: count,
message: 'All Mentors found'
}
} catch (err) {
Expand Down

0 comments on commit 0548d5b

Please sign in to comment.