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 mentors by category endpoint (Admin) #26 #76

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 16 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
31 changes: 31 additions & 0 deletions src/controllers/admin/mentor.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Request, Response } from 'express'
import {
findAllMentorEmails,
getAllMentors,
searchMentorsByCategory,
updateMentorStatus
} from '../../services/admin/mentor.service'
import { ApplicationStatus, ProfileTypes } from '../../enums'
Expand Down Expand Up @@ -163,3 +164,33 @@ export const searchMentors = async (
throw err
}
}

export const getAllMentorsByCategory = async (
req: Request,
res: Response
): Promise<ApiResponse<Mentor>> => {
try {
const user = req.user as Profile
const category: string | undefined = req.query.category as
| string
| undefined

if (user.type !== ProfileTypes.ADMIN) {
return res.status(403).json({ message: 'Only Admins are allowed' })
}

const { statusCode, mentors, message } = await searchMentorsByCategory(
category
)
return res.status(statusCode).json({ mentors, message })
} catch (err) {
if (err instanceof Error) {
console.error('Error executing query', err)
return res
.status(500)
.json({ error: 'Internal server error', message: err.message })
}

throw err
}
}
24 changes: 24 additions & 0 deletions src/routes/admin/mentor/mentor.route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { dataSource } from '../../../configs/dbConfig'
import bcrypt from 'bcrypt'
import { v4 as uuidv4 } from 'uuid'
import { mentorApplicationInfo, mockAdmin, mockMentor } from '../../../../mocks'
import type Category from '../../../entities/category.entity'
import type Mentor from '../../../entities/mentor.entity'

const port = Math.floor(Math.random() * (9999 - 3000 + 1)) + 3000

Expand All @@ -15,6 +17,7 @@ let mentorAgent: supertest.SuperAgentTest
let adminAgent: supertest.SuperAgentTest
let mentorId: string
let mentorProfileId: string
let category: Category

describe('Admin mentor routes', () => {
beforeAll(async () => {
Expand Down Expand Up @@ -56,6 +59,7 @@ describe('Admin mentor routes', () => {

mentorId = response.body.mentor.uuid
mentorProfileId = response.body.mentor.profile.uuid
category = categoryResponse.body.category
}, 5000)

it('should update the mentor application state', async () => {
Expand Down Expand Up @@ -178,6 +182,26 @@ describe('Admin mentor routes', () => {
await mentorAgent.get(`/api/admin/mentors/search?q=john`).expect(403)
})

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

const mentorDetails = response.body.mentors

mentorDetails.forEach((mentor: Mentor) => {
expect(mentor).toHaveProperty('profile')
expect(mentor).toHaveProperty('application')
expect(mentor).toHaveProperty('category')
})
})

it('should only allow admins to search mentors by category', async () => {
await mentorAgent
.get(`/api/admin/mentors/category?category=${category.category}`)
.expect(403)
})

afterAll(async () => {
await dataSource.destroy()
})
Expand Down
2 changes: 2 additions & 0 deletions src/routes/admin/mentor/mentor.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import express from 'express'
import { requireAuth } from '../../../controllers/auth.controller'
import {
getAllMentorEmails,
getAllMentorsByCategory,
getAllMentorsByStatus,
mentorStatusHandler,
searchMentors,
Expand All @@ -19,5 +20,6 @@ mentorRouter.put(
updateMentorAvailability
)
mentorRouter.get('/search', requireAuth, searchMentors)
mentorRouter.get('/category', requireAuth, getAllMentorsByCategory)

export default mentorRouter
49 changes: 49 additions & 0 deletions src/services/admin/mentor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,52 @@ export const findAllMentorEmails = async (
throw new Error('Error getting mentors emails')
}
}

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

const categoryQuery = category ? `${category}%` : ''

const mentors: Mentor[] = await mentorRepository
.createQueryBuilder('mentor')
.innerJoinAndSelect('mentor.category', 'category')
.leftJoinAndSelect('mentor.profile', 'profile')
.select([
'mentor.uuid',
'category.category',
'mentor.application',
'profile.contact_email',
'profile.first_name',
'profile.last_name',
'profile.image_url',
'profile.linkedin_url'
])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without selecting fields, what does it return? @sathudeva7 Also can you drop a comment on the issue and get assigned?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for the mistake could you please assign me to this issue

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without selecting fields ->

Screenshot 2023-10-24 at 16 27 54

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sathudeva7 Can you drop a comment on the issue then I will assign you

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sathudeva7 Can you update it to without selecting?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any updates @sathudeva7?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i will update within this week

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any updates @sathudeva7?

.where('category.category ILIKE :name', {
name: categoryQuery
})
.getMany()

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

return {
statusCode: 200,
mentors,
message: 'All search Mentors by category found'
}
} catch (err) {
console.error('Error getting mentor', err)
throw new Error('Error getting mentor')
}
}