2019-05-29 05:21:06 -04:00
|
|
|
let UserController
|
|
|
|
const UserGetter = require('./UserGetter')
|
|
|
|
const AuthenticationController = require('../Authentication/AuthenticationController')
|
2020-09-23 04:49:26 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
module.exports = UserController = {
|
|
|
|
getLoggedInUsersPersonalInfo(req, res, next) {
|
2019-09-24 04:43:43 -04:00
|
|
|
const userId = AuthenticationController.getLoggedInUserId(req)
|
|
|
|
if (!userId) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return next(new Error('User is not logged in'))
|
|
|
|
}
|
2019-09-24 04:43:43 -04:00
|
|
|
UserGetter.getUser(
|
|
|
|
userId,
|
2019-05-29 05:21:06 -04:00
|
|
|
{
|
|
|
|
first_name: true,
|
|
|
|
last_name: true,
|
|
|
|
role: true,
|
|
|
|
institution: true,
|
|
|
|
email: true,
|
|
|
|
signUpDate: true
|
|
|
|
},
|
2021-04-14 09:17:21 -04:00
|
|
|
function (error, user) {
|
2019-09-24 04:43:43 -04:00
|
|
|
if (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return next(error)
|
|
|
|
}
|
2019-09-24 04:43:43 -04:00
|
|
|
UserController.sendFormattedPersonalInfo(user, res, next)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
getPersonalInfo(req, res, next) {
|
|
|
|
let query
|
2019-09-24 04:43:43 -04:00
|
|
|
const userId = req.params.user_id
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-09-24 04:43:43 -04:00
|
|
|
if (/^\d+$/.test(userId)) {
|
|
|
|
query = { 'overleaf.id': parseInt(userId, 10) }
|
|
|
|
} else if (/^[a-f0-9]{24}$/.test(userId)) {
|
|
|
|
query = { _id: ObjectId(userId) }
|
2019-05-29 05:21:06 -04:00
|
|
|
} else {
|
2020-05-06 06:02:16 -04:00
|
|
|
return res.sendStatus(400)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2019-09-24 04:43:43 -04:00
|
|
|
UserGetter.getUser(
|
2019-05-29 05:21:06 -04:00
|
|
|
query,
|
|
|
|
{ _id: true, first_name: true, last_name: true, email: true },
|
2021-04-14 09:17:21 -04:00
|
|
|
function (error, user) {
|
2019-09-24 04:43:43 -04:00
|
|
|
if (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return next(error)
|
|
|
|
}
|
2019-09-24 04:43:43 -04:00
|
|
|
if (!user) {
|
2020-05-06 06:02:16 -04:00
|
|
|
return res.sendStatus(404)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-09-24 04:43:43 -04:00
|
|
|
UserController.sendFormattedPersonalInfo(user, res, next)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
sendFormattedPersonalInfo(user, res, next) {
|
|
|
|
const info = UserController.formatPersonalInfo(user)
|
2019-09-24 04:43:43 -04:00
|
|
|
res.json(info)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
formatPersonalInfo(user, callback) {
|
2019-09-24 04:43:43 -04:00
|
|
|
if (!user) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return {}
|
|
|
|
}
|
2019-09-24 04:43:43 -04:00
|
|
|
const formattedUser = { id: user._id.toString() }
|
2019-05-29 05:21:06 -04:00
|
|
|
for (let key of [
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'email',
|
|
|
|
'signUpDate',
|
|
|
|
'role',
|
|
|
|
'institution'
|
|
|
|
]) {
|
2019-09-24 04:43:43 -04:00
|
|
|
if (user[key]) {
|
|
|
|
formattedUser[key] = user[key]
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|
2019-09-24 04:43:43 -04:00
|
|
|
return formattedUser
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|