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 { Subscription } = require('../../models/Subscription')
|
|
|
|
|
2019-11-05 09:00:57 -05:00
|
|
|
const SubscriptionGroupHandler = {
|
2022-05-17 06:10:19 -04:00
|
|
|
removeUserFromGroup(subscriptionId, userIdToRemove, callback) {
|
|
|
|
SubscriptionUpdater.removeUserFromGroup(
|
2019-05-29 05:21:06 -04:00
|
|
|
subscriptionId,
|
2022-05-17 06:10:19 -04:00
|
|
|
userIdToRemove,
|
2019-05-29 05:21:06 -04:00
|
|
|
callback
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
replaceUserReferencesInGroups(oldId, newId, callback) {
|
2022-05-17 06:10:19 -04:00
|
|
|
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) {
|
2022-05-17 06:10:19 -04:00
|
|
|
if (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
|
2022-05-17 06:10:19 -04:00
|
|
|
replaceInArray(
|
2019-05-29 05:21:06 -04:00
|
|
|
Subscription,
|
|
|
|
'manager_ids',
|
|
|
|
oldId,
|
|
|
|
newId,
|
2021-04-14 09:17:21 -04:00
|
|
|
function (error) {
|
2022-05-17 06:10:19 -04:00
|
|
|
if (error) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
|
2022-05-17 06:10:19 -04:00
|
|
|
replaceInArray(Subscription, 'member_ids', oldId, newId, callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2022-05-17 06:10:19 -04:00
|
|
|
isUserPartOfGroup(userId, subscriptionId, callback) {
|
|
|
|
SubscriptionLocator.getSubscriptionByMemberIdAndId(
|
|
|
|
userId,
|
|
|
|
subscriptionId,
|
2021-04-14 09:17:21 -04:00
|
|
|
function (err, subscription) {
|
2019-05-29 05:21:06 -04:00
|
|
|
let partOfGroup
|
2022-05-17 06:10:19 -04:00
|
|
|
if (subscription) {
|
2019-05-29 05:21:06 -04:00
|
|
|
partOfGroup = true
|
|
|
|
} else {
|
|
|
|
partOfGroup = false
|
|
|
|
}
|
2022-05-17 06:10:19 -04:00
|
|
|
callback(err, partOfGroup)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
2022-05-17 06:10:19 -04:00
|
|
|
getTotalConfirmedUsersInGroup(subscriptionId, callback) {
|
|
|
|
SubscriptionLocator.getSubscription(subscriptionId, (err, subscription) =>
|
|
|
|
callback(err, subscription?.member_ids?.length)
|
2019-05-29 05:21:06 -04:00
|
|
|
)
|
2021-04-27 03:52:58 -04:00
|
|
|
},
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2021-10-26 04:08:56 -04:00
|
|
|
function replaceInArray(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
|
|
|
}
|
|
|
|
|
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
|