overleaf/services/web/app/coffee/models/Institution.coffee
Hugh O'Brien 491c29bad1 Merge pull request #1569 from sharelatex/hb-v2-metrics-email-opt-out
v2 metrics email opt out and last sent tracking

GitOrigin-RevId: d45ac653c26e780dc380883c0ac1da7436bf8c2b
2019-03-11 11:03:39 +00:00

37 lines
1.3 KiB
CoffeeScript

mongoose = require 'mongoose'
Schema = mongoose.Schema
ObjectId = Schema.ObjectId
settings = require 'settings-sharelatex'
logger = require 'logger-sharelatex'
request = require 'request'
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', (callback = (error, institution)->) ->
url = "#{settings.apis.v1.url}/universities/list/#{this.v1Id}"
request.get url, (error, response, body) =>
try
parsedBody = JSON.parse(body)
catch error # log error and carry on without v1 data
logger.err { model: 'Institution', v1Id: this.v1Id, error }, '[fetchV1DataError]'
this.name = parsedBody?.name
this.countryCode = parsedBody?.country_code
this.departments = parsedBody?.departments
this.portalSlug = parsedBody?.portal_slug
callback(null, this)
conn = mongoose.createConnection(settings.mongo.url, {
server: {poolSize: settings.mongo.poolSize || 10},
config: {autoIndex: false}
})
Institution = conn.model 'Institution', InstitutionSchema
exports.Institution = Institution
exports.InstitutionSchema = InstitutionSchema