Institution.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. const mongoose = require('../infrastructure/Mongoose')
  2. const { Schema } = mongoose
  3. const { ObjectId } = Schema
  4. const settings = require('settings-sharelatex')
  5. const logger = require('logger-sharelatex')
  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(url, (error, response, body) => {
  19. let parsedBody
  20. try {
  21. parsedBody = JSON.parse(body)
  22. } catch (error1) {
  23. // log error and carry on without v1 data
  24. error = error1
  25. logger.err(
  26. { model: 'Institution', v1Id: this.v1Id, error },
  27. '[fetchV1DataError]'
  28. )
  29. }
  30. this.name = parsedBody != null ? parsedBody.name : undefined
  31. this.countryCode = parsedBody != null ? parsedBody.country_code : undefined
  32. this.departments = parsedBody != null ? parsedBody.departments : undefined
  33. this.portalSlug = parsedBody != null ? parsedBody.portal_slug : undefined
  34. callback(null, this)
  35. })
  36. })
  37. exports.Institution = mongoose.model('Institution', InstitutionSchema)
  38. exports.InstitutionSchema = InstitutionSchema