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
|
2021-07-07 05:38:56 -04:00
|
|
|
const settings = require('@overleaf/settings')
|
2021-11-10 08:40:18 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2019-05-29 05:21:06 -04:00
|
|
|
const request = require('request')
|
|
|
|
|
|
|
|
const PublisherSchema = new Schema({
|
|
|
|
slug: { type: String, required: true },
|
2021-04-27 03:52:58 -04:00
|
|
|
managerIds: [{ type: ObjectId, ref: 'User' }],
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
// 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,
|
2021-04-27 03:52:58 -04:00
|
|
|
sendImmediately: true,
|
|
|
|
},
|
2022-03-07 09:13:00 -05:00
|
|
|
timeout: settings.apis.v1.timeout,
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
(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
|