set_manually_collected_subscriptions.mjs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.mjs'
  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. /**
  65. * @param {any} filename
  66. */
  67. function readFile(filename) {
  68. const contents = fs.readFileSync(filename, { encoding: 'utf-8' })
  69. const subscriptionIds = contents.split('\n').filter(id => id.length > 0)
  70. return subscriptionIds
  71. }
  72. /**
  73. * Get the ids of subscriptions that need to have their collection method set to
  74. * manual
  75. *
  76. * @param {string[]} expectedManualRecurlyIds
  77. * @return {Promise<ObjectId[]>}
  78. */
  79. async function getSubscriptionIdsToSetToManual(expectedManualRecurlyIds) {
  80. const ids = await db.subscriptions
  81. .find(
  82. {
  83. recurlySubscription_id: { $in: expectedManualRecurlyIds },
  84. collectionMethod: { $ne: 'manual' },
  85. },
  86. { projection: { _id: 1 }, readPreference: READ_PREFERENCE_SECONDARY }
  87. )
  88. .map(record => record._id)
  89. .toArray()
  90. return ids
  91. }
  92. /**
  93. * Get the ids of subscriptions that need to have their collection method set to
  94. * automatic
  95. *
  96. * @param {string[]} expectedManualRecurlyIds
  97. * @return {Promise<ObjectId[]>}
  98. */
  99. async function getSubscriptionIdsToSetToAutomatic(expectedManualRecurlyIds) {
  100. const ids = await db.subscriptions
  101. .find(
  102. {
  103. recurlySubscription_id: { $nin: expectedManualRecurlyIds },
  104. collectionMethod: 'manual',
  105. },
  106. { projection: { _id: 1 }, readPreference: READ_PREFERENCE_SECONDARY }
  107. )
  108. .map(record => record._id)
  109. .toArray()
  110. return ids
  111. }
  112. /**
  113. * Set the collection method for the given subscriptions
  114. *
  115. * @param {ObjectId[]} subscriptionIds
  116. * @param {"automatic" | "manual"} collectionMethod
  117. */
  118. async function setCollectionMethod(subscriptionIds, collectionMethod) {
  119. await db.subscriptions.updateMany(
  120. { _id: { $in: subscriptionIds } },
  121. { $set: { collectionMethod } }
  122. )
  123. }