mirror of
https://github.com/overleaf/overleaf.git
synced 2025-02-16 23:03:56 +00:00
Merge pull request #17384 from overleaf/dp-mongoose-callback-publisher-helper
Promisify Publisher acceptance test helper GitOrigin-RevId: cce447234e32bfb93f8ce30deaf7fa21838e9176
This commit is contained in:
parent
ac48d81987
commit
887a404fdd
1 changed files with 31 additions and 11 deletions
|
@ -1,9 +1,10 @@
|
|||
const { ObjectId } = require('mongodb')
|
||||
const PublisherModel = require('../../../../app/src/models/Publisher').Publisher
|
||||
const { callbackify } = require('util')
|
||||
|
||||
let count = parseInt(Math.random() * 999999)
|
||||
|
||||
class Publisher {
|
||||
class PromisifiedPublisher {
|
||||
constructor(options = {}) {
|
||||
this.slug = options.slug || `publisher-slug-${count}`
|
||||
this.managerIds = []
|
||||
|
@ -11,22 +12,41 @@ class Publisher {
|
|||
count += 1
|
||||
}
|
||||
|
||||
ensureExists(callback) {
|
||||
async ensureExists() {
|
||||
const filter = { slug: this.slug }
|
||||
const options = { upsert: true, new: true, setDefaultsOnInsert: true }
|
||||
PublisherModel.findOneAndUpdate(filter, {}, options, (error, publisher) => {
|
||||
this._id = publisher._id
|
||||
callback(error)
|
||||
})
|
||||
const publisher = await PublisherModel.findOneAndUpdate(
|
||||
filter,
|
||||
{},
|
||||
options
|
||||
).exec()
|
||||
|
||||
this._id = publisher._id
|
||||
}
|
||||
|
||||
setManagerIds(managerIds, callback) {
|
||||
return PublisherModel.findOneAndUpdate(
|
||||
async setManagerIds(managerIds) {
|
||||
return await PublisherModel.findOneAndUpdate(
|
||||
{ _id: new ObjectId(this._id) },
|
||||
{ managerIds },
|
||||
callback
|
||||
)
|
||||
{ managerIds }
|
||||
).exec()
|
||||
}
|
||||
}
|
||||
|
||||
class Publisher extends PromisifiedPublisher {}
|
||||
Publisher.promises = class extends PromisifiedPublisher {}
|
||||
|
||||
// callbackify publisher class methods
|
||||
const nonPromiseMethods = ['constructor']
|
||||
Object.getOwnPropertyNames(PromisifiedPublisher.prototype).forEach(
|
||||
methodName => {
|
||||
const method = PromisifiedPublisher.prototype[methodName]
|
||||
if (
|
||||
typeof method === 'function' &&
|
||||
!nonPromiseMethods.includes(methodName)
|
||||
) {
|
||||
Publisher.prototype[methodName] = callbackify(method)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
module.exports = Publisher
|
||||
|
|
Loading…
Reference in a new issue