Merge pull request #17384 from overleaf/dp-mongoose-callback-publisher-helper

Promisify Publisher acceptance test helper

GitOrigin-RevId: cce447234e32bfb93f8ce30deaf7fa21838e9176
This commit is contained in:
David 2024-03-06 11:17:21 +00:00 committed by Copybot
parent ac48d81987
commit 887a404fdd

View file

@ -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