Skip to content

Commit

Permalink
Refactored controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Krishnadeva committed Oct 10, 2023
1 parent 1ea89a5 commit e2416f6
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/controllers/admin/mentor.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,29 +78,30 @@ export const getAllMentorsByStatus = async (
export const getAllMentorEmails = async (
req: Request,
res: Response
): Promise<void> => {
): Promise<ApiResponse<string[]>> => {
try {
const user = req.user as Profile
const status: ApplicationStatus | undefined = req.query.status as
| ApplicationStatus
| undefined

if (user.type !== ProfileTypes.ADMIN) {
res.status(403).json({ message: 'Only Admins are allowed' })
} else {
if (status && !(status?.toUpperCase() in ApplicationStatus)) {
res.status(400).json({ message: 'Please provide a valid status' })
return
}
const { emails, statusCode, message } = await findAllMentorEmails(status)
res.status(statusCode).json({ emails, message })
return res.status(403).json({ message: 'Only Admins are allowed' })
}

if (status && !(status?.toUpperCase() in ApplicationStatus)) {
return res.status(400).json({ message: 'Please provide a valid status' })
}

const { emails, statusCode, message } = await findAllMentorEmails(status)
return res.status(statusCode).json({ emails, message })
} catch (err) {
if (err instanceof Error) {
console.error('Error executing query', err)
res
return res
.status(500)
.json({ error: 'Internal server error', message: err.message })
}
throw err
}
}

0 comments on commit e2416f6

Please sign in to comment.