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')
|
|
|
|
const Modules = require('../../infrastructure/Modules')
|
2023-10-19 07:38:17 -04:00
|
|
|
const { expressify } = require('@overleaf/promise-utils')
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2023-06-19 05:02:16 -04:00
|
|
|
function _formatContact(contact) {
|
|
|
|
return {
|
|
|
|
id: contact._id?.toString(),
|
|
|
|
email: contact.email || '',
|
|
|
|
first_name: contact.first_name || '',
|
|
|
|
last_name: contact.last_name || '',
|
|
|
|
type: 'user',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getContacts(req, res) {
|
|
|
|
const userId = SessionManager.getLoggedInUserId(req.session)
|
|
|
|
|
|
|
|
const contactIds = await ContactManager.promises.getContactIds(userId, {
|
|
|
|
limit: 50,
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2023-06-19 05:02:16 -04:00
|
|
|
let contacts = await UserGetter.promises.getUsers(contactIds, {
|
|
|
|
email: 1,
|
|
|
|
first_name: 1,
|
|
|
|
last_name: 1,
|
|
|
|
holdingAccount: 1,
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2023-06-19 05:02:16 -04:00
|
|
|
// UserGetter.getUsers may not preserve order so put them back in order
|
|
|
|
const positions = {}
|
|
|
|
for (let i = 0; i < contactIds.length; i++) {
|
|
|
|
const contactId = contactIds[i]
|
|
|
|
positions[contactId] = i
|
|
|
|
}
|
|
|
|
contacts.sort(
|
|
|
|
(a, b) => positions[a._id?.toString()] - positions[b._id?.toString()]
|
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2023-06-19 05:02:16 -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
|
|
|
|
2023-06-19 05:02:16 -04:00
|
|
|
contacts = contacts.map(_formatContact)
|
|
|
|
|
|
|
|
const additionalContacts = await Modules.promises.hooks.fire(
|
|
|
|
'getContacts',
|
|
|
|
userId,
|
|
|
|
contacts
|
|
|
|
)
|
|
|
|
|
|
|
|
contacts = contacts.concat(...(additionalContacts || []))
|
|
|
|
return res.json({
|
|
|
|
contacts,
|
|
|
|
})
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2023-06-19 05:02:16 -04:00
|
|
|
module.exports = {
|
|
|
|
getContacts: expressify(getContacts),
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|