InstitutionsController.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. const logger = require('logger-sharelatex')
  2. const UserGetter = require('../User/UserGetter')
  3. const { addAffiliation } = require('../Institutions/InstitutionsAPI')
  4. const FeaturesUpdater = require('../Subscription/FeaturesUpdater')
  5. const async = require('async')
  6. const ASYNC_AFFILIATIONS_LIMIT = 10
  7. module.exports = {
  8. confirmDomain(req, res, next) {
  9. const { hostname } = req.body
  10. affiliateUsers(hostname, function(error) {
  11. if (error) {
  12. return next(error)
  13. }
  14. res.sendStatus(200)
  15. })
  16. }
  17. }
  18. var affiliateUsers = function(hostname, callback) {
  19. const reversedHostname = hostname
  20. .trim()
  21. .split('')
  22. .reverse()
  23. .join('')
  24. UserGetter.getUsersByHostname(hostname, { _id: 1, emails: 1 }, function(
  25. error,
  26. users
  27. ) {
  28. if (error) {
  29. logger.warn({ error }, 'problem fetching users by hostname')
  30. return callback(error)
  31. }
  32. async.mapLimit(
  33. users,
  34. ASYNC_AFFILIATIONS_LIMIT,
  35. (user, innerCallback) =>
  36. affiliateUserByReversedHostname(user, reversedHostname, innerCallback),
  37. callback
  38. )
  39. })
  40. }
  41. var affiliateUserByReversedHostname = function(
  42. user,
  43. reversedHostname,
  44. callback
  45. ) {
  46. const matchingEmails = user.emails.filter(
  47. email => email.reversedHostname === reversedHostname
  48. )
  49. async.mapSeries(
  50. matchingEmails,
  51. (email, innerCallback) =>
  52. addAffiliation(
  53. user._id,
  54. email.email,
  55. { confirmedAt: email.confirmedAt },
  56. error => {
  57. if (error) {
  58. logger.warn(
  59. { error },
  60. 'problem adding affiliation while confirming hostname'
  61. )
  62. return innerCallback(error)
  63. }
  64. FeaturesUpdater.refreshFeatures(user._id, innerCallback)
  65. }
  66. ),
  67. callback
  68. )
  69. }