Institution.js 1.5 KB

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