diff --git a/src/controllers/mentor.controller.ts b/src/controllers/mentor.controller.ts index 1336d912..3edf33e3 100644 --- a/src/controllers/mentor.controller.ts +++ b/src/controllers/mentor.controller.ts @@ -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' @@ -90,3 +91,38 @@ export const mentorDetailsHandler = async ( throw err } } + +export const getAllMentorsHandler = async ( + req: Request, + res: Response +): Promise> => { + 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 + } +} diff --git a/src/routes/mentor/mentor.route.ts b/src/routes/mentor/mentor.route.ts index 495b4aed..5e8c6d8b 100644 --- a/src/routes/mentor/mentor.route.ts +++ b/src/routes/mentor/mentor.route.ts @@ -3,7 +3,8 @@ import { requireAuth } from './../../controllers/auth.controller' import { mentorApplicationHandler, mentorAvailabilityHandler, - mentorDetailsHandler + mentorDetailsHandler, + getAllMentorsHandler } from './../../controllers/mentor.controller' const mentorRouter = express.Router() @@ -11,5 +12,6 @@ const mentorRouter = express.Router() mentorRouter.post('/', requireAuth, mentorApplicationHandler) mentorRouter.put('/me/availability', requireAuth, mentorAvailabilityHandler) mentorRouter.get('/:mentorId', mentorDetailsHandler) +mentorRouter.get('/', getAllMentorsHandler) export default mentorRouter diff --git a/src/services/mentor.service.test.ts b/src/services/mentor.service.test.ts new file mode 100644 index 00000000..c8b39a04 --- /dev/null +++ b/src/services/mentor.service.test.ts @@ -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> +} + +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 + }) + } +} diff --git a/src/services/mentor.service.ts b/src/services/mentor.service.ts index 0d0c8b0f..1971942c 100644 --- a/src/services/mentor.service.ts +++ b/src/services/mentor.service.ts @@ -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') + } +}