Skip to content
Open
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
24 changes: 6 additions & 18 deletions CustomError.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
const {getReasonPhrase} = require('http-status-codes');

// eslint-disable-next-line require-jsdoc
class CustomError extends Error {
static get NAME_AUTHENTICATION_ERROR() {
return 'AuthenticationError';
}
static get NAME_AUTHORIZATION_ERROR() {
return 'AuthorizationError';
}
static get NAME_VALUE_ERROR() {
return 'ValueError';
}
// eslint-disable-next-line require-jsdoc
constructor(message, statusCode) {
super(message);
this.name = this.constructor.name;
this.statusCode = statusCode;

if (this.statusCode === 401) {
this.name = this.constructor.NAME_AUTHENTICATION_ERROR;
}
if (this.statusCode === 403) {
this.name = this.constructor.NAME_AUTHORIZATION_ERROR;
}
if (this.statusCode === 400) {
this.name = this.constructor.NAME_VALUE_ERROR;
if (getReasonPhrase(statusCode)) {
this.name = getReasonPhrase(statusCode);
}

if (Error.captureStackTrace) {
Expand Down
1 change: 1 addition & 0 deletions fields/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const ROOT = require(__dirname + '/../config').ROOT;
const academiaFields = require(ROOT + '/fields/academia');

module.exports = Object.freeze({
EMAIL: 'email',
COLLEGE_ID: academiaFields.COLLEGE_ID,
COURSE_ID: academiaFields.COURSE_ID,
BRANCH_ID: academiaFields.BRANCH_ID,
Expand Down
7 changes: 3 additions & 4 deletions middlewares/authenticateUser.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const {StatusCodes, ReasonPhrases} = require('http-status-codes');
const ROOT = require(__dirname + '/../config').ROOT;
const authenticateClientId = require(ROOT +
'/utility/oauth/authenticateClientId');
const CustomError = require(ROOT + '/CustomError');

/**
*
* @param {RequestObject} req
Expand All @@ -23,8 +22,8 @@ function authenticateUser(req, res, next) {
next();
})
.catch((err) => {
err.statusCode = 401;
err.name = CustomError.NAME_AUTHENTICATION_ERROR;
err.statusCode = StatusCodes.UNAUTHORIZED;
err.name = ReasonPhrases.UNAUTHORIZED;
next(err);
});
}
Expand Down
21 changes: 20 additions & 1 deletion routes/user/gpa.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ const authenticateUser = require(ROOT + '/middlewares/authenticateUser');
const {User} = require(ROOT + '/models/user');
const utility = require(ROOT + '/utility');
const userServices = require(ROOT + '/services/user');
const userFields = require(ROOT + '/fields/user');
const CustomError = require(ROOT + '/CustomError');
// eslint-disable-next-line new-cap
const router = express.Router();

router.get('/gpa-data', authenticateUser, (req, res, next) => {
User.findOne({email: req.user.email})
.select('-_id -__v')
Expand Down Expand Up @@ -50,4 +51,22 @@ router.post('/gpa-data', authenticateUser, (req, res, next) => {
.catch(next);
});

router.delete('/gpa-data', authenticateUser, (req, res, next) => {
User.deleteOne({
[userFields.EMAIL]: req.user.email,
})
.then((queryRes) => {
if (queryRes.deletedCount) {
res
.status(StatusCodes.OK)
.json(utility.responseUtil.getSuccessResponse());
} else {
throw new CustomError(
'Nothing to delete, user data do not exist',
StatusCodes.NOT_FOUND,
);
}
})
.catch(next);
});
module.exports = router;