Skip to content

Commit

Permalink
Merge pull request #55 from ballyalley-o/hotfix/TCCPSERVER54-status-c…
Browse files Browse the repository at this point in the history
…odes-responses

Hotfix/TCCPSERVER54-status-codes-responses
  • Loading branch information
ballyalley-o authored Apr 14, 2024
2 parents 75bf409 + 9ad9b1f commit 98cee5d
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 91 deletions.
8 changes: 1 addition & 7 deletions src/config/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,13 @@ class App {
this._app.use(morgan(Key.MorganDev))
this._app.use(cookieParser())
this._app.use(fileupload())
this._app.use(
cors({
credentials: true,
origin: true
})
)
this._app.use(cors(corsConfig))
this._app.use(mongoSanitize())
this._app.use(helmet())
this._app.use(xssHandler)
this._app.use(rateLimit(GLOBAL.LIMITER))
this._app.use(hpp())
this.registerRoute()
// this._app.use(setHeader)
this._app.use(errorHandler)
this._app.use(notFound)
}
Expand Down
42 changes: 26 additions & 16 deletions src/controller/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ class AuthController {

if (emailExist) {
res.status(Code.FORBIDDEN).json({ message: RESPONSE.error.ALREADY_EXISTS(email) })
return next(new ErrorResponse(RESPONSE.error.ALREADY_EXISTS(email), Code.FORBIDDEN))
return next(new ErrorResponse(RESPONSE.error.ALREADY_EXISTS(email), (res.statusCode = Code.FORBIDDEN)))
}

if (usernameExist) {
res.status(Code.FORBIDDEN).json({ message: RESPONSE.error.ALREADY_EXISTS(email) })
return next(new ErrorResponse(RESPONSE.error.ALREADY_EXISTS(username), Code.FORBIDDEN))
return next(new ErrorResponse(RESPONSE.error.ALREADY_EXISTS(username), (res.statusCode = Code.FORBIDDEN)))
}

const user = await User.create(req.body)
Expand All @@ -78,19 +78,19 @@ class AuthController {

try {
if (!email || !password) {
return next(new ErrorResponse(RESPONSE.error.INVALID_CREDENTIAL, Code.BAD_REQUEST))
return next(new ErrorResponse(RESPONSE.error.INVALID_CREDENTIAL, (res.statusCode = Code.BAD_REQUEST)))
}

const user = await User.findOne({ email }).select(Key.Password)

if (!user) {
return next(new ErrorResponse(RESPONSE.error.INVALID_CREDENTIAL, Code.UNAUTHORIZED))
return next(new ErrorResponse(RESPONSE.error.INVALID_CREDENTIAL, (res.statusCode = Code.UNAUTHORIZED)))
}

const isMatch = await user.matchPassword(password)

if (!isMatch) {
return next(new ErrorResponse(RESPONSE.error.INVALID_CREDENTIAL, Code.UNAUTHORIZED))
return next(new ErrorResponse(RESPONSE.error.INVALID_CREDENTIAL, (res.statusCode = Code.UNAUTHORIZED)))
}

if (user) {
Expand All @@ -99,7 +99,7 @@ class AuthController {
} catch (error) {
if (error instanceof Error) {
goodlog.log(error.message)
return next(new ErrorResponse(RESPONSE.error.INVALID_CREDENTIAL, Code.BAD_REQUEST))
return next(new ErrorResponse(RESPONSE.error.INVALID_CREDENTIAL, (res.statusCode = Code.BAD_REQUEST)))
}
}
}
Expand All @@ -126,13 +126,23 @@ class AuthController {
//@access PRIVATE
@use(LogRequest)
public static async myAccount(req: any, res: Response, _next: NextFunction) {
const user = (await User.findById(req.user.id)) || null
try {
const user = (await User.findById(req.user.id)) || null

res.status(Code.OK).json({
success: true,
message: RESPONSE.success[200],
data: user
})
if (!user) {
return new ErrorResponse(RESPONSE.error[404], (res.statusCode = Code.NOT_FOUND))
}
res.status(Code.OK).json({
success: true,
message: RESPONSE.success[200],
data: user
})
} catch (error) {
if (error instanceof Error) {
goodlog.log(error.message)
return new ErrorResponse(RESPONSE.error[500], (res.statusCode = Code.INTERNAL_SERVER_ERROR))
}
}
}

//@desc Update user details
Expand Down Expand Up @@ -175,7 +185,7 @@ class AuthController {
const user = await User.findById(AuthController._userId).select(Key.Password)

if (!(await user?.matchPassword(req.body.currentPassword))) {
return next(new ErrorResponse(RESPONSE.error.INVALID_CREDENTIAL, Code.UNAUTHORIZED))
return next(new ErrorResponse(RESPONSE.error.INVALID_CREDENTIAL, (res.statusCode = Code.UNAUTHORIZED)))
}

if (user) {
Expand All @@ -196,7 +206,7 @@ class AuthController {
const user = await User.findOne({ email: req.body.email })

if (!user) {
return next(new ErrorResponse(RESPONSE.error.NOT_FOUND(userEmail), Code.NOT_FOUND))
return next(new ErrorResponse(RESPONSE.error.NOT_FOUND(userEmail), (res.statusCode = Code.NOT_FOUND)))
}
const resetToken = user.getResetPasswordToken()

Expand All @@ -219,7 +229,7 @@ class AuthController {
validateBeforeSave: false
})

return next(new ErrorResponse(RESPONSE.error.FAILED_EMAIL, Code.INTERNAL_SERVER_ERROR))
return next(new ErrorResponse(RESPONSE.error.FAILED_EMAIL, (res.statusCode = Code.INTERNAL_SERVER_ERROR)))
}
}

Expand All @@ -243,7 +253,7 @@ class AuthController {
})

if (!user) {
return next(new ErrorResponse(RESPONSE.error.INVALID_TOKEN, Code.ALREADY_REPORTED))
return next(new ErrorResponse(RESPONSE.error.INVALID_TOKEN, (res.statusCode = Code.ALREADY_REPORTED)))
}

user.password = req.body.password
Expand Down
32 changes: 16 additions & 16 deletions src/controller/bootcamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class BootcampController {
const bootcamp = await Bootcamp.findById(BootcampController._bootcampId).populate(Key.UserVirtual, Key.BootcampPopulate)

if (!bootcamp) {
return next(new ErrorResponse(RESPONSE.error.NOT_FOUND_BOOTCAMP(BootcampController._bootcampId), Code.NOT_FOUND))
return next(new ErrorResponse(RESPONSE.error.NOT_FOUND_BOOTCAMP(BootcampController._bootcampId), (res.statusCode = Code.NOT_FOUND)))
}
res.status(Code.OK).json({ success: true, data: bootcamp })
}
Expand All @@ -68,7 +68,7 @@ class BootcampController {
})

if (publishedBootcamp && BootcampController._userRole !== Key.Admin) {
return next(new ErrorResponse(RESPONSE.error.BOOTCAMP_ALREADY_PUBLISHED(BootcampController._userId), Code.BAD_REQUEST))
return next(new ErrorResponse(RESPONSE.error.BOOTCAMP_ALREADY_PUBLISHED(BootcampController._userId), (res.statusCode = Code.BAD_REQUEST)))
}

const bootcamp = await Bootcamp.create(req.body)
Expand All @@ -90,11 +90,11 @@ class BootcampController {
let bootcamp = await Bootcamp.findById(BootcampController._bootcampId)

if (!bootcamp) {
return next(new ErrorResponse(RESPONSE.error.NOT_FOUND_BOOTCAMP(BootcampController._bootcampId), Code.NOT_FOUND))
return next(new ErrorResponse(RESPONSE.error.NOT_FOUND_BOOTCAMP(BootcampController._bootcampId), (res.statusCode = Code.NOT_FOUND)))
}

if (bootcamp.user.toString() !== BootcampController._userRole && BootcampController._userRole !== Key.Admin) {
return next(new ErrorResponse(RESPONSE.error[401], Code.UNAUTHORIZED))
return next(new ErrorResponse(RESPONSE.error[401], (res.statusCode = Code.UNAUTHORIZED)))
}

bootcamp = await Bootcamp.findOneAndUpdate(req.params.id, req.body, {
Expand All @@ -120,11 +120,11 @@ class BootcampController {
const bootcamp = await Bootcamp.findById(BootcampController._bootcampId)

if (!bootcamp) {
return next(new ErrorResponse(RESPONSE.error.NOT_FOUND_BOOTCAMP(BootcampController._bootcampId), Code.NOT_FOUND))
return next(new ErrorResponse(RESPONSE.error.NOT_FOUND_BOOTCAMP(BootcampController._bootcampId), (res.statusCode = Code.NOT_FOUND)))
}

if (bootcamp.user.toString() !== BootcampController._userId && BootcampController._userRole !== Key.Admin) {
return next(new ErrorResponse(RESPONSE.error[401], Code.UNAUTHORIZED))
return next(new ErrorResponse(RESPONSE.error[401], (res.statusCode = Code.UNAUTHORIZED)))
}

await Bootcamp.deleteOne({ _id: BootcampController._bootcampId })
Expand Down Expand Up @@ -168,26 +168,26 @@ class BootcampController {
const bootcamp = await Bootcamp.findById(BootcampController._bootcampId)

if (!bootcamp) {
return next(new ErrorResponse(RESPONSE.error.NOT_FOUND_BOOTCAMP(BootcampController._bootcampId), Code.NOT_FOUND))
return next(new ErrorResponse(RESPONSE.error.NOT_FOUND_BOOTCAMP(BootcampController._bootcampId), (res.statusCode = Code.NOT_FOUND)))
}

if (!req.files) {
return next(new ErrorResponse(RESPONSE.error.FAILED_UPLOAD, Code.BAD_REQUEST))
return next(new ErrorResponse(RESPONSE.error.FAILED_UPLOAD, (res.statusCode = Code.BAD_REQUEST)))
}

if (!photo.mimetype.startsWith(Key.Image)) {
return next(new ErrorResponse(RESPONSE.error.FAILED_UPLOAD, Code.BAD_REQUEST))
return next(new ErrorResponse(RESPONSE.error.FAILED_UPLOAD, (res.statusCode = Code.BAD_REQUEST)))
}

if (photo.size > GLOBAL.MAX_FILE_UPLOAD) {
return next(new ErrorResponse(RESPONSE.error.FAILED_FILESIZE(NumKey.ONE_MB), Code.BAD_REQUEST))
return next(new ErrorResponse(RESPONSE.error.FAILED_FILESIZE(NumKey.ONE_MB), (res.statusCode = Code.BAD_REQUEST)))
}

photo.name = GLOBAL.PHOTO_FILENAME(bootcamp._id, photo.name)
GLOBAL.PHOTO_UPLOAD_MV(photo, bootcamp, async (error: any) => {
goodlog.error(error?.message)
if (error) {
return next(new ErrorResponse(RESPONSE.error.FAILED_UPLOAD, Code.INTERNAL_SERVER_ERROR))
return next(new ErrorResponse(RESPONSE.error.FAILED_UPLOAD, (res.statusCode = Code.INTERNAL_SERVER_ERROR)))
}

await Bootcamp.findByIdAndUpdate(BootcampController._bootcampId, {
Expand Down Expand Up @@ -216,26 +216,26 @@ class BootcampController {
const bootcamp = await Bootcamp.findById(BootcampController._bootcampId)

if (!bootcamp) {
return next(new ErrorResponse(RESPONSE.error.NOT_FOUND_BOOTCAMP(BootcampController._bootcampId), Code.NOT_FOUND))
return next(new ErrorResponse(RESPONSE.error.NOT_FOUND_BOOTCAMP(BootcampController._bootcampId), (res.statusCode = Code.NOT_FOUND)))
}

if (!req.files) {
return next(new ErrorResponse(RESPONSE.error.FAILED_UPLOAD, Code.BAD_REQUEST))
return next(new ErrorResponse(RESPONSE.error.FAILED_UPLOAD, (res.statusCode = Code.BAD_REQUEST)))
}

if (!badge.mimetype.startsWith(Key.Image)) {
return next(new ErrorResponse(RESPONSE.error.FAILED_UPLOAD, Code.BAD_REQUEST))
return next(new ErrorResponse(RESPONSE.error.FAILED_UPLOAD, (res.statusCode = Code.BAD_REQUEST)))
}

if (badge.size > GLOBAL.MAX_FILE_UPLOAD) {
return next(new ErrorResponse(RESPONSE.error.FAILED_FILESIZE(NumKey.ONE_MB), Code.BAD_REQUEST))
return next(new ErrorResponse(RESPONSE.error.FAILED_FILESIZE(NumKey.ONE_MB), (res.statusCode = Code.BAD_REQUEST)))
}

badge.name = GLOBAL.BADGE_FILENAME(bootcamp._id, badge.name)
GLOBAL.BADGE_UPLOAD_MV(badge, bootcamp, async (error: any) => {
goodlog.error(error?.message)
if (error) {
return next(new ErrorResponse(RESPONSE.error.FAILED_UPLOAD, Code.INTERNAL_SERVER_ERROR))
return next(new ErrorResponse(RESPONSE.error.FAILED_UPLOAD, (res.statusCode = Code.INTERNAL_SERVER_ERROR)))
}

await Bootcamp.findByIdAndUpdate(BootcampController._bootcampId, {
Expand Down
44 changes: 21 additions & 23 deletions src/controller/course.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ class CourseController {

if (CourseController._bootcampId) {
const course = await Course.find({
bootcamp: CourseController._bootcampId,
bootcamp: CourseController._bootcampId
})

res.status(Code.OK).json({
success: true,
count: course.length,
data: course,
data: course
})
} else {
res.status(Code.OK).json((res as IResponseExtended).advancedResult)
Expand All @@ -64,16 +64,16 @@ class CourseController {

const course = await Course.findById(CourseController._courseId).populate({
path: Key.BootcampVirtual,
select: Key.CourseSelect,
select: Key.CourseSelect
})

if (!course) {
return next(new ErrorResponse(RESPONSE.error.NOT_FOUND_COURSE(CourseController._courseId), Code.NOT_FOUND))
return next(new ErrorResponse(RESPONSE.error.NOT_FOUND_COURSE(CourseController._courseId), (res.statusCode = Code.NOT_FOUND)))
}
res.status(Code.OK).json({
success: true,
message: RESPONSE.success[200],
data: course,
data: course
})
}

Expand All @@ -91,18 +91,18 @@ class CourseController {
const bootcamp = await Bootcamp.findById(CourseController._bootcampId)

if (!bootcamp) {
return next(new ErrorResponse(RESPONSE.error.NOT_FOUND_BOOTCAMP(CourseController._bootcampId), Code.NOT_FOUND))
return next(new ErrorResponse(RESPONSE.error.NOT_FOUND_BOOTCAMP(CourseController._bootcampId), (res.statusCode = Code.NOT_FOUND)))
}

if (bootcamp.user.toString() !== CourseController._userId && CourseController._userRole !== Key.Admin) {
return next(new ErrorResponse(RESPONSE.error.NOT_OWNER(req.user.id, CourseController._bootcampId), 401))
return next(new ErrorResponse(RESPONSE.error.NOT_OWNER(req.user.id, CourseController._bootcampId), (res.statusCode = Code.UNAUTHORIZED)))
}

const course = await Course.create(req.body)

res.status(Code.CREATED).json({
success: true,
data: course,
data: course
})
}

Expand All @@ -117,22 +117,26 @@ class CourseController {
let course = await Course.findById(CourseController._courseId)

if (!course) {
return next(new ErrorResponse(RESPONSE.error.NOT_OWNER(CourseController._userId, CourseController._courseId), Code.UNAUTHORIZED))
return next(
new ErrorResponse(RESPONSE.error.NOT_OWNER(CourseController._userId, CourseController._courseId), (res.statusCode = Code.NOT_FOUND))
)
}

if (course.user.toString() !== CourseController._userId && CourseController._userRole !== Key.Admin) {
return next(new ErrorResponse(RESPONSE.error.NOT_OWNER(CourseController._userId, CourseController._courseId), Code.UNAUTHORIZED))
return next(
new ErrorResponse(RESPONSE.error.NOT_OWNER(CourseController._userId, CourseController._courseId), (res.statusCode = Code.UNAUTHORIZED))
)
}

course = await Course.findByIdAndUpdate(CourseController._courseId, req.body, {
new: true,
runValidators: true,
runValidators: true
})

res.status(Code.OK).json({
success: true,
message: RESPONSE.success.UPDATED,
data: course,
data: course
})
}
//@desc Delete a course
Expand All @@ -146,29 +150,23 @@ class CourseController {
const course = await Course.findById(CourseController._courseId)

if (!course) {
return next(new ErrorResponse(RESPONSE.error.NOT_FOUND_COURSE(CourseController._courseId), Code.NOT_FOUND))
return next(new ErrorResponse(RESPONSE.error.NOT_FOUND_COURSE(CourseController._courseId), (res.statusCode = Code.NOT_FOUND)))
}

if (course.user.toString() !== CourseController._userId && CourseController._userRole !== Key.Admin) {
return next(new ErrorResponse(RESPONSE.error.NOT_OWNER(CourseController._userId, CourseController._courseId), Code.UNAUTHORIZED))
return next(
new ErrorResponse(RESPONSE.error.NOT_OWNER(CourseController._userId, CourseController._courseId), (res.statusCode = Code.UNAUTHORIZED))
)
}

await Course.deleteOne({ _id: CourseController._courseId })

res.status(Code.OK).json({
success: true,
message: RESPONSE.success.DELETED,
data: {},
data: {}
})
}
}

// const courseController = {
// getCourses: asyncHandler(CourseController.getCourses),
// getCourse: asyncHandler(CourseController.getCourse),
// addCourse: asyncHandler(CourseController.addCourse),
// updateCourse: asyncHandler(CourseController.updateCourse),
// deleteCourse: asyncHandler(CourseController.deleteCourse),
// }

export default CourseController
Loading

0 comments on commit 98cee5d

Please sign in to comment.