set_manually_collected_subscriptions.mjs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // @ts-check
  2. import fs from 'node:fs'
  3. import minimist from 'minimist'
  4. import {
  5. db,
  6. READ_PREFERENCE_SECONDARY,
  7. } from '../../app/src/infrastructure/mongodb.js'
  8. /**
  9. * @import { ObjectId } from 'mongodb-legacy'
  10. */
  11. const OPTS = parseArgs()
  12. const expectedManualRecurlyIds = readFile(OPTS.filename)
  13. const idsToSetToManual = await getSubscriptionIdsToSetToManual(
  14. expectedManualRecurlyIds
  15. )
  16. const idsToSetToAutomatic = await getSubscriptionIdsToSetToAutomatic(
  17. expectedManualRecurlyIds
  18. )
  19. if (idsToSetToManual.length > 0) {
  20. if (OPTS.commit) {
  21. console.log(
  22. `Setting ${idsToSetToManual.length} subscriptions to manual invoice collection...`
  23. )
  24. await setCollectionMethod(idsToSetToManual, 'manual')
  25. } else {
  26. console.log(
  27. `Would set ${idsToSetToManual.length} subscriptions to manual invoice collection`
  28. )
  29. }
  30. }
  31. if (idsToSetToAutomatic.length > 0) {
  32. if (OPTS.commit) {
  33. console.log(
  34. `Setting ${idsToSetToAutomatic.length} subscriptions to automatic invoice collection...`
  35. )
  36. await setCollectionMethod(idsToSetToAutomatic, 'automatic')
  37. } else {
  38. console.log(
  39. `Would set ${idsToSetToAutomatic.length} subscriptions to automatic invoice collection`
  40. )
  41. }
  42. }
  43. if (!OPTS.commit) {
  44. console.log('This was a dry run. Add the --commit option to apply changes')
  45. }
  46. process.exit(0)
  47. function parseArgs() {
  48. const args = minimist(process.argv.slice(2), {
  49. boolean: ['commit'],
  50. })
  51. if (args._.length !== 1) {
  52. usage()
  53. process.exit(1)
  54. }
  55. return {
  56. filename: args._[0],
  57. commit: args.commit,
  58. }
  59. }
  60. function usage() {
  61. console.log(`Usage: node set_manually_collected_subscriptions.mjs FILE [--commit]
  62. where FILE contains the list of subscription ids that are manually collected`)
  63. }
  64. function readFile(filename) {
  65. const contents = fs.readFileSync(filename, { encoding: 'utf-8' })
  66. const subscriptionIds = contents.split('\n').filter(id => id.length > 0)
  67. return subscriptionIds
  68. }
  69. /**
  70. * Get the ids of subscriptions that need to have their collection method set to
  71. * manual
  72. *
  73. * @param {string[]} expectedManualRecurlyIds
  74. * @return {Promise<ObjectId[]>}
  75. */
  76. async function getSubscriptionIdsToSetToManual(expectedManualRecurlyIds) {
  77. const ids = await db.subscriptions
  78. .find(
  79. {
  80. recurlySubscription_id: { $in: expectedManualRecurlyIds },
  81. collectionMethod: { $ne: 'manual' },
  82. },
  83. { projection: { _id: 1 }, readPreference: READ_PREFERENCE_SECONDARY }
  84. )
  85. .map(record => record._id)
  86. .toArray()
  87. return ids
  88. }
  89. /**
  90. * Get the ids of subscriptions that need to have their collection method set to
  91. * automatic
  92. *
  93. * @param {string[]} expectedManualRecurlyIds
  94. * @return {Promise<ObjectId[]>}
  95. */
  96. async function getSubscriptionIdsToSetToAutomatic(expectedManualRecurlyIds) {
  97. const ids = await db.subscriptions
  98. .find(
  99. {
  100. recurlySubscription_id: { $nin: expectedManualRecurlyIds },
  101. collectionMethod: 'manual',
  102. },
  103. { projection: { _id: 1 }, readPreference: READ_PREFERENCE_SECONDARY }
  104. )
  105. .map(record => record._id)
  106. .toArray()
  107. return ids
  108. }
  109. /**
  110. * Set the collection method for the given subscriptions
  111. *
  112. * @param {ObjectId[]} subscriptionIds
  113. * @param {"automatic" | "manual"} collectionMethod
  114. */
  115. async function setCollectionMethod(subscriptionIds, collectionMethod) {
  116. await db.subscriptions.updateMany(
  117. { _id: { $in: subscriptionIds } },
  118. { $set: { collectionMethod } }
  119. )
  120. }