Institution.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const mongoose = require('../infrastructure/Mongoose')
  2. const { Schema } = mongoose
  3. const { ObjectId } = Schema
  4. const settings = require('@overleaf/settings')
  5. const logger = require('@overleaf/logger')
  6. const request = require('request')
  7. const InstitutionSchema = new Schema(
  8. {
  9. v1Id: { type: Number, required: true },
  10. managerIds: [{ type: ObjectId, ref: 'User' }],
  11. metricsEmail: {
  12. optedOutUserIds: [{ type: ObjectId, ref: 'User' }],
  13. lastSent: { type: Date },
  14. },
  15. },
  16. { minimize: false }
  17. )
  18. // fetch institution's data from v1 API. Errors are ignored
  19. InstitutionSchema.method('fetchV1Data', function (callback) {
  20. const url = `${settings.apis.v1.url}/universities/list/${this.v1Id}`
  21. request.get(
  22. { url, timeout: settings.apis.v1.timeout },
  23. (error, response, body) => {
  24. let parsedBody
  25. try {
  26. parsedBody = JSON.parse(body)
  27. } catch (error1) {
  28. // log error and carry on without v1 data
  29. error = error1
  30. logger.err(
  31. { model: 'Institution', v1Id: this.v1Id, error },
  32. '[fetchV1DataError]'
  33. )
  34. }
  35. this.name = parsedBody != null ? parsedBody.name : undefined
  36. this.countryCode =
  37. parsedBody != null ? parsedBody.country_code : undefined
  38. this.departments = parsedBody != null ? parsedBody.departments : undefined
  39. this.portalSlug = parsedBody != null ? parsedBody.portal_slug : undefined
  40. callback(null, this)
  41. }
  42. )
  43. })
  44. exports.Institution = mongoose.model('Institution', InstitutionSchema)
  45. exports.InstitutionSchema = InstitutionSchema