2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
handle-callback-err,
|
|
|
|
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
|
2020-09-23 04:49:26 -04:00
|
|
|
const { ObjectId } = require('mongodb')
|
2019-05-29 05:21:06 -04:00
|
|
|
const UserGetter = require('../User/UserGetter')
|
|
|
|
|
|
|
|
module.exports = UserMembershipViewModel = {
|
|
|
|
build(userOrEmail) {
|
|
|
|
if (userOrEmail._id) {
|
|
|
|
return buildUserViewModel(userOrEmail)
|
|
|
|
} else {
|
|
|
|
return buildUserViewModelWithEmail(userOrEmail)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
buildAsync(userOrIdOrEmail, callback) {
|
|
|
|
if (callback == null) {
|
|
|
|
callback = function(error, viewModel) {}
|
|
|
|
}
|
|
|
|
if (!(userOrIdOrEmail instanceof ObjectId)) {
|
|
|
|
// 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
|
|
|
|
}
|
2019-10-01 07:27:15 -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))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var buildUserViewModel = function(user, isInvite) {
|
|
|
|
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)
|