Skip to content

Commit

Permalink
Fixed update mentor availability service
Browse files Browse the repository at this point in the history
  • Loading branch information
Krishnadeva committed Oct 12, 2023
1 parent 690d6e9 commit 5f9c5a7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 57 deletions.
10 changes: 4 additions & 6 deletions src/controllers/admin/mentor.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/mentor.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 10 additions & 3 deletions src/routes/admin/mentor/mentor.route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -153,4 +156,8 @@ describe('Admin mentor routes', () => {
.expect(404)
}
)

afterAll(async () => {
await dataSource.destroy()
})
})
42 changes: 0 additions & 42 deletions src/services/admin/mentor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
}
18 changes: 13 additions & 5 deletions src/services/mentor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)
Expand Down

0 comments on commit 5f9c5a7

Please sign in to comment.