Publisher.js 803 B

1234567891011121314151617181920212223242526272829303132
  1. const { ObjectId } = require('mongodb')
  2. const PublisherModel = require('../../../../app/src/models/Publisher').Publisher
  3. let count = parseInt(Math.random() * 999999)
  4. class Publisher {
  5. constructor(options = {}) {
  6. this.slug = options.slug || `publisher-slug-${count}`
  7. this.managerIds = []
  8. count += 1
  9. }
  10. ensureExists(callback) {
  11. const filter = { slug: this.slug }
  12. const options = { upsert: true, new: true, setDefaultsOnInsert: true }
  13. PublisherModel.findOneAndUpdate(filter, {}, options, (error, publisher) => {
  14. this._id = publisher._id
  15. callback(error)
  16. })
  17. }
  18. setManagerIds(managerIds, callback) {
  19. return PublisherModel.findOneAndUpdate(
  20. { _id: ObjectId(this._id) },
  21. { managerIds },
  22. callback
  23. )
  24. }
  25. }
  26. module.exports = Publisher