2018-11-30 08:03:35 -05:00
|
|
|
mongoose = require 'mongoose'
|
|
|
|
Schema = mongoose.Schema
|
|
|
|
ObjectId = Schema.ObjectId
|
|
|
|
settings = require 'settings-sharelatex'
|
2018-12-03 06:05:14 -05:00
|
|
|
logger = require 'logger-sharelatex'
|
2018-11-30 08:03:35 -05:00
|
|
|
request = require 'request'
|
|
|
|
|
|
|
|
PublisherSchema = new Schema
|
|
|
|
slug: { type: String, required: true }
|
|
|
|
managerIds: [ type:ObjectId, ref:'User' ]
|
|
|
|
|
|
|
|
# fetch publisher's (brand on v1) data from v1 API. Errors are ignored
|
|
|
|
PublisherSchema.method 'fetchV1Data', (callback = (error, publisher)->) ->
|
|
|
|
request {
|
|
|
|
baseUrl: settings.apis.v1.url
|
|
|
|
url: "/api/v2/brands/#{this.slug}"
|
|
|
|
method: 'GET'
|
|
|
|
auth:
|
|
|
|
user: settings.apis.v1.user
|
|
|
|
pass: settings.apis.v1.pass
|
|
|
|
sendImmediately: true
|
|
|
|
}, (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: 'Publisher', slug: this.slug, error }, '[fetchV1DataError]'
|
2018-11-30 08:03:35 -05:00
|
|
|
this.name = parsedBody?.name
|
|
|
|
this.partner = parsedBody?.partner
|
|
|
|
callback(null, this)
|
|
|
|
|
|
|
|
conn = mongoose.createConnection(settings.mongo.url, {
|
|
|
|
server: {poolSize: settings.mongo.poolSize || 10},
|
|
|
|
config: {autoIndex: false}
|
|
|
|
})
|
|
|
|
|
|
|
|
Publisher = conn.model 'Publisher', PublisherSchema
|
|
|
|
exports.Publisher = Publisher
|
|
|
|
exports.PublisherSchema = PublisherSchema
|