Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Implement approve/reject mentee endpoint for admins and mentors #108

Merged
merged 3 commits into from
Apr 28, 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
30 changes: 29 additions & 1 deletion src/controllers/admin/mentee.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { ApplicationStatus, ProfileTypes } from '../../enums'
import type Profile from '../../entities/profile.entity'
import type Mentee from '../../entities/mentee.entity'
import type { ApiResponse } from '../../types'
import { getAllMentees } from '../../services/admin/mentee.service'
import {
getAllMentees,
updateStatus
} from '../../services/admin/mentee.service'

export const getMentees = async (
req: Request,
Expand Down Expand Up @@ -32,7 +35,32 @@ export const getMentees = async (
.status(500)
.json({ error: 'Internal server error', message: err.message })
}
throw err
}
}

export const updateMenteeStatus = async (
req: Request,
res: Response
): Promise<ApiResponse<Mentee>> => {
try {
const user = req.user as Profile
const { state } = req.body
const { menteeId } = req.params

if (user.type !== ProfileTypes.ADMIN) {
return res.status(403).json({ message: 'Only Admins are allowed' })
}

const { statusCode, message } = await updateStatus(menteeId, state)
return res.status(statusCode).json({ message })
} 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
}
}
29 changes: 28 additions & 1 deletion src/controllers/mentee.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 { type ApiResponse } from '../types'
import type Mentee from '../entities/mentee.entity'
import type Profile from '../entities/profile.entity'
import { addMentee } from '../services/admin/mentee.service'
import { addMentee, updateStatus } from '../services/admin/mentee.service'
import { ProfileTypes } from '../enums'

export const menteeApplicationHandler = async (
req: Request,
Expand All @@ -28,3 +29,29 @@ export const menteeApplicationHandler = async (
throw err
}
}

export const updateMenteeStatus = async (
req: Request,
res: Response
): Promise<ApiResponse<Mentee>> => {
try {
const user = req.user as Profile
const { state } = req.body
const { menteeId } = req.params

if (user.type !== ProfileTypes.ADMIN) {
return res.status(403).json({ message: 'Only Admins are allowed' })
}

const { statusCode, message } = await updateStatus(menteeId, state)
return res.status(statusCode).json({ message })
} 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
}
}
6 changes: 5 additions & 1 deletion src/routes/admin/mentee/mentee.route.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import express from 'express'
import { requireAuth } from '../../../controllers/auth.controller'
import { getAllMenteeEmails } from '../../../controllers/admin/email.controller'
import { getMentees } from '../../../controllers/admin/mentee.controller'
import {
getMentees,
updateMenteeStatus
} from '../../../controllers/admin/mentee.controller'

const menteeRouter = express.Router()

menteeRouter.get('/emails/', requireAuth, getAllMenteeEmails)
menteeRouter.get('/applications', requireAuth, getMentees)
menteeRouter.put('/:menteeId/status/', requireAuth, updateMenteeStatus)

export default menteeRouter
6 changes: 5 additions & 1 deletion src/routes/mentee/mentee.route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import express from 'express'
import { requireAuth } from '../../controllers/auth.controller'
import { menteeApplicationHandler } from '../../controllers/mentee.controller'
import {
menteeApplicationHandler,
updateMenteeStatus
} from '../../controllers/mentee.controller'

const menteeRouter = express.Router()

menteeRouter.post('/', requireAuth, menteeApplicationHandler)
menteeRouter.put('/:menteeId/status/', requireAuth, updateMenteeStatus)

export default menteeRouter
67 changes: 67 additions & 0 deletions src/services/admin/mentee.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,70 @@ export const getAllMenteesByMentor = async (
throw new Error('Error getting mentees')
}
}

export const updateStatus = async (
menteeId: string,
state: string
): Promise<{
statusCode: number
updatedMenteeApplication?: Mentee
message: string
}> => {
try {
const menteeRepository = dataSource.getRepository(Mentee)
const mentee = await menteeRepository.findOne({
where: {
uuid: menteeId
}
})

if (!mentee) {
return {
statusCode: 404,
message: 'Mentee not found'
}
}

const profileUuid = mentee.profile.uuid

const approvedApplications = await menteeRepository.findOne({
where: {
state: ApplicationStatus.APPROVED,
profile: {
uuid: profileUuid
}
}
})

// Handle Approve status
if (approvedApplications && state === 'approved') {
return {
statusCode: 400,
message: 'Mentee is already approved'
}
} else {
switch (state) {
case 'approved':
mentee.state = ApplicationStatus.APPROVED
break
case 'rejected':
mentee.state = ApplicationStatus.REJECTED
break
case 'pending':
mentee.state = ApplicationStatus.PENDING
break
default:
break
}
const updatedMenteeApplication = await menteeRepository.save(mentee)
return {
statusCode: 200,
updatedMenteeApplication,
message: 'Mentee application state successfully updated'
}
}
} catch (err) {
console.error('Error updating mentee status', err)
throw new Error('Error updating mentee status')
}
}
Loading