Skip to content

Commit

Permalink
Merge branch 'sef-global:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
sathudeva7 authored Oct 10, 2023
2 parents ab8c138 + 8dd5fb1 commit 1ea89a5
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 102 deletions.
36 changes: 17 additions & 19 deletions src/controllers/admin/category.controller.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,47 +11,43 @@ import {
export const addCategory = async (
req: Request,
res: Response
): Promise<void> => {
): Promise<ApiResponse<Category>> => {
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<void> => {
): Promise<ApiResponse<Category>> => {
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 })
}
}
52 changes: 29 additions & 23 deletions src/controllers/admin/mentor.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,66 +6,72 @@ 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<void> => {
): Promise<ApiResponse<Mentor>> => {
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<void> => {
): Promise<ApiResponse<Mentor>> => {
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 { 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
}
}

Expand Down
21 changes: 11 additions & 10 deletions src/controllers/admin/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
): Promise<ApiResponse<Profile>> => {
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 })
}
}
43 changes: 31 additions & 12 deletions src/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
export const register = async (
req: Request,
res: Response
): Promise<ApiResponse<Profile>> => {
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<void> => {
export const login = async (
req: Request,
res: Response
): Promise<ApiResponse<Profile>> => {
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)
Expand All @@ -42,28 +54,35 @@ export const login = async (req: Request, res: Response): Promise<void> => {
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<void> => {
export const logout = async (
req: Request,
res: Response
): Promise<ApiResponse<Profile>> => {
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
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/controllers/category.controller.ts
Original file line number Diff line number Diff line change
@@ -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<void> => {
): Promise<ApiResponse<Category>> => {
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
}
}
Loading

0 comments on commit 1ea89a5

Please sign in to comment.