Institution.mjs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import mongoose from '../infrastructure/Mongoose.mjs'
  2. import settings from '@overleaf/settings'
  3. import logger from '@overleaf/logger'
  4. import { callbackify } 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. async function fetchV1DataPromise() {
  22. const url = `${settings.apis.v1.url}/universities/list/${this.v1Id}`
  23. try {
  24. const parsedBody = await fetchJson(url)
  25. this.name = parsedBody?.name
  26. this.countryCode = parsedBody?.country_code
  27. this.departments = parsedBody?.departments
  28. this.portalSlug = parsedBody?.portal_slug
  29. this.enterpriseCommons = parsedBody?.enterprise_commons
  30. } catch (error) {
  31. // log error and carry on without v1 data
  32. logger.err(
  33. { model: 'Institution', v1Id: this.v1Id, error },
  34. '[fetchV1DataError]'
  35. )
  36. }
  37. return this
  38. }
  39. InstitutionSchema.method('fetchV1DataPromise', fetchV1DataPromise)
  40. InstitutionSchema.method('fetchV1Data', callbackify(fetchV1DataPromise))
  41. export const Institution = mongoose.model('Institution', InstitutionSchema)