-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpa.js
More file actions
72 lines (69 loc) · 2.12 KB
/
gpa.js
File metadata and controls
72 lines (69 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const express = require('express');
const {StatusCodes} = require('http-status-codes');
const ROOT = require(__dirname + '/../../config').ROOT;
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')
.exec()
.then((user) => {
if (user === null) {
return {};
} else {
return userServices.replaceAcademiaIdsWithValues(user.toObject());
}
})
.then((user) => {
res.status(StatusCodes.OK).json(user);
})
.catch(next);
});
router.post('/gpa-data', authenticateUser, (req, res, next) => {
userServices
.replaceAcademiaValuesWithIds(req.body)
.then(() => {
req.body.email = req.user.email;
return User.findOne({email: req.user.email}).exec();
})
.then((user) => {
if (user === null) {
user = new User(req.body);
} else {
user.overwrite(req.body);
}
user.markModified('semesters');
return user.save();
})
.then((doc) => {
res
.status(StatusCodes.OK)
.send(utility.responseUtil.getSuccessResponse());
})
.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;