Merge pull request #15044 from overleaf/mf-send-warning-to-users-with-personal-and-group-subscriptions

Show notification warning to user with both personal and group subscriptions

GitOrigin-RevId: 7f46d7af10389f552175ce26fae9469e0167f95b
This commit is contained in:
M Fahru
2023-10-18 08:05:04 +00:00
committed by Copybot
parent a4d9c900fc
commit e53f3bb158
6 changed files with 119 additions and 0 deletions
@@ -0,0 +1,73 @@
const NotificationsBuilder = require('../app/src/Features/Notifications/NotificationsBuilder')
const { db, waitForDb } = require('../app/src/infrastructure/mongodb')
const { batchedUpdate } = require('./helpers/batchedUpdate')
const DRY_RUN = !process.argv.includes('--dry-run=false')
if (DRY_RUN) {
console.log('Doing dry run')
}
async function processBatch(groupSubscriptionsBatch) {
console.log('\n')
console.log('----- Batch computation started -----')
const flattenedMemberIds = groupSubscriptionsBatch
.map(sub => sub.member_ids)
.flatMap(memberId => memberId)
const uniqueFlattenedMemberIds = [...new Set(flattenedMemberIds)]
const userWithIndividualAndGroupSubscriptions = await db.subscriptions
.find({
groupPlan: false,
'recurlyStatus.state': 'active',
admin_id: { $in: uniqueFlattenedMemberIds },
})
.toArray()
console.log(
`Found ${userWithIndividualAndGroupSubscriptions.length} affected users in this batch`
)
if (DRY_RUN) {
console.error('---')
console.error('Dry-run enabled, use --dry-run=false to commit changes')
console.error('---')
} else {
if (userWithIndividualAndGroupSubscriptions.length > 0) {
console.log(
`Notifying ${userWithIndividualAndGroupSubscriptions.length} users`
)
for (const notif of userWithIndividualAndGroupSubscriptions) {
await NotificationsBuilder.promises
.personalAndGroupSubscriptions(notif.admin_id.toString())
.create()
}
console.log(
`${userWithIndividualAndGroupSubscriptions.length} users successfully notified in this batch`
)
} else {
console.log(
'No users currently subscribe to both individual and group subscription in this batch'
)
}
}
}
async function main() {
await waitForDb()
await batchedUpdate('subscriptions', { groupPlan: true }, processBatch, {
member_ids: 1,
})
}
main()
.then(() => {
process.exit(0)
})
.catch(err => {
console.error(err)
process.exit(1)
})