Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Get all mentors endpoint (Admin) #24 #67

Merged
merged 4 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/controllers/admin/mentor.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { Request, Response } from 'express'
import { updateMentorStatus } from '../../services/admin/mentor.service'
import {
getAllMentors,
updateMentorStatus
} from '../../services/admin/mentor.service'
import { ApplicationStatus, ProfileTypes } from '../../enums'
import type Profile from '../../entities/profile.entity'

Expand Down Expand Up @@ -34,3 +37,31 @@ export const mentorStatusHandler = async (
}
}
}

export const getAllMentorsByStatus = async (
req: Request,
res: Response
): Promise<void> => {
try {
const user = req.user as Profile
const status: ApplicationStatus = req.query.status as ApplicationStatus
anjula-sack marked this conversation as resolved.
Show resolved Hide resolved

if (user.type !== ProfileTypes.ADMIN) {
res.status(403).json({ message: 'Only Admins are allowed' })
} else {
if (!(status.toUpperCase() in ApplicationStatus)) {
res.status(400).json({ message: 'Please provide a valid status' })
return
}
const { mentors, statusCode, message } = await getAllMentors(status)
res.status(statusCode).json({ mentors, message })
}
} catch (err) {
if (err instanceof Error) {
console.error('Error executing query', err)
res
.status(500)
.json({ error: 'Internal server error', message: err.message })
}
}
}
16 changes: 15 additions & 1 deletion src/routes/admin/mentor/mentor.route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { startServer } from '../../../app'
import type { Express } from 'express'
import supertest from 'supertest'
import Profile from '../../../entities/profile.entity'
import { ProfileTypes } from '../../../enums'
import { ApplicationStatus, ProfileTypes } from '../../../enums'
import { dataSource } from '../../../configs/dbConfig'
import bcrypt from 'bcrypt'
import { mentorApplicationInfo, mockAdmin, mockMentor } from '../../../../mocks'
Expand Down Expand Up @@ -75,4 +75,18 @@ describe('Admin mentor routes', () => {
.send({ status: 'approved' })
.expect(403)
})

it('should return mentors and a success message when mentors are found', async () => {
const response = await adminAgent
.get(`/api/admin/mentors?status=${ApplicationStatus.APPROVED}`)
.expect(200)

expect(response.body).toHaveProperty('mentors')
})
anjula-sack marked this conversation as resolved.
Show resolved Hide resolved

it('should only allow admins to get the mentors', async () => {
await mentorAgent
.get(`/api/admin/mentors?status=${ApplicationStatus.APPROVED}`)
.expect(403)
})
})
6 changes: 5 additions & 1 deletion src/routes/admin/mentor/mentor.route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import express from 'express'
import { requireAuth } from '../../../controllers/auth.controller'
import { mentorStatusHandler } from '../../../controllers/admin/mentor.controller'
import {
getAllMentorsByStatus,
mentorStatusHandler
} from '../../../controllers/admin/mentor.controller'

const mentorRouter = express.Router()

mentorRouter.put('/:mentorId/status', requireAuth, mentorStatusHandler)
mentorRouter.get('/', requireAuth, getAllMentorsByStatus)

export default mentorRouter
39 changes: 39 additions & 0 deletions src/services/admin/mentor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,42 @@ export const updateMentorStatus = async (
throw new Error('Error updating the mentor status')
}
}

export const getAllMentors = async (
status: ApplicationStatus | null
): Promise<{
statusCode: number
mentors?: Mentor[]
message: string
}> => {
try {
const mentorRepository = dataSource.getRepository(Mentor)

const mentors: Mentor[] = await mentorRepository.find({
where: status ? { state: status } : {},
select: [
'application',
'availability',
'state',
'created_at',
'updated_at'
],
relations: ['profile', 'category']
})

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

return {
statusCode: 200,
mentors,
message: 'All Mentors found'
}
} catch (err) {
throw new Error('Error getting mentors')
}
}