merge_group_subscription_members.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // dry run:
  2. // node scripts/merge_group_subscription_members \
  3. // --target [targetSubscriptionId] --source [sourceSubscriptionId]
  4. //
  5. // commit changes:
  6. // node scripts/merge_group_subscription_members \
  7. // --target [targetSubscriptionId] --source [sourceSubscriptionId] --commit
  8. const { db, ObjectId, waitForDb } = require('../app/src/infrastructure/mongodb')
  9. const SubscriptionUpdater = require('../app/src/Features/Subscription/SubscriptionUpdater')
  10. const minimist = require('minimist')
  11. const argv = minimist(process.argv.slice(2), {
  12. string: ['target', 'source'],
  13. boolean: ['commit'],
  14. })
  15. const { target, source, commit } = argv
  16. async function getSubscription(subscriptionId) {
  17. const projection = {
  18. member_ids: 1,
  19. membersLimit: 1,
  20. groupPlan: 1,
  21. teamName: 1,
  22. }
  23. return await db.subscriptions.findOne(
  24. {
  25. _id: subscriptionId,
  26. },
  27. { projection }
  28. )
  29. }
  30. async function main() {
  31. if (!target) {
  32. throw new Error('missing --target argument')
  33. }
  34. if (!source) {
  35. throw new Error('missing --source argument')
  36. }
  37. if (!commit) {
  38. console.log('Doing dry run without --commit')
  39. }
  40. await waitForDb()
  41. const targetSubscription = await getSubscription(new ObjectId(target))
  42. const sourceSubscription = await getSubscription(new ObjectId(source))
  43. if (!targetSubscription) {
  44. throw new Error('couldnt find target (to) subscription')
  45. }
  46. if (!sourceSubscription) {
  47. throw new Error('couldnt find source (from) subscription')
  48. }
  49. console.log(
  50. `\nTarget/destination subscription (${targetSubscription.member_ids.length} members) is:`,
  51. targetSubscription
  52. )
  53. console.log(
  54. `\nSource subscription (${sourceSubscription.member_ids.length} members) is:`,
  55. sourceSubscription
  56. )
  57. if (!targetSubscription.groupPlan || !sourceSubscription.groupPlan) {
  58. throw new Error('both subscriptions must be group subscriptions')
  59. }
  60. let addCount = 0
  61. for (const member of sourceSubscription.member_ids) {
  62. const exists = targetSubscription.member_ids.find(m => {
  63. return m.toString() === member.toString()
  64. })
  65. if (!exists) {
  66. console.log(`adding ${member} to target ${targetSubscription._id}`)
  67. addCount += 1
  68. if (commit) {
  69. await SubscriptionUpdater.promises.addUserToGroup(
  70. targetSubscription._id,
  71. member
  72. )
  73. }
  74. } else {
  75. console.log(`skipping ${member}, already exists in target`)
  76. }
  77. }
  78. console.log(`Added ${addCount} users to target subscription`)
  79. if (!commit) {
  80. console.log('Run again with --commit to make the above changes')
  81. }
  82. }
  83. main()
  84. .then(() => {
  85. console.error('Done.')
  86. process.exit(0)
  87. })
  88. .catch(error => {
  89. console.error({ error })
  90. process.exit(1)
  91. })