Publisher.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. const mongoose = require('../infrastructure/Mongoose')
  2. const { Schema } = mongoose
  3. const { ObjectId } = Schema
  4. const settings = require('@overleaf/settings')
  5. const logger = require('@overleaf/logger')
  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. timeout: settings.apis.v1.timeout,
  24. },
  25. (error, response, body) => {
  26. let parsedBody
  27. try {
  28. parsedBody = JSON.parse(body)
  29. } catch (error1) {
  30. // log error and carry on without v1 data
  31. error = error1
  32. logger.err(
  33. { model: 'Publisher', slug: this.slug, error },
  34. '[fetchV1DataError]'
  35. )
  36. }
  37. this.name = parsedBody != null ? parsedBody.name : undefined
  38. this.partner = parsedBody != null ? parsedBody.partner : undefined
  39. callback(null, this)
  40. }
  41. )
  42. })
  43. exports.Publisher = mongoose.model('Publisher', PublisherSchema)
  44. exports.PublisherSchema = PublisherSchema