2018-09-25 09:10:06 -04:00
|
|
|
mongoose = require 'mongoose'
|
|
|
|
Schema = mongoose.Schema
|
|
|
|
ObjectId = Schema.ObjectId
|
2018-11-16 04:02:57 -05:00
|
|
|
settings = require 'settings-sharelatex'
|
2018-12-03 06:05:14 -05:00
|
|
|
logger = require 'logger-sharelatex'
|
2018-11-16 04:02:57 -05:00
|
|
|
request = require 'request'
|
2018-09-25 09:10:06 -04:00
|
|
|
|
|
|
|
InstitutionSchema = new Schema
|
|
|
|
v1Id: { type: Number, required: true }
|
|
|
|
managerIds: [ type:ObjectId, ref:'User' ]
|
|
|
|
|
2018-11-16 04:02:57 -05:00
|
|
|
# fetch institution's data from v1 API. Errors are ignored
|
|
|
|
InstitutionSchema.method 'fetchV1Data', (callback = (error, institution)->) ->
|
|
|
|
url = "#{settings.apis.v1.url}/universities/list/#{this.v1Id}"
|
|
|
|
request.get url, (error, response, body) =>
|
2018-12-03 06:05:14 -05:00
|
|
|
try
|
|
|
|
parsedBody = JSON.parse(body)
|
|
|
|
catch error # log error and carry on without v1 data
|
|
|
|
logger.err { model: 'Institution', v1Id: this.v1Id, error }, '[fetchV1DataError]'
|
2018-11-16 04:02:57 -05:00
|
|
|
this.name = parsedBody?.name
|
|
|
|
this.countryCode = parsedBody?.country_code
|
|
|
|
this.departments = parsedBody?.departments
|
2018-11-16 04:05:13 -05:00
|
|
|
this.portalSlug = parsedBody?.portal_slug
|
2018-11-16 04:02:57 -05:00
|
|
|
callback(null, this)
|
|
|
|
|
2018-11-20 05:48:21 -05:00
|
|
|
conn = mongoose.createConnection(settings.mongo.url, {
|
|
|
|
server: {poolSize: settings.mongo.poolSize || 10},
|
|
|
|
config: {autoIndex: false}
|
|
|
|
})
|
|
|
|
|
|
|
|
Institution = conn.model 'Institution', InstitutionSchema
|
|
|
|
exports.Institution = Institution
|
2018-09-25 09:10:06 -04:00
|
|
|
exports.InstitutionSchema = InstitutionSchema
|