Skip to content

Commit

Permalink
Resolved mentioned issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Krishnadeva committed Oct 7, 2023
1 parent ee7c77b commit ffe92b7
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 154 deletions.
4 changes: 0 additions & 4 deletions src/controllers/admin/mentor.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ export const getAllMentorsByStatus = async (
if (user.type !== ProfileTypes.ADMIN) {
res.status(403).json({ message: 'Only Admins are allowed' })
} else {
if (!(status.toUpperCase() in ApplicationStatus)) {
res.status(400).json({ message: 'Please provide a valid status' })
return
}
const { mentors, statusCode, message } = await getAllMentors(status)
res.status(statusCode).json({ mentors, message })
}
Expand Down
16 changes: 15 additions & 1 deletion src/routes/admin/mentor/mentor.route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { startServer } from '../../../app'
import type { Express } from 'express'
import supertest from 'supertest'
import Profile from '../../../entities/profile.entity'
import { ProfileTypes } from '../../../enums'
import { ApplicationStatus, ProfileTypes } from '../../../enums'
import { dataSource } from '../../../configs/dbConfig'
import bcrypt from 'bcrypt'
import { mentorApplicationInfo, mockAdmin, mockMentor } from '../../../../mocks'
Expand Down Expand Up @@ -75,4 +75,18 @@ describe('Admin mentor routes', () => {
.send({ status: 'approved' })
.expect(403)
})

it('should return mentorsArray and a success message when mentors are found', async () => {
const response = await adminAgent
.get(`/api/admin/mentors?status=${ApplicationStatus.APPROVED}`)
.expect(200)

expect(response.body).toHaveProperty('mentors')
})

it('should only allow admins to get the mentors', async () => {
await mentorAgent
.get(`/api/admin/mentors?status=${ApplicationStatus.APPROVED}`)
.expect(403)
})
})
78 changes: 0 additions & 78 deletions src/services/admin/mentor.service.test.ts

This file was deleted.

66 changes: 5 additions & 61 deletions src/services/admin/mentor.service.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
import { dataSource } from '../../configs/dbConfig'
import Mentor from '../../entities/mentor.entity'
import type { ApplicationStatus } from '../../enums'
import type { AllMentorsArray, MentorInfo } from '../../types'

const applicationFormat: MentorInfo = {
designation: '',
country: '',
areas_of_expertise: '',
expectations_from_mentees: '',
mentoring_philosophy: '',
commitment_to_program: false,
previous_experience_as_mentor: false,
reason_for_being_mentor: '',
cv_link: ''
}

export const updateMentorStatus = async (
mentorId: string,
Expand Down Expand Up @@ -51,17 +38,17 @@ export const updateMentorStatus = async (
}

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

const mentorsList: Mentor[] = await mentorRepository.find({
where: { state: status },
const mentors: Mentor[] = await mentorRepository.find({
where: status ? { state: status } : {},
select: [
'application',
'availability',
Expand All @@ -72,19 +59,6 @@ export const getAllMentors = async (
relations: ['profile', 'category']
})

const mentors: AllMentorsArray[] = mentorsList.map((mentor, i) => {
Object.entries(mentor.application).map((item) => {
switchQuestion(item, applicationFormat)
})
return {
...mentor,
mentor_id: i + 1,
application: applicationFormat,
category: mentor.category.category,
state: mentor.state.toUpperCase()
}
})

if (!mentors) {
return {
statusCode: 404,
Expand All @@ -98,36 +72,6 @@ export const getAllMentors = async (
message: 'All Mentors found'
}
} catch (err) {
console.error('Error updating the mentor status', err)
throw new Error('Error updating the mentor status')
}
}

function switchQuestion(item: any, format: MentorInfo): void {
switch (item[1].question.toLowerCase()) {
case 'what is your country?':
format.country = item[1].answers
break
case 'what is your expertise?':
format.areas_of_expertise = item[1].answers
break
case 'what is your mentoring startegy?':
format.mentoring_philosophy = item[1].answers
break
case 'what is your designation?':
format.designation = item[1].answers
break
case 'what is your reason for being mentor?':
format.reason_for_being_mentor = item[1].answers
break
case 'what is your expectations from mentees?':
format.expectations_from_mentees = item[1].answers
break
case 'do you have revious experience as mentor?':
format.previous_experience_as_mentor = item[1].answers
break
default:
// Handle other cases if needed
break
throw new Error('Error getting mentors')
}
}
10 changes: 0 additions & 10 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type Profile from './entities/profile.entity'
interface Option {
// todo: To be determined (Not final)
answer: string
Expand Down Expand Up @@ -29,12 +28,3 @@ export interface MentorInfo {
reason_for_being_mentor: string
cv_link: string
}

export interface AllMentorsArray {
mentor_id: number
state: string
category: string
application: MentorInfo
availability: boolean
profile: Profile
}

0 comments on commit ffe92b7

Please sign in to comment.