Publisher.mjs 1017 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import mongodb from 'mongodb-legacy'
  2. import { Publisher as PublisherModel } from '../../../../app/src/models/Publisher.mjs'
  3. import { callbackifyClass } from '@overleaf/promise-utils'
  4. const { ObjectId } = mongodb
  5. let count = parseInt(Math.random() * 999999)
  6. class PromisifiedPublisher {
  7. constructor(options = {}) {
  8. this.slug = options.slug || `publisher-slug-${count}`
  9. this.managerIds = []
  10. count += 1
  11. }
  12. async ensureExists() {
  13. const filter = { slug: this.slug }
  14. const options = { upsert: true, new: true, setDefaultsOnInsert: true }
  15. const publisher = await PublisherModel.findOneAndUpdate(
  16. filter,
  17. {},
  18. options
  19. ).exec()
  20. this._id = publisher._id
  21. }
  22. async setManagerIds(managerIds) {
  23. return await PublisherModel.findOneAndUpdate(
  24. { _id: new ObjectId(this._id) },
  25. { managerIds }
  26. ).exec()
  27. }
  28. }
  29. const Publisher = callbackifyClass(PromisifiedPublisher)
  30. Publisher.promises = class extends PromisifiedPublisher {}
  31. export default Publisher