merge_group_subscription_members.mjs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. import { db, ObjectId } from '../app/src/infrastructure/mongodb.js'
  9. import SubscriptionUpdater from '../app/src/Features/Subscription/SubscriptionUpdater.js'
  10. import minimist from '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. const targetSubscription = await getSubscription(new ObjectId(target))
  41. const sourceSubscription = await getSubscription(new ObjectId(source))
  42. if (!targetSubscription) {
  43. throw new Error('couldnt find target (to) subscription')
  44. }
  45. if (!sourceSubscription) {
  46. throw new Error('couldnt find source (from) subscription')
  47. }
  48. console.log(
  49. `\nTarget/destination subscription (${targetSubscription.member_ids.length} members) is:`,
  50. targetSubscription
  51. )
  52. console.log(
  53. `\nSource subscription (${sourceSubscription.member_ids.length} members) is:`,
  54. sourceSubscription
  55. )
  56. if (!targetSubscription.groupPlan || !sourceSubscription.groupPlan) {
  57. throw new Error('both subscriptions must be group subscriptions')
  58. }
  59. let addCount = 0
  60. for (const member of sourceSubscription.member_ids) {
  61. const exists = targetSubscription.member_ids.find(m => {
  62. return m.toString() === member.toString()
  63. })
  64. if (!exists) {
  65. console.log(`adding ${member} to target ${targetSubscription._id}`)
  66. addCount += 1
  67. if (commit) {
  68. await SubscriptionUpdater.promises.addUserToGroup(
  69. targetSubscription._id,
  70. member
  71. )
  72. }
  73. } else {
  74. console.log(`skipping ${member}, already exists in target`)
  75. }
  76. }
  77. console.log(`Added ${addCount} users to target subscription`)
  78. if (!commit) {
  79. console.log('Run again with --commit to make the above changes')
  80. }
  81. }
  82. try {
  83. await main()
  84. console.error('Done.')
  85. process.exit(0)
  86. } catch (error) {
  87. console.error({ error })
  88. process.exit(1)
  89. }