diff --git a/src/controllers/admin/category.controller.ts b/src/controllers/admin/category.controller.ts index 4b39c71f..abeeca5d 100644 --- a/src/controllers/admin/category.controller.ts +++ b/src/controllers/admin/category.controller.ts @@ -1,6 +1,8 @@ import type { Request, Response } from 'express' import type Profile from '../../entities/profile.entity' import { ProfileTypes } from '../../enums' +import type Category from '../../entities/category.entity' +import type { ApiResponse } from '../../types' import { changeCategory, createCategory @@ -9,47 +11,43 @@ import { export const addCategory = async ( req: Request, res: Response -): Promise => { +): Promise> => { try { const user = req.user as Profile const { categoryName } = req.body if (user.type !== ProfileTypes.ADMIN) { - res.status(403).json({ message: 'Only Admins are allowed' }) - } else { - const { category, statusCode, message } = await createCategory( - categoryName - ) - - res.status(statusCode).json({ category, message }) + return res.status(403).json({ message: 'Only Admins are allowed' }) } + + const { category, statusCode, message } = await createCategory(categoryName) + return res.status(statusCode).json({ category, message }) } catch (err) { console.error('Error executing query', err) - res.status(500).json({ error: err }) + return res.status(500).json({ error: err }) } } export const updateCategory = async ( req: Request, res: Response -): Promise => { +): Promise> => { try { const user = req.user as Profile const { categoryName } = req.body const { categoryId } = req.params if (user.type !== ProfileTypes.ADMIN) { - res.status(403).json({ message: 'Only Admins are allowed' }) - } else { - const { category, statusCode, message } = await changeCategory( - categoryId, - categoryName - ) - - res.status(statusCode).json({ category, message }) + return res.status(403).json({ message: 'Only Admins are allowed' }) } + + const { category, statusCode, message } = await changeCategory( + categoryId, + categoryName + ) + return res.status(statusCode).json({ category, message }) } catch (err) { console.error('Error executing query', err) - res.status(500).json({ error: err }) + return res.status(500).json({ error: err }) } } diff --git a/src/controllers/admin/mentor.controller.ts b/src/controllers/admin/mentor.controller.ts index 7dde6a69..a77f9a64 100644 --- a/src/controllers/admin/mentor.controller.ts +++ b/src/controllers/admin/mentor.controller.ts @@ -6,43 +6,47 @@ import { } 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' export const mentorStatusHandler = async ( req: Request, res: Response -): Promise => { +): Promise> => { try { const user = req.user as Profile const { status } = req.body const { mentorId } = req.params 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 { mentor, statusCode, message } = await updateMentorStatus( - mentorId, - status - ) - res.status(statusCode).json({ mentor, message }) + return res.status(403).json({ message: 'Only Admins are allowed' }) } + + if (!(status.toUpperCase() in ApplicationStatus)) { + return res.status(400).json({ message: 'Please provide a valid status' }) + } + + const { mentor, statusCode, message } = await updateMentorStatus( + mentorId, + status + ) + return res.status(statusCode).json({ mentor, 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 } } export const getAllMentorsByStatus = async ( req: Request, res: Response -): Promise => { +): Promise> => { try { const user = req.user as Profile const status: ApplicationStatus | undefined = req.query.status as @@ -50,22 +54,24 @@ export const getAllMentorsByStatus = async ( | 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 { mentors, statusCode, message } = await getAllMentors(status) - res.status(statusCode).json({ mentors, 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 { mentors, statusCode, message } = await getAllMentors(status) + return res.status(statusCode).json({ mentors, 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 } } diff --git a/src/controllers/admin/user.controller.ts b/src/controllers/admin/user.controller.ts index 2d6e6052..88c83a41 100644 --- a/src/controllers/admin/user.controller.ts +++ b/src/controllers/admin/user.controller.ts @@ -2,26 +2,27 @@ import type { Request, Response } from 'express' import { getAllUsers } from '../../services/admin/user.service' import type Profile from '../../entities/profile.entity' import { ProfileTypes } from '../../enums' +import type { ApiResponse } from '../../types' export const getAllUsersHandler = async ( req: Request, res: Response -): Promise => { +): Promise> => { try { const user = req.user as Profile if (user.type !== ProfileTypes.ADMIN) { - res.status(403).json({ message: 'Only Admins are allowed' }) - } else { - const users = await getAllUsers() - if (users?.length === 0) { - res.status(404).json({ message: 'No users available' }) - } else { - res.status(200).json({ profiles: users }) - } + return res.status(403).json({ message: 'Only Admins are allowed' }) } + + const users = await getAllUsers() + if (users?.length === 0) { + return res.status(404).json({ message: 'No users available' }) + } + + return res.status(200).json({ profiles: users }) } catch (err) { console.error('Error executing query', err) - res.status(500).json({ error: err }) + return res.status(500).json({ error: err }) } } diff --git a/src/controllers/auth.controller.ts b/src/controllers/auth.controller.ts index 553eeac4..a6a4eecb 100644 --- a/src/controllers/auth.controller.ts +++ b/src/controllers/auth.controller.ts @@ -4,34 +4,46 @@ import passport from 'passport' import type Profile from '../entities/profile.entity' import jwt from 'jsonwebtoken' import { JWT_SECRET } from '../configs/envConfig' +import type { ApiResponse } from '../types' -export const register = async (req: Request, res: Response): Promise => { +export const register = async ( + req: Request, + res: Response +): Promise> => { try { const { email, password } = req.body if (!email || !password) { - res.status(400).json({ error: 'Email and password are required fields' }) + return res + .status(400) + .json({ error: 'Email and password are required fields' }) } const { statusCode, message, profile } = await registerUser(email, password) - - res.status(statusCode).json({ message, profile }) + return res.status(statusCode).json({ message, profile }) } 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 } } -export const login = async (req: Request, res: Response): Promise => { +export const login = async ( + req: Request, + res: Response +): Promise> => { try { const { email, password } = req.body if (!email || !password) { - res.status(400).json({ error: 'Email and password are required fields' }) + return res + .status(400) + .json({ error: 'Email and password are required fields' }) } const { statusCode, message, token } = await loginUser(email, password) @@ -42,28 +54,35 @@ export const login = async (req: Request, res: Response): Promise => { secure: false // TODO: Set to true when using HTTPS }) - res.status(statusCode).json({ message }) + return res.status(statusCode).json({ 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 } } -export const logout = async (req: Request, res: Response): Promise => { +export const logout = async ( + req: Request, + res: Response +): Promise> => { try { res.clearCookie('jwt', { httpOnly: true }) - res.status(200).json({ message: 'Logged out successfully' }) + return res.status(200).json({ message: 'Logged out successfully' }) } catch (err) { if (err instanceof Error) { console.error('Something went wrong', err) - res + return res .status(500) .json({ error: 'Internal server error', message: err.message }) } + + throw err } } diff --git a/src/controllers/category.controller.ts b/src/controllers/category.controller.ts index cfb6889b..2a89ccd9 100644 --- a/src/controllers/category.controller.ts +++ b/src/controllers/category.controller.ts @@ -1,20 +1,24 @@ import type { Request, Response } from 'express' import { getAllCategories } from '../services/category.service' +import type { ApiResponse } from '../types' +import type Category from '../entities/category.entity' export const getCategories = async ( req: Request, res: Response -): Promise => { +): Promise> => { try { const { statusCode, categories, message } = await getAllCategories() - res.status(statusCode).json({ categories, message }) + return res.status(statusCode).json({ categories, 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 } } diff --git a/src/controllers/mentor.controller.ts b/src/controllers/mentor.controller.ts index ff34edbb..3a517791 100644 --- a/src/controllers/mentor.controller.ts +++ b/src/controllers/mentor.controller.ts @@ -5,11 +5,13 @@ import { getMentor } from '../services/mentor.service' import type Profile from '../entities/profile.entity' +import type Mentor from '../entities/mentor.entity' +import type { ApiResponse } from '../types' export const mentorApplicationHandler = async ( req: Request, res: Response -): Promise => { +): Promise> => { try { const user = req.user as Profile const { application, categoryId } = req.body @@ -19,66 +21,72 @@ export const mentorApplicationHandler = async ( categoryId ) - res.status(statusCode).json({ mentor, message }) + return res.status(statusCode).json({ mentor, 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 } } export const mentorAvailabilityHandler = async ( req: Request, res: Response -): Promise => { +): Promise> => { try { const user = req.user as Profile const { availability } = req.body const result = await updateAvailability(user, availability) - res.status(result.statusCode).json(result.updatedMentorApplication) + return res.status(result.statusCode).json(result.updatedMentorApplication) } 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 } } export const mentorDetailsHandler = async ( req: Request, res: Response -): Promise => { +): Promise> => { try { const mentorId = req.params.mentorId const { mentor, statusCode, message } = await getMentor(mentorId) if (!mentor) { - res.status(statusCode).json({ error: message }) - } else { - const mentorDetails = { - mentorId: mentor.uuid, - category: mentor.category.category, - profile: { - contact_email: mentor.profile.contact_email, - first_name: mentor.profile.first_name, - last_name: mentor.profile.last_name, - image_url: mentor.profile.image_url, - linkedin_url: mentor.profile.linkedin_url - } + return res.status(statusCode).json({ error: message }) + } + + const mentorDetails = { + mentorId: mentor.uuid, + category: mentor.category.category, + profile: { + contact_email: mentor.profile.contact_email, + first_name: mentor.profile.first_name, + last_name: mentor.profile.last_name, + image_url: mentor.profile.image_url, + linkedin_url: mentor.profile.linkedin_url } - res.status(statusCode).json({ ...mentorDetails }) } + return res.status(statusCode).json({ ...mentorDetails }) } 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 } } diff --git a/src/controllers/profile.controller.ts b/src/controllers/profile.controller.ts index 1e7c10f7..6ab739dd 100644 --- a/src/controllers/profile.controller.ts +++ b/src/controllers/profile.controller.ts @@ -1,69 +1,77 @@ import type { Request, Response } from 'express' import { updateProfile, deleteProfile } from '../services/profile.service' import type Profile from '../entities/profile.entity' +import type { ApiResponse } from '../types' + export const getProfileHandler = async ( req: Request, res: Response -): Promise => { +): Promise> => { try { const { user } = req if (!user) { - res.status(404).json({ message: 'Profile not found' }) + return res.status(404).json({ message: 'Profile not found' }) } - res.status(200).json(user) + return res.status(200).json(user) } 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 } } export const updateProfileHandler = async ( req: Request, res: Response -): Promise => { +): Promise> => { try { const user = req.user as Profile if (!user) { - res.status(404).json({ message: 'Profile not found' }) + return res.status(404).json({ message: 'Profile not found' }) } const { statusCode, message, profile } = user && (await updateProfile(user, req.body)) - res.status(statusCode).json({ message, profile }) + return res.status(statusCode).json({ message, profile }) } 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 } } export const deleteProfileHandler = async ( req: Request, res: Response -): Promise => { +): Promise> => { try { const user = req.user as Profile if (!user) { - res.status(404).json({ message: 'Profile not found' }) - } else { - await deleteProfile(user.uuid) - res.status(200).json({ message: 'Profile deleted' }) + return res.status(404).json({ message: 'Profile not found' }) } + + await deleteProfile(user.uuid) + return res.status(200).json({ message: 'Profile deleted' }) } catch (err) { if (err instanceof Error) { console.error('Error executing query', err) - res + return res .status(500) .json({ error: 'Internal server errorrrr', message: err.message }) } + + throw err } } diff --git a/src/types.ts b/src/types.ts index 5596d525..8d017dd0 100644 --- a/src/types.ts +++ b/src/types.ts @@ -16,3 +16,9 @@ export interface MenteeApplication { state: string mentor_id: bigint } + +export interface ApiResponse { + statusCode: number + message?: string + data?: T | null +}