2019-08-28 08:59:41 -04:00
|
|
|
const { promisify } = require('util')
|
2019-05-29 05:21:06 -04:00
|
|
|
const { Subscription } = require('../../models/Subscription')
|
2019-09-10 10:03:29 -04:00
|
|
|
const { DeletedSubscription } = require('../../models/DeletedSubscription')
|
2021-11-10 08:40:18 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-01-07 06:03:14 -05:00
|
|
|
require('./GroupPlansData') // make sure dynamic group plans are loaded
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-08-28 08:59:41 -04:00
|
|
|
const SubscriptionLocator = {
|
2022-05-17 06:10:19 -04:00
|
|
|
getUsersSubscription(userOrId, callback) {
|
|
|
|
const userId = SubscriptionLocator._getUserId(userOrId)
|
|
|
|
Subscription.findOne({ admin_id: userId }, function (err, subscription) {
|
|
|
|
logger.debug({ userId }, 'got users subscription')
|
2021-12-17 07:33:17 -05:00
|
|
|
callback(err, subscription)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
2022-05-17 06:10:19 -04:00
|
|
|
getUserIndividualSubscription(userOrId, callback) {
|
|
|
|
const userId = SubscriptionLocator._getUserId(userOrId)
|
2021-12-17 07:33:17 -05:00
|
|
|
Subscription.findOne(
|
2022-05-17 06:10:19 -04:00
|
|
|
{ admin_id: userId, groupPlan: false },
|
2021-04-14 09:17:21 -04:00
|
|
|
function (err, subscription) {
|
2022-05-17 06:10:19 -04:00
|
|
|
logger.debug({ userId }, 'got users individual subscription')
|
2021-12-17 07:33:17 -05:00
|
|
|
callback(err, subscription)
|
2020-02-03 09:11:23 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2022-05-17 06:10:19 -04:00
|
|
|
getManagedGroupSubscriptions(userOrId, callback) {
|
2021-12-17 07:33:17 -05:00
|
|
|
Subscription.find({
|
2022-05-17 06:10:19 -04:00
|
|
|
manager_ids: userOrId,
|
2021-04-27 03:52:58 -04:00
|
|
|
groupPlan: true,
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
.populate('admin_id')
|
|
|
|
.exec(callback)
|
|
|
|
},
|
|
|
|
|
2022-05-17 06:10:19 -04:00
|
|
|
getMemberSubscriptions(userOrId, callback) {
|
|
|
|
const userId = SubscriptionLocator._getUserId(userOrId)
|
|
|
|
Subscription.find({ member_ids: userId })
|
2019-05-29 05:21:06 -04:00
|
|
|
.populate('admin_id')
|
|
|
|
.exec(callback)
|
|
|
|
},
|
|
|
|
|
2022-06-22 05:34:25 -04:00
|
|
|
hasRecurlyGroupSubscription(userOrId, callback) {
|
|
|
|
const userId = SubscriptionLocator._getUserId(userOrId)
|
|
|
|
Subscription.exists(
|
|
|
|
{
|
|
|
|
groupPlan: true,
|
|
|
|
recurlySubscription_id: { $exists: true },
|
|
|
|
$or: [
|
|
|
|
{ member_ids: userId },
|
|
|
|
{ manager_ids: userId },
|
|
|
|
{ admin_id: userId },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2022-05-17 06:10:19 -04:00
|
|
|
getSubscription(subscriptionId, callback) {
|
|
|
|
Subscription.findOne({ _id: subscriptionId }, callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
2022-05-17 06:10:19 -04:00
|
|
|
getSubscriptionByMemberIdAndId(userId, subscriptionId, callback) {
|
2021-12-17 07:33:17 -05:00
|
|
|
Subscription.findOne(
|
2022-05-17 06:10:19 -04:00
|
|
|
{ member_ids: userId, _id: subscriptionId },
|
2019-05-29 05:21:06 -04:00
|
|
|
{ _id: 1 },
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2022-05-17 06:10:19 -04:00
|
|
|
getGroupSubscriptionsMemberOf(userId, callback) {
|
|
|
|
Subscription.find({ member_ids: userId }, { _id: 1, planCode: 1 }, callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
getGroupsWithEmailInvite(email, callback) {
|
2021-12-17 07:33:17 -05:00
|
|
|
Subscription.find({ invited_emails: email }, callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
getGroupWithV1Id(v1TeamId, callback) {
|
2021-12-17 07:33:17 -05:00
|
|
|
Subscription.findOne({ 'overleaf.id': v1TeamId }, callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
2019-09-10 10:03:29 -04:00
|
|
|
getUserDeletedSubscriptions(userId, callback) {
|
|
|
|
DeletedSubscription.find({ 'subscription.admin_id': userId }, callback)
|
|
|
|
},
|
|
|
|
|
|
|
|
getDeletedSubscription(subscriptionId, callback) {
|
|
|
|
DeletedSubscription.findOne(
|
|
|
|
{
|
2021-04-27 03:52:58 -04:00
|
|
|
'subscription._id': subscriptionId,
|
2019-09-10 10:03:29 -04:00
|
|
|
},
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2022-05-17 06:10:19 -04:00
|
|
|
_getUserId(userOrId) {
|
|
|
|
if (userOrId && userOrId._id) {
|
|
|
|
return userOrId._id
|
|
|
|
} else if (userOrId) {
|
|
|
|
return userOrId
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-08-28 08:59:41 -04:00
|
|
|
|
|
|
|
SubscriptionLocator.promises = {
|
|
|
|
getUsersSubscription: promisify(SubscriptionLocator.getUsersSubscription),
|
2020-02-03 09:11:23 -05:00
|
|
|
getUserIndividualSubscription: promisify(
|
|
|
|
SubscriptionLocator.getUserIndividualSubscription
|
|
|
|
),
|
2019-08-28 08:59:41 -04:00
|
|
|
getManagedGroupSubscriptions: promisify(
|
|
|
|
SubscriptionLocator.getManagedGroupSubscriptions
|
|
|
|
),
|
|
|
|
getMemberSubscriptions: promisify(SubscriptionLocator.getMemberSubscriptions),
|
|
|
|
getSubscription: promisify(SubscriptionLocator.getSubscription),
|
|
|
|
getSubscriptionByMemberIdAndId: promisify(
|
|
|
|
SubscriptionLocator.getSubscriptionByMemberIdAndId
|
|
|
|
),
|
|
|
|
getGroupSubscriptionsMemberOf: promisify(
|
|
|
|
SubscriptionLocator.getGroupSubscriptionsMemberOf
|
|
|
|
),
|
|
|
|
getGroupsWithEmailInvite: promisify(
|
|
|
|
SubscriptionLocator.getGroupsWithEmailInvite
|
|
|
|
),
|
2019-09-10 10:03:29 -04:00
|
|
|
getGroupWithV1Id: promisify(SubscriptionLocator.getGroupWithV1Id),
|
|
|
|
getUserDeletedSubscriptions: promisify(
|
|
|
|
SubscriptionLocator.getUserDeletedSubscriptions
|
|
|
|
),
|
2021-04-27 03:52:58 -04:00
|
|
|
getDeletedSubscription: promisify(SubscriptionLocator.getDeletedSubscription),
|
2022-06-22 05:34:25 -04:00
|
|
|
hasRecurlyGroupSubscription: promisify(
|
|
|
|
SubscriptionLocator.hasRecurlyGroupSubscription
|
|
|
|
),
|
2019-08-28 08:59:41 -04:00
|
|
|
}
|
|
|
|
module.exports = SubscriptionLocator
|