Institution.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 { promisify } = require('@overleaf/promise-utils')
  7. const { fetchJson } = require('@overleaf/fetch-utils')
  8. const InstitutionSchema = new Schema(
  9. {
  10. v1Id: { type: Number, required: true },
  11. managerIds: [{ type: ObjectId, ref: 'User' }],
  12. metricsEmail: {
  13. optedOutUserIds: [{ type: ObjectId, ref: 'User' }],
  14. lastSent: { type: Date },
  15. },
  16. groupPolicy: { type: ObjectId, ref: 'GroupPolicy' },
  17. },
  18. { minimize: false }
  19. )
  20. // fetch institution's data from v1 API. Errors are ignored
  21. InstitutionSchema.method('fetchV1Data', async function (callback) {
  22. const url = `${settings.apis.v1.url}/universities/list/${this.v1Id}`
  23. try {
  24. const parsedBody = await fetchJson(url)
  25. this.name = parsedBody != null ? parsedBody.name : undefined
  26. this.countryCode = parsedBody != null ? parsedBody.country_code : undefined
  27. this.departments = parsedBody != null ? parsedBody.departments : undefined
  28. this.portalSlug = parsedBody != null ? parsedBody.portal_slug : undefined
  29. this.enterpriseCommons =
  30. parsedBody != null ? parsedBody.enterprise_commons : undefined
  31. } catch (error) {
  32. // log error and carry on without v1 data
  33. logger.err(
  34. { model: 'Institution', v1Id: this.v1Id, error },
  35. '[fetchV1DataError]'
  36. )
  37. }
  38. callback(null, this)
  39. })
  40. InstitutionSchema.method(
  41. 'fetchV1DataPromise',
  42. promisify(InstitutionSchema.methods.fetchV1Data)
  43. )
  44. exports.Institution = mongoose.model('Institution', InstitutionSchema)
  45. exports.InstitutionSchema = InstitutionSchema