2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
max-len,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
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 = {
|
2019-05-29 05:21:06 -04:00
|
|
|
getUsersSubscription(user_or_id, callback) {
|
2019-08-28 08:59:41 -04:00
|
|
|
const user_id = SubscriptionLocator._getUserId(user_or_id)
|
2021-12-17 07:33:17 -05:00
|
|
|
Subscription.findOne({ admin_id: user_id }, function (err, subscription) {
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ user_id }, 'got users subscription')
|
2021-12-17 07:33:17 -05:00
|
|
|
callback(err, subscription)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
2020-02-03 09:11:23 -05:00
|
|
|
getUserIndividualSubscription(user_or_id, callback) {
|
|
|
|
const user_id = SubscriptionLocator._getUserId(user_or_id)
|
2021-12-17 07:33:17 -05:00
|
|
|
Subscription.findOne(
|
2020-02-03 09:11:23 -05:00
|
|
|
{ admin_id: user_id, groupPlan: false },
|
2021-04-14 09:17:21 -04:00
|
|
|
function (err, subscription) {
|
2022-05-16 08:38:18 -04:00
|
|
|
logger.debug({ user_id }, 'got users individual subscription')
|
2021-12-17 07:33:17 -05:00
|
|
|
callback(err, subscription)
|
2020-02-03 09:11:23 -05:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
getManagedGroupSubscriptions(user_or_id, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-12-17 07:33:17 -05:00
|
|
|
Subscription.find({
|
2019-05-29 05:21:06 -04:00
|
|
|
manager_ids: user_or_id,
|
2021-04-27 03:52:58 -04:00
|
|
|
groupPlan: true,
|
2019-05-29 05:21:06 -04:00
|
|
|
})
|
|
|
|
.populate('admin_id')
|
|
|
|
.exec(callback)
|
|
|
|
},
|
|
|
|
|
|
|
|
getMemberSubscriptions(user_or_id, callback) {
|
2019-08-28 08:59:41 -04:00
|
|
|
const user_id = SubscriptionLocator._getUserId(user_or_id)
|
2021-12-17 07:33:17 -05:00
|
|
|
Subscription.find({ member_ids: user_id })
|
2019-05-29 05:21:06 -04:00
|
|
|
.populate('admin_id')
|
|
|
|
.exec(callback)
|
|
|
|
},
|
|
|
|
|
|
|
|
getSubscription(subscription_id, callback) {
|
2021-12-17 07:33:17 -05:00
|
|
|
Subscription.findOne({ _id: subscription_id }, callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
getSubscriptionByMemberIdAndId(user_id, subscription_id, callback) {
|
2021-12-17 07:33:17 -05:00
|
|
|
Subscription.findOne(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ member_ids: user_id, _id: subscription_id },
|
|
|
|
{ _id: 1 },
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
getGroupSubscriptionsMemberOf(user_id, callback) {
|
2021-12-17 07:33:17 -05:00
|
|
|
Subscription.find(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ member_ids: user_id },
|
|
|
|
{ _id: 1, planCode: 1 },
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
_getUserId(user_or_id) {
|
|
|
|
if (user_or_id != null && user_or_id._id != null) {
|
|
|
|
return user_or_id._id
|
|
|
|
} else if (user_or_id != null) {
|
|
|
|
return user_or_id
|
|
|
|
}
|
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),
|
2019-08-28 08:59:41 -04:00
|
|
|
}
|
|
|
|
module.exports = SubscriptionLocator
|