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 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
|
2021-04-14 09:17:21 -04:00
|
|
|
PublisherSchema.method('fetchV1Data', function (callback) {
|
2019-11-18 09:03:20 -05:00
|
|
|
request(
|
2019-05-29 05:21:06 -04:00
|
|
|
{
|
|
|
|
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) => {
|
|
|
|
let parsedBody
|
|
|
|
try {
|
|
|
|
parsedBody = JSON.parse(body)
|
|
|
|
} catch (error1) {
|
|
|
|
// log error and carry on without v1 data
|
|
|
|
error = error1
|
|
|
|
logger.err(
|
|
|
|
{ model: 'Publisher', slug: this.slug, error },
|
|
|
|
'[fetchV1DataError]'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
this.name = parsedBody != null ? parsedBody.name : undefined
|
|
|
|
this.partner = parsedBody != null ? parsedBody.partner : 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.Publisher = mongoose.model('Publisher', PublisherSchema)
|
2019-05-29 05:21:06 -04:00
|
|
|
exports.PublisherSchema = PublisherSchema
|