2019-05-29 05:21:06 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
2020-12-15 05:23:54 -05:00
|
|
|
node/handle-callback-err,
|
2019-05-29 05:21:06 -04:00
|
|
|
max-len,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// 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')
|
2019-05-29 05:21:06 -04:00
|
|
|
const logger = require('logger-sharelatex')
|
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-04-14 09:17:21 -04:00
|
|
|
return Subscription.findOne(
|
|
|
|
{ admin_id: user_id },
|
|
|
|
function (err, subscription) {
|
|
|
|
logger.log({ user_id }, 'got users subscription')
|
|
|
|
return 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)
|
|
|
|
return Subscription.findOne(
|
|
|
|
{ admin_id: user_id, groupPlan: false },
|
2021-04-14 09:17:21 -04:00
|
|
|
function (err, subscription) {
|
2020-02-03 09:11:23 -05:00
|
|
|
logger.log({ user_id }, 'got users individual subscription')
|
|
|
|
return callback(err, subscription)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
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
|
|
|
}
|
2019-08-28 08:59:41 -04:00
|
|
|
const user_id = SubscriptionLocator._getUserId(user_or_id)
|
2019-05-29 05:21:06 -04:00
|
|
|
return Subscription.find({
|
|
|
|
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)
|
2019-05-29 05:21:06 -04:00
|
|
|
return Subscription.find({ member_ids: user_id })
|
|
|
|
.populate('admin_id')
|
|
|
|
.exec(callback)
|
|
|
|
},
|
|
|
|
|
|
|
|
getSubscription(subscription_id, callback) {
|
|
|
|
return Subscription.findOne({ _id: subscription_id }, callback)
|
|
|
|
},
|
|
|
|
|
|
|
|
getSubscriptionByMemberIdAndId(user_id, subscription_id, callback) {
|
|
|
|
return Subscription.findOne(
|
|
|
|
{ member_ids: user_id, _id: subscription_id },
|
|
|
|
{ _id: 1 },
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
getGroupSubscriptionsMemberOf(user_id, callback) {
|
|
|
|
return Subscription.find(
|
|
|
|
{ member_ids: user_id },
|
|
|
|
{ _id: 1, planCode: 1 },
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
getGroupsWithEmailInvite(email, callback) {
|
|
|
|
return Subscription.find({ invited_emails: email }, callback)
|
|
|
|
},
|
|
|
|
|
|
|
|
getGroupWithV1Id(v1TeamId, callback) {
|
|
|
|
return Subscription.findOne({ 'overleaf.id': v1TeamId }, callback)
|
|
|
|
},
|
|
|
|
|
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
|