Publisher.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const mongoose = require('../infrastructure/Mongoose')
  2. const { Schema } = mongoose
  3. const { ObjectId } = Schema
  4. const settings = require('@overleaf/settings')
  5. const logger = require('logger-sharelatex')
  6. const request = require('request')
  7. const PublisherSchema = new Schema({
  8. slug: { type: String, required: true },
  9. managerIds: [{ type: ObjectId, ref: 'User' }],
  10. })
  11. // fetch publisher's (brand on v1) data from v1 API. Errors are ignored
  12. PublisherSchema.method('fetchV1Data', function (callback) {
  13. request(
  14. {
  15. baseUrl: settings.apis.v1.url,
  16. url: `/api/v2/brands/${this.slug}`,
  17. method: 'GET',
  18. auth: {
  19. user: settings.apis.v1.user,
  20. pass: settings.apis.v1.pass,
  21. sendImmediately: true,
  22. },
  23. },
  24. (error, response, body) => {
  25. let parsedBody
  26. try {
  27. parsedBody = JSON.parse(body)
  28. } catch (error1) {
  29. // log error and carry on without v1 data
  30. error = error1
  31. logger.err(
  32. { model: 'Publisher', slug: this.slug, error },
  33. '[fetchV1DataError]'
  34. )
  35. }
  36. this.name = parsedBody != null ? parsedBody.name : undefined
  37. this.partner = parsedBody != null ? parsedBody.partner : undefined
  38. callback(null, this)
  39. }
  40. )
  41. })
  42. exports.Publisher = mongoose.model('Publisher', PublisherSchema)
  43. exports.PublisherSchema = PublisherSchema