From 887a404fdda1e06af987db33e6f44b4bb662fef6 Mon Sep 17 00:00:00 2001 From: David <33458145+davidmcpowell@users.noreply.github.com> Date: Wed, 6 Mar 2024 11:17:21 +0000 Subject: [PATCH] Merge pull request #17384 from overleaf/dp-mongoose-callback-publisher-helper Promisify Publisher acceptance test helper GitOrigin-RevId: cce447234e32bfb93f8ce30deaf7fa21838e9176 --- .../test/acceptance/src/helpers/Publisher.js | 42 ++++++++++++++----- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/services/web/test/acceptance/src/helpers/Publisher.js b/services/web/test/acceptance/src/helpers/Publisher.js index baf83b041b..a631db6f0d 100644 --- a/services/web/test/acceptance/src/helpers/Publisher.js +++ b/services/web/test/acceptance/src/helpers/Publisher.js @@ -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