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 public endpoint #90

Merged
merged 7 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
38 changes: 37 additions & 1 deletion src/controllers/mentor.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import type { Request, Response } from 'express'
import {
createMentor,
updateAvailability,
getMentor
getMentor,
getAllMentors
} from '../services/mentor.service'
import type Profile from '../entities/profile.entity'
import type Mentor from '../entities/mentor.entity'
Expand Down Expand Up @@ -90,3 +91,38 @@ export const mentorDetailsHandler = async (
throw err
}
}

export const getAllMentorsHandler = async (
req: Request,
res: Response
): Promise<ApiResponse<Mentor>> => {
try {
const category: string = req.query.category as string
const { mentors, statusCode } = await getAllMentors(category)
if (mentors !== null && mentors !== undefined) {
const mentorDetails = mentors.map((mentor) => ({
mentorId: mentor.uuid,
category: mentor.category.category,
profile: {
contact_email: mentor.profile.contact_email,
first_name: mentor.profile.first_name,
last_name: mentor.profile.last_name,
image_url: mentor.profile.image_url,
linkedin_url: mentor.profile.linkedin_url
}
}))
return res.status(statusCode).json({ mentors: mentorDetails })
} else {
return res.status(404).json({ error: 'Mentors not found' })
}
} 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
}
}
4 changes: 3 additions & 1 deletion src/routes/mentor/mentor.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { requireAuth } from './../../controllers/auth.controller'
import {
mentorApplicationHandler,
mentorAvailabilityHandler,
mentorDetailsHandler
mentorDetailsHandler,
getAllMentorsHandler
} from './../../controllers/mentor.controller'

const mentorRouter = express.Router()

mentorRouter.post('/', requireAuth, mentorApplicationHandler)
mentorRouter.put('/me/availability', requireAuth, mentorAvailabilityHandler)
mentorRouter.get('/:mentorId', mentorDetailsHandler)
mentorRouter.get('/', getAllMentorsHandler)

export default mentorRouter
84 changes: 84 additions & 0 deletions src/services/mentor.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { getAllMentors } from './mentor.service'
import { dataSource } from '../configs/dbConfig'

interface Mentor {
id: number
name: string
}

interface MockMentorRepository {
createQueryBuilder: jest.Mock
leftJoinAndSelect: jest.Mock
where: jest.Mock
andWhere: jest.Mock
getMany: jest.Mock<Promise<Mentor[]>>
}

jest.mock('../configs/dbConfig', () => ({
dataSource: {
getRepository: jest.fn()
}
}))

describe('getAllMentors', () => {
it('should get all mentors without category', async () => {
const mockMentorRepository = createMockMentorRepository([
{ id: 1, name: 'Mentor 1' }
])
;(dataSource.getRepository as jest.Mock).mockReturnValueOnce(
mockMentorRepository
)

const result = await getAllMentors(undefined)

expect(result.statusCode).toBe(200)
expect(result.message).toBe('Mentors found')
expect(result.mentors).toEqual([{ id: 1, name: 'Mentor 1' }])
})

it('should get mentors with category', async () => {
const mockMentorRepository = createMockMentorRepository([
{ id: 1, name: 'Mentor 1' }
])
;(dataSource.getRepository as jest.Mock).mockReturnValueOnce(
mockMentorRepository
)

const result = await getAllMentors('SomeCategory')

expect(result.statusCode).toBe(200)
expect(result.message).toBe('Mentors found')
expect(result.mentors).toEqual([{ id: 1, name: 'Mentor 1' }])
})

it('should return 404 if no mentors found', async () => {
const mockMentorRepository = createMockMentorRepository([])
;(dataSource.getRepository as jest.Mock).mockReturnValueOnce(
mockMentorRepository
)

const result = await getAllMentors('SomeCategory')

expect(result.statusCode).toBe(404)
expect(result.message).toBe('No mentors found')
expect(result.mentors).toBeUndefined()
})
})

function createMockMentorRepository(
data: Mentor[] | undefined,
error?: Error
): MockMentorRepository {
return {
createQueryBuilder: jest.fn().mockReturnThis(),
leftJoinAndSelect: jest.fn().mockReturnThis(),
where: jest.fn().mockReturnThis(),
andWhere: jest.fn().mockReturnThis(),
getMany: jest.fn().mockImplementation(async () => {
if (error) {
throw error
}
return data
})
}
}
45 changes: 45 additions & 0 deletions src/services/mentor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,48 @@ export const searchMentorsByQuery = async (
throw new Error('Error getting mentor')
}
}

export const getAllMentors = async (
category?: string | null
): Promise<{
statusCode: number
mentors?: Mentor[] | null
message: string
}> => {
try {
let mentors: Mentor[] = []
const mentorRepository = dataSource.getRepository(Mentor)
if (category == null) {
mentors = await mentorRepository
.createQueryBuilder('mentor')
.leftJoinAndSelect('mentor.profile', 'profile')
.leftJoinAndSelect('mentor.category', 'category')
.where('mentor.state = :state', { state: 'approved' })
.getMany()
} else {
mentors = await mentorRepository
.createQueryBuilder('mentor')
.leftJoinAndSelect('mentor.profile', 'profile')
.leftJoinAndSelect('mentor.category', 'category')
.where('mentor.state = :state', { state: 'approved' })
.andWhere('category.category = :category', { category })
.getMany()
}

if (!mentors || mentors.length === 0) {
return {
statusCode: 404,
message: 'No mentors found'
}
}

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