2019-05-29 05:21:06 -04:00
|
|
|
let UserController
|
|
|
|
const UserGetter = require('./UserGetter')
|
|
|
|
const AuthenticationController = require('../Authentication/AuthenticationController')
|
|
|
|
const { ObjectId } = require('mongojs')
|
|
|
|
|
|
|
|
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
|
|
|
|
},
|
|
|
|
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 {
|
|
|
|
return res.send(400)
|
|
|
|
}
|
|
|
|
|
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 },
|
|
|
|
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) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return res.send(404)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|