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
|
|
|
|
* DS103: Rewrite code to no longer use __guard__
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
const async = require('async')
|
|
|
|
const _ = require('underscore')
|
2019-11-05 09:00:57 -05:00
|
|
|
const { promisify } = require('util')
|
2019-05-29 05:21:06 -04:00
|
|
|
const SubscriptionUpdater = require('./SubscriptionUpdater')
|
|
|
|
const SubscriptionLocator = require('./SubscriptionLocator')
|
|
|
|
const UserGetter = require('../User/UserGetter')
|
|
|
|
const { Subscription } = require('../../models/Subscription')
|
|
|
|
const LimitationsManager = require('./LimitationsManager')
|
|
|
|
const logger = require('logger-sharelatex')
|
|
|
|
const OneTimeTokenHandler = require('../Security/OneTimeTokenHandler')
|
|
|
|
const EmailHandler = require('../Email/EmailHandler')
|
|
|
|
const settings = require('settings-sharelatex')
|
|
|
|
const NotificationsBuilder = require('../Notifications/NotificationsBuilder')
|
|
|
|
const UserMembershipViewModel = require('../UserMembership/UserMembershipViewModel')
|
|
|
|
|
2019-11-05 09:00:57 -05:00
|
|
|
const SubscriptionGroupHandler = {
|
2019-05-29 05:21:06 -04:00
|
|
|
removeUserFromGroup(subscriptionId, userToRemove_id, callback) {
|
|
|
|
return SubscriptionUpdater.removeUserFromGroup(
|
|
|
|
subscriptionId,
|
|
|
|
userToRemove_id,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
replaceUserReferencesInGroups(oldId, newId, callback) {
|
2020-11-03 04:19:05 -05:00
|
|
|
return Subscription.updateOne(
|
2019-05-29 05:21:06 -04:00
|
|
|
{ admin_id: oldId },
|
|
|
|
{ admin_id: newId },
|
2021-04-14 09:17:21 -04:00
|
|
|
function (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return replaceInArray(
|
|
|
|
Subscription,
|
|
|
|
'manager_ids',
|
|
|
|
oldId,
|
|
|
|
newId,
|
2021-04-14 09:17:21 -04:00
|
|
|
function (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (error != null) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return replaceInArray(
|
|
|
|
Subscription,
|
|
|
|
'member_ids',
|
|
|
|
oldId,
|
|
|
|
newId,
|
|
|
|
callback
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
isUserPartOfGroup(user_id, subscription_id, callback) {
|
|
|
|
if (callback == null) {
|
2021-04-14 09:17:21 -04:00
|
|
|
callback = function (err, partOfGroup) {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
return SubscriptionLocator.getSubscriptionByMemberIdAndId(
|
|
|
|
user_id,
|
|
|
|
subscription_id,
|
2021-04-14 09:17:21 -04:00
|
|
|
function (err, subscription) {
|
2019-05-29 05:21:06 -04:00
|
|
|
let partOfGroup
|
|
|
|
if (subscription != null) {
|
|
|
|
partOfGroup = true
|
|
|
|
} else {
|
|
|
|
partOfGroup = false
|
|
|
|
}
|
|
|
|
return callback(err, partOfGroup)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
getTotalConfirmedUsersInGroup(subscription_id, callback) {
|
|
|
|
if (callback == null) {
|
2021-04-14 09:17:21 -04:00
|
|
|
callback = function (err, totalUsers) {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
return SubscriptionLocator.getSubscription(
|
|
|
|
subscription_id,
|
|
|
|
(err, subscription) =>
|
|
|
|
callback(
|
|
|
|
err,
|
|
|
|
__guard__(
|
|
|
|
subscription != null ? subscription.member_ids : undefined,
|
|
|
|
x => x.length
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
var replaceInArray = function (model, property, oldValue, newValue, callback) {
|
2019-05-29 05:21:06 -04:00
|
|
|
// Mongo won't let us pull and addToSet in the same query, so do it in
|
|
|
|
// two. Note we need to add first, since the query is based on the old user.
|
|
|
|
const query = {}
|
|
|
|
query[property] = oldValue
|
|
|
|
|
|
|
|
const setNewValue = {}
|
|
|
|
setNewValue[property] = newValue
|
|
|
|
|
|
|
|
const setOldValue = {}
|
|
|
|
setOldValue[property] = oldValue
|
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
model.updateMany(query, { $addToSet: setNewValue }, function (error) {
|
2020-11-03 04:19:05 -05:00
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2020-11-03 04:19:05 -05:00
|
|
|
model.updateMany(query, { $pull: setOldValue }, callback)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function __guard__(value, transform) {
|
|
|
|
return typeof value !== 'undefined' && value !== null
|
|
|
|
? transform(value)
|
|
|
|
: undefined
|
|
|
|
}
|
2019-11-05 09:00:57 -05:00
|
|
|
|
|
|
|
SubscriptionGroupHandler.promises = {
|
|
|
|
getTotalConfirmedUsersInGroup: promisify(
|
|
|
|
SubscriptionGroupHandler.getTotalConfirmedUsersInGroup
|
|
|
|
),
|
2021-04-27 03:52:58 -04:00
|
|
|
isUserPartOfGroup: promisify(SubscriptionGroupHandler.isUserPartOfGroup),
|
2019-11-05 09:00:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SubscriptionGroupHandler
|