Merge pull request #17079 from overleaf/dp-mongoose-callback-subscription-locator

Promisify SubscriptionLocator and SubscriptionLocatorTests

GitOrigin-RevId: 9956a72d0cd94cb0b89da0fc1ec9c0e06fdcfeea
This commit is contained in:
David 2024-02-28 11:20:20 +00:00 committed by Copybot
parent 0827139e48
commit 6a96e160d9
2 changed files with 112 additions and 152 deletions

View file

@ -1,126 +1,115 @@
const { promisify } = require('util') const { callbackifyAll } = require('@overleaf/promise-utils')
const { Subscription } = require('../../models/Subscription') const { Subscription } = require('../../models/Subscription')
const { DeletedSubscription } = require('../../models/DeletedSubscription') const { DeletedSubscription } = require('../../models/DeletedSubscription')
const logger = require('@overleaf/logger') const logger = require('@overleaf/logger')
require('./GroupPlansData') // make sure dynamic group plans are loaded require('./GroupPlansData') // make sure dynamic group plans are loaded
const SubscriptionLocator = { const SubscriptionLocator = {
getUsersSubscription(userOrId, callback) { async getUsersSubscription(userOrId) {
const userId = SubscriptionLocator._getUserId(userOrId) const userId = SubscriptionLocator._getUserId(userOrId)
Subscription.findOne({ admin_id: userId }, function (err, subscription) { const subscription = await Subscription.findOne({ admin_id: userId }).exec()
logger.debug({ userId }, 'got users subscription') logger.debug({ userId }, 'got users subscription')
callback(err, subscription) return subscription
})
}, },
getUserIndividualSubscription(userOrId, callback) { async getUserIndividualSubscription(userOrId) {
const userId = SubscriptionLocator._getUserId(userOrId) const userId = SubscriptionLocator._getUserId(userOrId)
Subscription.findOne( const subscription = await Subscription.findOne({
{ admin_id: userId, groupPlan: false }, admin_id: userId,
function (err, subscription) { groupPlan: false,
logger.debug({ userId }, 'got users individual subscription') }).exec()
callback(err, subscription) logger.debug({ userId }, 'got users individual subscription')
} return subscription
)
}, },
getManagedGroupSubscriptions(userOrId, callback) { async getManagedGroupSubscriptions(userOrId) {
Subscription.find({ return await Subscription.find({
manager_ids: userOrId, manager_ids: userOrId,
groupPlan: true, groupPlan: true,
}) })
.populate('admin_id') .populate('admin_id')
.exec(callback) .exec()
}, },
getMemberSubscriptions(userOrId, callback) { async getMemberSubscriptions(userOrId) {
const userId = SubscriptionLocator._getUserId(userOrId) const userId = SubscriptionLocator._getUserId(userOrId)
Subscription.find({ member_ids: userId }) return await Subscription.find({ member_ids: userId })
.populate('admin_id') .populate('admin_id')
.exec(callback) .exec()
}, },
getAdminEmail(subscriptionId, callback) { async getAdminEmail(subscriptionId) {
Subscription.findById(subscriptionId) const subscription = await Subscription.findById(subscriptionId)
.populate('admin_id', 'email') .populate('admin_id', 'email')
.exec((err, subscription) => { .exec()
if (err) {
return callback(err) return subscription?.admin_id?.email
}
callback(err, subscription?.admin_id?.email)
})
}, },
getAdminEmailAndName(subscriptionId, callback) { async getAdminEmailAndName(subscriptionId) {
Subscription.findById(subscriptionId) const subscription = await Subscription.findById(subscriptionId)
.populate('admin_id', ['email', 'first_name', 'last_name']) .populate('admin_id', ['email', 'first_name', 'last_name'])
.exec((err, subscription) => { .exec()
if (err) {
return callback(err) return subscription?.admin_id
}
callback(err, subscription?.admin_id)
})
}, },
hasRecurlyGroupSubscription(userOrId, callback) { async hasRecurlyGroupSubscription(userOrId) {
const userId = SubscriptionLocator._getUserId(userOrId) const userId = SubscriptionLocator._getUserId(userOrId)
Subscription.exists( return await Subscription.exists({
{ groupPlan: true,
groupPlan: true, recurlySubscription_id: { $exists: true },
recurlySubscription_id: { $exists: true }, $or: [
$or: [ { member_ids: userId },
{ member_ids: userId }, { manager_ids: userId },
{ manager_ids: userId }, { admin_id: userId },
{ admin_id: userId }, ],
], }).exec()
},
callback
)
}, },
getSubscription(subscriptionId, callback) { async getSubscription(subscriptionId) {
Subscription.findOne({ _id: subscriptionId }, callback) return await Subscription.findOne({ _id: subscriptionId }).exec()
}, },
getSubscriptionByMemberIdAndId(userId, subscriptionId, callback) { async getSubscriptionByMemberIdAndId(userId, subscriptionId) {
Subscription.findOne( return await Subscription.findOne(
{ member_ids: userId, _id: subscriptionId }, { member_ids: userId, _id: subscriptionId },
{ _id: 1 }, { _id: 1 }
callback ).exec()
},
async getGroupSubscriptionsMemberOf(userId) {
return await Subscription.find(
{ member_ids: userId },
{ _id: 1, planCode: 1 }
) )
}, },
getGroupSubscriptionsMemberOf(userId, callback) { async getGroupsWithEmailInvite(email) {
Subscription.find({ member_ids: userId }, { _id: 1, planCode: 1 }, callback) return await Subscription.find({ invited_emails: email }).exec()
}, },
getGroupsWithEmailInvite(email, callback) { async getGroupsWithTeamInvitesEmail(email) {
Subscription.find({ invited_emails: email }, callback) return await Subscription.find(
},
getGroupsWithTeamInvitesEmail(email, callback) {
Subscription.find(
{ teamInvites: { $elemMatch: { email } } }, { teamInvites: { $elemMatch: { email } } },
{ teamInvites: 1 }, { teamInvites: 1 }
callback ).exec()
)
}, },
getGroupWithV1Id(v1TeamId, callback) { async getGroupWithV1Id(v1TeamId) {
Subscription.findOne({ 'overleaf.id': v1TeamId }, callback) return await Subscription.findOne({ 'overleaf.id': v1TeamId }).exec()
}, },
getUserDeletedSubscriptions(userId, callback) { async getUserDeletedSubscriptions(userId) {
DeletedSubscription.find({ 'subscription.admin_id': userId }, callback) return await DeletedSubscription.find({
'subscription.admin_id': userId,
}).exec()
}, },
getDeletedSubscription(subscriptionId, callback) { async getDeletedSubscription(subscriptionId) {
DeletedSubscription.findOne( return await DeletedSubscription.findOne({
{ 'subscription._id': subscriptionId,
'subscription._id': subscriptionId, }).exec()
},
callback
)
}, },
_getUserId(userOrId) { _getUserId(userOrId) {
@ -132,37 +121,7 @@ const SubscriptionLocator = {
}, },
} }
SubscriptionLocator.promises = { module.exports = {
getUsersSubscription: promisify(SubscriptionLocator.getUsersSubscription), ...callbackifyAll(SubscriptionLocator),
getUserIndividualSubscription: promisify( promises: SubscriptionLocator,
SubscriptionLocator.getUserIndividualSubscription
),
getManagedGroupSubscriptions: promisify(
SubscriptionLocator.getManagedGroupSubscriptions
),
getMemberSubscriptions: promisify(SubscriptionLocator.getMemberSubscriptions),
getAdminEmail: promisify(SubscriptionLocator.getAdminEmail),
getAdminEmailAndName: promisify(SubscriptionLocator.getAdminEmailAndName),
getSubscription: promisify(SubscriptionLocator.getSubscription),
getSubscriptionByMemberIdAndId: promisify(
SubscriptionLocator.getSubscriptionByMemberIdAndId
),
getGroupSubscriptionsMemberOf: promisify(
SubscriptionLocator.getGroupSubscriptionsMemberOf
),
getGroupsWithEmailInvite: promisify(
SubscriptionLocator.getGroupsWithEmailInvite
),
getGroupsWithTeamInvitesEmail: promisify(
SubscriptionLocator.getGroupsWithTeamInvitesEmail
),
getGroupWithV1Id: promisify(SubscriptionLocator.getGroupWithV1Id),
getUserDeletedSubscriptions: promisify(
SubscriptionLocator.getUserDeletedSubscriptions
),
getDeletedSubscription: promisify(SubscriptionLocator.getDeletedSubscription),
hasRecurlyGroupSubscription: promisify(
SubscriptionLocator.hasRecurlyGroupSubscription
),
} }
module.exports = SubscriptionLocator

View file

@ -2,22 +2,29 @@ const SandboxedModule = require('sandboxed-module')
const sinon = require('sinon') const sinon = require('sinon')
const modulePath = const modulePath =
'../../../../app/src/Features/Subscription/SubscriptionLocator' '../../../../app/src/Features/Subscription/SubscriptionLocator'
const { expect } = require('chai')
describe('Subscription Locator Tests', function () { describe('Subscription Locator Tests', function () {
beforeEach(function () { beforeEach(function () {
this.user = { _id: '5208dd34438842e2db333333' } this.user = { _id: '5208dd34438842e2db333333' }
this.subscription = { hello: 'world' } this.subscription = { hello: 'world' }
this.Subscription = { this.Subscription = {
findOne: sinon.stub(), findOne: sinon.stub().returns({
find: sinon.stub(), exec: sinon.stub().resolves(),
}),
find: sinon.stub().returns({
exec: sinon.stub().resolves(),
}),
} }
this.DeletedSubscription = { this.DeletedSubscription = {
findOne: sinon.stub().yields(), findOne: sinon.stub().returns({
find: sinon.stub().yields(), exec: sinon.stub().resolves(),
} }),
this.SSOConfig = { find: sinon.stub().returns({
findById: sinon.stub().yields(), exec: sinon.stub().resolves(),
}),
} }
this.SubscriptionLocator = SandboxedModule.require(modulePath, { this.SubscriptionLocator = SandboxedModule.require(modulePath, {
requires: { requires: {
'./GroupPlansData': {}, './GroupPlansData': {},
@ -35,45 +42,39 @@ describe('Subscription Locator Tests', function () {
}) })
describe('finding users subscription', function () { describe('finding users subscription', function () {
it('should send the users features', function (done) { it('should send the users features', async function () {
this.Subscription.findOne.callsArgWith(1, null, this.subscription) this.Subscription.findOne.returns({
this.SubscriptionLocator.getUsersSubscription( exec: sinon.stub().resolves(this.subscription),
this.user, })
(err, subscription) => { const subscription =
if (err) return done(err) await this.SubscriptionLocator.promises.getUsersSubscription(this.user)
this.Subscription.findOne this.Subscription.findOne
.calledWith({ admin_id: this.user._id }) .calledWith({ admin_id: this.user._id })
.should.equal(true) .should.equal(true)
subscription.should.equal(this.subscription) subscription.should.equal(this.subscription)
done()
}
)
}) })
it('should error if not found', function (done) { it('should error if not found', async function () {
this.Subscription.findOne.callsArgWith(1, 'not found') this.Subscription.findOne.returns({
this.SubscriptionLocator.getUsersSubscription( exec: sinon.stub().rejects('not found'),
this.user, })
(err, subscription) => { await expect(
err.should.exist this.SubscriptionLocator.promises.getUsersSubscription(this.user)
done() ).to.be.rejected
}
)
}) })
it('should take a user id rather than the user object', function (done) { it('should take a user id rather than the user object', async function () {
this.Subscription.findOne.callsArgWith(1, null, this.subscription) this.Subscription.findOne.returns({
this.SubscriptionLocator.getUsersSubscription( exec: sinon.stub().resolves(this.subscription),
this.user._id, })
(err, subscription) => { const subscription =
if (err) return done(err) await this.SubscriptionLocator.promises.getUsersSubscription(
this.Subscription.findOne this.user._id
.calledWith({ admin_id: this.user._id }) )
.should.equal(true) this.Subscription.findOne
subscription.should.equal(this.subscription) .calledWith({ admin_id: this.user._id })
done() .should.equal(true)
} subscription.should.equal(this.subscription)
)
}) })
}) })
}) })