2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
2020-12-15 05:23:54 -05:00
|
|
|
node/handle-callback-err,
|
2019-05-29 05:21:06 -04:00
|
|
|
max-len,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
let UserMembershipViewModel
|
|
|
|
const UserGetter = require('../User/UserGetter')
|
2020-10-06 06:31:34 -04:00
|
|
|
const { isObjectIdInstance } = require('../Helpers/Mongo')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
|
|
|
module.exports = UserMembershipViewModel = {
|
|
|
|
build(userOrEmail) {
|
|
|
|
if (userOrEmail._id) {
|
|
|
|
return buildUserViewModel(userOrEmail)
|
|
|
|
} else {
|
|
|
|
return buildUserViewModelWithEmail(userOrEmail)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
buildAsync(userOrIdOrEmail, callback) {
|
|
|
|
if (callback == null) {
|
2021-04-14 09:17:21 -04:00
|
|
|
callback = function (error, viewModel) {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-10-06 06:31:34 -04:00
|
|
|
if (!isObjectIdInstance(userOrIdOrEmail)) {
|
2019-05-29 05:21:06 -04:00
|
|
|
// userOrIdOrEmail is a user or an email and can be parsed by #build
|
|
|
|
return callback(null, UserMembershipViewModel.build(userOrIdOrEmail))
|
|
|
|
}
|
|
|
|
|
|
|
|
const userId = userOrIdOrEmail
|
2020-08-12 10:19:14 -04:00
|
|
|
const projection = {
|
|
|
|
email: 1,
|
|
|
|
first_name: 1,
|
|
|
|
last_name: 1,
|
|
|
|
lastLoggedIn: 1
|
|
|
|
}
|
2021-04-14 09:17:21 -04:00
|
|
|
return UserGetter.getUser(userId, projection, function (error, user) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null || user == null) {
|
|
|
|
return callback(null, buildUserViewModelWithId(userId.toString()))
|
|
|
|
}
|
|
|
|
return callback(null, buildUserViewModel(user))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
var buildUserViewModel = function (user, isInvite) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (isInvite == null) {
|
|
|
|
isInvite = false
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
_id: user._id || null,
|
|
|
|
email: user.email || null,
|
|
|
|
first_name: user.first_name || null,
|
|
|
|
last_name: user.last_name || null,
|
2020-08-12 10:19:14 -04:00
|
|
|
last_logged_in_at: user.lastLoggedIn || null,
|
2019-05-29 05:21:06 -04:00
|
|
|
invite: isInvite
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var buildUserViewModelWithEmail = email => buildUserViewModel({ email }, true)
|
|
|
|
|
|
|
|
var buildUserViewModelWithId = id => buildUserViewModel({ _id: id }, false)
|