diff --git a/src/controllers/admin/mentor.controller.ts b/src/controllers/admin/mentor.controller.ts index a7f678eb..0d36086a 100644 --- a/src/controllers/admin/mentor.controller.ts +++ b/src/controllers/admin/mentor.controller.ts @@ -2,13 +2,13 @@ import type { Request, Response } from 'express' import { findAllMentorEmails, getAllMentors, - updateAvailability, updateMentorStatus } from '../../services/admin/mentor.service' import { ApplicationStatus, ProfileTypes } from '../../enums' import type Profile from '../../entities/profile.entity' import type Mentor from '../../entities/mentor.entity' import type { ApiResponse } from '../../types' +import { updateAvailability } from '../../services/mentor.service' export const mentorStatusHandler = async ( req: Request, @@ -120,11 +120,9 @@ export const updateMentorAvailability = async ( return res.status(403).json({ message: 'Only Admins are allowed' }) } - const { mentor, statusCode, message } = await updateAvailability( - mentorId, - availability - ) - return res.status(statusCode).json({ mentor, message }) + const { statusCode, updatedMentorApplication, message } = + await updateAvailability(mentorId, availability) + return res.status(statusCode).json({ updatedMentorApplication, message }) } catch (err) { if (err instanceof Error) { console.error('Error executing query', err) diff --git a/src/controllers/mentor.controller.ts b/src/controllers/mentor.controller.ts index 3a517791..1336d912 100644 --- a/src/controllers/mentor.controller.ts +++ b/src/controllers/mentor.controller.ts @@ -41,7 +41,7 @@ export const mentorAvailabilityHandler = async ( try { const user = req.user as Profile const { availability } = req.body - const result = await updateAvailability(user, availability) + const result = await updateAvailability(user.uuid, availability) return res.status(result.statusCode).json(result.updatedMentorApplication) } catch (err) { if (err instanceof Error) { diff --git a/src/routes/admin/mentor/mentor.route.test.ts b/src/routes/admin/mentor/mentor.route.test.ts index be934908..551038c1 100644 --- a/src/routes/admin/mentor/mentor.route.test.ts +++ b/src/routes/admin/mentor/mentor.route.test.ts @@ -14,6 +14,7 @@ let server: Express let mentorAgent: supertest.SuperAgentTest let adminAgent: supertest.SuperAgentTest let mentorId: string +let mentorProfileId: string describe('Admin mentor routes', () => { beforeAll(async () => { @@ -54,6 +55,7 @@ describe('Admin mentor routes', () => { .expect(201) mentorId = response.body.mentor.uuid + mentorProfileId = response.body.mentor.profile.uuid }, 5000) it('should update the mentor application state', async () => { @@ -123,11 +125,11 @@ describe('Admin mentor routes', () => { 'should update mentor availability and return a 201 with the updated availability', async (availability) => { const response = await adminAgent - .put(`/api/admin/mentors/${mentorId}/availability`) + .put(`/api/admin/mentors/${mentorProfileId}/availability`) .send({ availability }) .expect(200) - const mentor = response.body.mentor + const mentor = response.body.updatedMentorApplication expect(mentor).toHaveProperty('availability', availability) } @@ -136,8 +138,9 @@ describe('Admin mentor routes', () => { it.each([true, false])( 'should only allow admins to update the mentor availability', async (availability) => { + console.log(mentorProfileId) await mentorAgent - .put(`/api/admin/mentors/${mentorId}/availability`) + .put(`/api/admin/mentors/${mentorProfileId}/availability`) .send({ availability }) .expect(403) } @@ -153,4 +156,8 @@ describe('Admin mentor routes', () => { .expect(404) } ) + + afterAll(async () => { + await dataSource.destroy() + }) }) diff --git a/src/services/admin/mentor.service.ts b/src/services/admin/mentor.service.ts index dc095d8c..b826d4a8 100644 --- a/src/services/admin/mentor.service.ts +++ b/src/services/admin/mentor.service.ts @@ -109,45 +109,3 @@ export const findAllMentorEmails = async ( throw new Error('Error getting mentors emails') } } - -export const updateAvailability = async ( - mentorId: string, - availability: boolean -): Promise<{ statusCode: number; mentor?: Mentor | null; message: string }> => { - try { - const mentorRepository = dataSource.getRepository(Mentor) - - const existingMentor = await mentorRepository.findOne({ - where: { uuid: mentorId }, - select: [ - 'application', - 'state', - 'availability', - 'uuid', - 'category', - 'profile', - 'created_at' - ], - relations: ['category', 'profile'] - }) - - if (!existingMentor) { - return { - statusCode: 404, - message: 'Mentor not found' - } - } - - existingMentor.availability = availability - const mentor = await mentorRepository.save(existingMentor) - - return { - statusCode: 200, - mentor, - message: 'Mentor Availability updated' - } - } catch (err) { - console.error('Error creating mentor', err) - throw new Error('Error creating mentor') - } -} diff --git a/src/services/mentor.service.ts b/src/services/mentor.service.ts index 34612da9..98ca228f 100644 --- a/src/services/mentor.service.ts +++ b/src/services/mentor.service.ts @@ -74,13 +74,17 @@ export const createMentor = async ( } export const updateAvailability = async ( - user: Profile, + mentorId: string, availability: boolean -): Promise<{ statusCode: number; updatedMentorApplication: Mentor }> => { +): Promise<{ + statusCode: number + updatedMentorApplication?: Mentor + message: string +}> => { try { const mentorRepository = dataSource.getRepository(Mentor) const existingMentorApplications = await mentorRepository.find({ - where: { profile: { uuid: user.uuid } } + where: { profile: { uuid: mentorId } } }) const mentorApplication = existingMentorApplications[0] @@ -92,10 +96,14 @@ export const updateAvailability = async ( ) return { statusCode: 200, - updatedMentorApplication + updatedMentorApplication, + message: 'Mentor availability updated sucessfully' } } else { - throw new Error('Mentor application not found') + return { + statusCode: 404, + message: 'Mentor not found' + } } } catch (err) { console.error('Error creating mentor', err)