2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
max-len,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* 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 ContactsController
|
2021-07-28 04:51:20 -04:00
|
|
|
const SessionManager = require('../Authentication/SessionManager')
|
2019-05-29 05:21:06 -04:00
|
|
|
const ContactManager = require('./ContactManager')
|
|
|
|
const UserGetter = require('../User/UserGetter')
|
2021-11-10 08:40:18 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2019-05-29 05:21:06 -04:00
|
|
|
const Modules = require('../../infrastructure/Modules')
|
|
|
|
|
|
|
|
module.exports = ContactsController = {
|
|
|
|
getContacts(req, res, next) {
|
2021-07-28 04:51:20 -04:00
|
|
|
const user_id = SessionManager.getLoggedInUserId(req.session)
|
2021-04-14 09:17:21 -04:00
|
|
|
return ContactManager.getContactIds(
|
|
|
|
user_id,
|
|
|
|
{ limit: 50 },
|
|
|
|
function (error, contact_ids) {
|
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
return UserGetter.getUsers(
|
|
|
|
contact_ids,
|
|
|
|
{
|
|
|
|
email: 1,
|
|
|
|
first_name: 1,
|
|
|
|
last_name: 1,
|
2021-04-27 03:52:58 -04:00
|
|
|
holdingAccount: 1,
|
2021-04-14 09:17:21 -04:00
|
|
|
},
|
|
|
|
function (error, contacts) {
|
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
// UserGetter.getUsers may not preserve order so put them back in order
|
|
|
|
const positions = {}
|
|
|
|
for (let i = 0; i < contact_ids.length; i++) {
|
|
|
|
const contact_id = contact_ids[i]
|
|
|
|
positions[contact_id] = i
|
|
|
|
}
|
|
|
|
contacts.sort(
|
|
|
|
(a, b) =>
|
|
|
|
positions[a._id != null ? a._id.toString() : undefined] -
|
|
|
|
positions[b._id != null ? b._id.toString() : undefined]
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
// Don't count holding accounts to discourage users from repeating mistakes (mistyped or wrong emails, etc)
|
|
|
|
contacts = contacts.filter(c => !c.holdingAccount)
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
contacts = contacts.map(ContactsController._formatContact)
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
return Modules.hooks.fire(
|
|
|
|
'getContacts',
|
|
|
|
user_id,
|
|
|
|
contacts,
|
|
|
|
function (error, additional_contacts) {
|
|
|
|
if (error != null) {
|
|
|
|
return next(error)
|
|
|
|
}
|
|
|
|
contacts = contacts.concat(
|
|
|
|
...Array.from(additional_contacts || [])
|
|
|
|
)
|
2022-01-17 05:19:53 -05:00
|
|
|
return res.json({
|
2021-04-27 03:52:58 -04:00
|
|
|
contacts,
|
2021-04-14 09:17:21 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
_formatContact(contact) {
|
|
|
|
return {
|
|
|
|
id: contact._id != null ? contact._id.toString() : undefined,
|
|
|
|
email: contact.email || '',
|
|
|
|
first_name: contact.first_name || '',
|
|
|
|
last_name: contact.last_name || '',
|
2021-04-27 03:52:58 -04:00
|
|
|
type: 'user',
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|