merge_group_subscription_members.mjs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.mjs'
  9. import SubscriptionUpdater from '../app/src/Features/Subscription/SubscriptionUpdater.mjs'
  10. import minimist from 'minimist'
  11. import { scriptRunner } from './lib/ScriptRunner.mjs'
  12. const argv = minimist(process.argv.slice(2), {
  13. string: ['target', 'source'],
  14. boolean: ['commit'],
  15. })
  16. const { target, source, commit } = argv
  17. async function getSubscription(subscriptionId) {
  18. const projection = {
  19. member_ids: 1,
  20. membersLimit: 1,
  21. groupPlan: 1,
  22. teamName: 1,
  23. }
  24. return await db.subscriptions.findOne(
  25. {
  26. _id: subscriptionId,
  27. },
  28. { projection }
  29. )
  30. }
  31. async function main() {
  32. if (!target) {
  33. throw new Error('missing --target argument')
  34. }
  35. if (!source) {
  36. throw new Error('missing --source argument')
  37. }
  38. if (!commit) {
  39. console.log('Doing dry run without --commit')
  40. }
  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. try {
  84. await scriptRunner(main)
  85. console.error('Done.')
  86. process.exit(0)
  87. } catch (error) {
  88. console.error({ error })
  89. process.exit(1)
  90. }