2024-11-08 03:34:14 -05:00
|
|
|
import { batchedUpdateWithResultHandling } from '@overleaf/mongo-utils/batchedUpdate.js'
|
2024-10-18 07:04:57 -04:00
|
|
|
import { promiseMapWithLimit } from '@overleaf/promise-utils'
|
|
|
|
import SubscriptionLocator from '../app/src/Features/Subscription/SubscriptionLocator.js'
|
|
|
|
import PlansLocator from '../app/src/Features/Subscription/PlansLocator.js'
|
|
|
|
import FeaturesHelper from '../app/src/Features/Subscription/FeaturesHelper.js'
|
|
|
|
import AnalyticsManager from '../app/src/Features/Analytics/AnalyticsManager.js'
|
2024-11-08 03:34:14 -05:00
|
|
|
import { db } from '../app/src/infrastructure/mongodb.js'
|
2022-01-25 05:18:07 -05:00
|
|
|
|
2024-10-18 07:04:57 -04:00
|
|
|
const WRITE_CONCURRENCY = parseInt(process.env.WRITE_CONCURRENCY, 10) || 10
|
2022-01-25 05:18:07 -05:00
|
|
|
|
|
|
|
async function getGroupSubscriptionPlanCode(userId) {
|
|
|
|
const subscriptions =
|
|
|
|
await SubscriptionLocator.promises.getMemberSubscriptions(userId)
|
|
|
|
let bestPlanCode = null
|
|
|
|
let bestFeatures = {}
|
|
|
|
for (const subscription of subscriptions) {
|
|
|
|
const plan = PlansLocator.findLocalPlanInSettings(subscription.planCode)
|
|
|
|
if (
|
|
|
|
plan &&
|
|
|
|
FeaturesHelper.isFeatureSetBetter(plan.features, bestFeatures)
|
|
|
|
) {
|
|
|
|
bestPlanCode = plan.planCode
|
|
|
|
bestFeatures = plan.features
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bestPlanCode
|
|
|
|
}
|
|
|
|
|
|
|
|
async function processUser(user) {
|
|
|
|
const analyticsId = user.analyticsId || user._id
|
|
|
|
|
|
|
|
const groupSubscriptionPlanCode = await getGroupSubscriptionPlanCode(user._id)
|
|
|
|
if (groupSubscriptionPlanCode) {
|
|
|
|
await AnalyticsManager.setUserPropertyForAnalyticsId(
|
|
|
|
analyticsId,
|
|
|
|
'group-subscription-plan-code',
|
|
|
|
groupSubscriptionPlanCode
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const matchedFeatureSet = FeaturesHelper.getMatchedFeatureSet(user.features)
|
|
|
|
if (matchedFeatureSet !== 'personal') {
|
|
|
|
await AnalyticsManager.setUserPropertyForAnalyticsId(
|
|
|
|
analyticsId,
|
|
|
|
'feature-set',
|
|
|
|
matchedFeatureSet
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-18 07:04:57 -04:00
|
|
|
async function processBatch(users) {
|
2022-01-25 05:18:07 -05:00
|
|
|
await promiseMapWithLimit(WRITE_CONCURRENCY, users, async user => {
|
|
|
|
await processUser(user)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-11-08 03:34:14 -05:00
|
|
|
batchedUpdateWithResultHandling(db.users, {}, processBatch, {
|
2022-01-25 05:18:07 -05:00
|
|
|
_id: true,
|
|
|
|
analyticsId: true,
|
|
|
|
features: true,
|
|
|
|
})
|