2019-11-18 09:03:20 -05:00
|
|
|
const mongoose = require('../infrastructure/Mongoose')
|
2019-05-29 05:21:06 -04:00
|
|
|
const { Schema } = mongoose
|
|
|
|
const { ObjectId } = Schema
|
|
|
|
const settings = require('settings-sharelatex')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const request = require('request')
|
|
|
|
|
|
|
|
const InstitutionSchema = new Schema({
|
|
|
|
v1Id: { type: Number, required: true },
|
|
|
|
managerIds: [{ type: ObjectId, ref: 'User' }],
|
|
|
|
metricsEmail: {
|
|
|
|
optedOutUserIds: [{ type: ObjectId, ref: 'User' }],
|
|
|
|
lastSent: { type: Date }
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// fetch institution's data from v1 API. Errors are ignored
|
|
|
|
InstitutionSchema.method('fetchV1Data', function(callback) {
|
|
|
|
const url = `${settings.apis.v1.url}/universities/list/${this.v1Id}`
|
2019-11-18 09:03:20 -05:00
|
|
|
request.get(url, (error, response, body) => {
|
2019-05-29 05:21:06 -04:00
|
|
|
let parsedBody
|
|
|
|
try {
|
|
|
|
parsedBody = JSON.parse(body)
|
|
|
|
} catch (error1) {
|
|
|
|
// log error and carry on without v1 data
|
|
|
|
error = error1
|
|
|
|
logger.err(
|
|
|
|
{ model: 'Institution', v1Id: this.v1Id, error },
|
|
|
|
'[fetchV1DataError]'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
this.name = parsedBody != null ? parsedBody.name : undefined
|
|
|
|
this.countryCode = parsedBody != null ? parsedBody.country_code : undefined
|
|
|
|
this.departments = parsedBody != null ? parsedBody.departments : undefined
|
|
|
|
this.portalSlug = parsedBody != null ? parsedBody.portal_slug : undefined
|
2019-11-18 09:03:20 -05:00
|
|
|
callback(null, this)
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-11-18 09:03:20 -05:00
|
|
|
exports.Institution = mongoose.model('Institution', InstitutionSchema)
|
2019-05-29 05:21:06 -04:00
|
|
|
exports.InstitutionSchema = InstitutionSchema
|