mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-06-11 23:20:47 +02:00
e0e8a2ffaa
Upgrade Prettier to v3 GitOrigin-RevId: 6f1338f196408f3edb4892d5220ad3665ff1a5bc
65 lines
2.0 KiB
JavaScript
65 lines
2.0 KiB
JavaScript
const { callbackify } = require('util')
|
|
const SubscriptionUpdater = require('./SubscriptionUpdater')
|
|
const SubscriptionLocator = require('./SubscriptionLocator')
|
|
const { Subscription } = require('../../models/Subscription')
|
|
|
|
async function removeUserFromGroup(subscriptionId, userIdToRemove) {
|
|
await SubscriptionUpdater.promises.removeUserFromGroup(
|
|
subscriptionId,
|
|
userIdToRemove
|
|
)
|
|
}
|
|
|
|
async function replaceUserReferencesInGroups(oldId, newId) {
|
|
await Subscription.updateOne({ admin_id: oldId }, { admin_id: newId }).exec()
|
|
|
|
await _replaceInArray(Subscription, 'manager_ids', oldId, newId)
|
|
await _replaceInArray(Subscription, 'member_ids', oldId, newId)
|
|
}
|
|
|
|
async function isUserPartOfGroup(userId, subscriptionId) {
|
|
const subscription =
|
|
await SubscriptionLocator.promises.getSubscriptionByMemberIdAndId(
|
|
userId,
|
|
subscriptionId
|
|
)
|
|
|
|
return !!subscription
|
|
}
|
|
|
|
async function getTotalConfirmedUsersInGroup(subscriptionId) {
|
|
const subscription =
|
|
await SubscriptionLocator.promises.getSubscription(subscriptionId)
|
|
|
|
return subscription?.member_ids?.length
|
|
}
|
|
|
|
async function _replaceInArray(model, property, oldValue, newValue) {
|
|
// 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
|
|
|
|
await model.updateMany(query, { $addToSet: setNewValue })
|
|
await model.updateMany(query, { $pull: setOldValue })
|
|
}
|
|
|
|
module.exports = {
|
|
removeUserFromGroup: callbackify(removeUserFromGroup),
|
|
replaceUserReferencesInGroups: callbackify(replaceUserReferencesInGroups),
|
|
getTotalConfirmedUsersInGroup: callbackify(getTotalConfirmedUsersInGroup),
|
|
isUserPartOfGroup: callbackify(isUserPartOfGroup),
|
|
promises: {
|
|
removeUserFromGroup,
|
|
replaceUserReferencesInGroups,
|
|
getTotalConfirmedUsersInGroup,
|
|
isUserPartOfGroup,
|
|
},
|
|
}
|