Institution.mjs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import mongoose from '../infrastructure/Mongoose.js'
  2. import settings from '@overleaf/settings'
  3. import logger from '@overleaf/logger'
  4. import { promisify } from '@overleaf/promise-utils'
  5. import { fetchJson } from '@overleaf/fetch-utils'
  6. const { Schema } = mongoose
  7. const { ObjectId } = Schema
  8. export 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. export const Institution = mongoose.model('Institution', InstitutionSchema)