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