rollback_price_changes.mjs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. #!/usr/bin/env node
  2. /**
  3. * Rollback pending price changes for Recurly subscriptions
  4. *
  5. * This script removes pending subscription changes that were created by the
  6. * change_existing_subscription_prices.mjs script. It only removes changes that
  7. * are purely price changes on an existing plan - if the user has made any other
  8. * modifications (plan change, add-on changes), the pending change is left untouched.
  9. *
  10. * Usage:
  11. * node scripts/recurly/rollback_price_changes.mjs [OPTIONS] [INPUT-FILE]
  12. *
  13. * Options:
  14. * --output PATH Output file path (default: /tmp/rollback_prices_output_<timestamp>.csv)
  15. * Use '-' to write to stdout
  16. * --commit Apply changes (without this flag, runs in dry-run mode)
  17. * --throttle DURATION Minimum time (in ms) between subscriptions processed (default: 2400)
  18. * --help Show a help message
  19. *
  20. * CSV Input Format:
  21. * The CSV must have the following columns (same format as change_existing_subscription_prices.mjs):
  22. * - subscription_uuid: Recurly subscription UUID
  23. * - plan_code: Plan code at time of price change
  24. * - currency: Currency
  25. * - unit_amount: Original price per unit (before our price increase)
  26. * - new_unit_amount: New price per unit (after our price increase)
  27. * - subscription_add_on_unit_amount_in_cents: Original additional-licenses add-on price (optional)
  28. * - new_subscription_add_on_unit_amount_in_cents: New additional-licenses add-on price (optional)
  29. *
  30. * Output:
  31. * Writes a CSV with columns:
  32. * - subscription_uuid: The subscription UUID processed
  33. * - status: Result status (rolled-back, skipped, validated, not-found, or error)
  34. * - note: Additional information about the status
  35. *
  36. * The script will SKIP (not rollback) a subscription if:
  37. * - There is no pending change
  38. * - The pending change involves a plan change (user downgrade/upgrade)
  39. * - The pending change involves add-on additions/removals
  40. * - The pending change involves add-on quantity changes
  41. * - The prices don't match what we expect from the CSV
  42. *
  43. * Running on a Pod:
  44. * This script may run for multiple days. When running using `rake run:longpod[ENV,web]`,
  45. * use one of these strategies to preserve output:
  46. *
  47. * 1. Tail the output file from another session:
  48. * kubectl exec -it <pod-name> -- tail -f /tmp/rollback_prices_output_<timestamp>.csv > local_backup.csv
  49. *
  50. * 2. Periodically copy the output file to your laptop:
  51. * kubectl cp <pod-name>:/tmp/rollback_prices_output_<timestamp>.csv ./backup.csv
  52. *
  53. * 3. Write to stdout and capture locally:
  54. * kubectl exec -it <pod-name> -- node scripts/recurly/rollback_price_changes.mjs \
  55. * --commit --output - input.csv > output.csv
  56. *
  57. * Examples:
  58. * # Dry run (preview only)
  59. * node scripts/recurly/rollback_price_changes.mjs input.csv
  60. *
  61. * # Actually perform the rollback
  62. * node scripts/recurly/rollback_price_changes.mjs --commit input.csv
  63. */
  64. import fs from 'node:fs'
  65. import path from 'node:path'
  66. import { setTimeout } from 'node:timers/promises'
  67. import * as csv from 'csv'
  68. import minimist from 'minimist'
  69. import recurly from 'recurly'
  70. import Settings from '@overleaf/settings'
  71. import AnalyticsManager from '../../app/src/Features/Analytics/AnalyticsManager.mjs'
  72. import { z } from '../../app/src/infrastructure/Validation.mjs'
  73. import { scriptRunner } from '../lib/ScriptRunner.mjs'
  74. /**
  75. * @import { ReadStream } from 'node:fs'
  76. * @import { Parser } from 'csv-parse'
  77. * @import { Stringifier } from 'csv-stringify'
  78. * @import { Subscription } from 'recurly'
  79. */
  80. /**
  81. * @typedef {Object} CSVSubscriptionChange
  82. * @property {string} subscription_uuid
  83. * @property {string} plan_code
  84. * @property {string} currency
  85. * @property {number} unit_amount
  86. * @property {number} new_unit_amount
  87. * @property {number | null} subscription_add_on_unit_amount_in_cents
  88. * @property {number | null} new_subscription_add_on_unit_amount_in_cents
  89. */
  90. const recurlyClient = new recurly.Client(Settings.apis.recurly.apiKey)
  91. // 2400 ms corresponds to approx. 3000 API calls per hour
  92. const DEFAULT_THROTTLE = 2400
  93. /**
  94. * Print usage information to stderr
  95. */
  96. function usage() {
  97. console.error(`Usage: node scripts/recurly/rollback_price_changes.mjs [OPTIONS] [INPUT-FILE]
  98. Rollback pending price changes for Recurly subscriptions.
  99. This script only removes pending changes that are purely price changes on an
  100. existing plan. If a user has made any other modifications (plan change, add-on
  101. changes), the subscription is skipped.
  102. Options:
  103. --output PATH Output file path (default: /tmp/rollback_prices_output_<timestamp>.csv)
  104. Use '-' to write to stdout
  105. --commit Apply changes (without this, runs in dry-run mode)
  106. --throttle DURATION Minimum time between requests in ms (default: ${DEFAULT_THROTTLE})
  107. --help Show this help message
  108. Output Statuses:
  109. rolled-back Pending price change was removed
  110. validated Dry run - would have removed pending price change
  111. mismatch Input prices malformed or subscription price does not match expected values
  112. skipped Not a price-only change, or values don't match (see note)
  113. not-found Subscription not found in Recurly
  114. error An error occurred
  115. See the source file header for detailed documentation on CSV format and pod usage.
  116. `)
  117. }
  118. /**
  119. * Main script entry point
  120. * @param {function(string): Promise<void>} trackProgress - Function to log progress messages
  121. */
  122. async function main(trackProgress) {
  123. const opts = parseArgs()
  124. const timestamp = new Date().toISOString().replace(/[:.]/g, '-')
  125. const outputFile =
  126. opts.output ?? `/tmp/rollback_prices_output_${timestamp}.csv`
  127. await trackProgress('Starting price rollback script for Recurly')
  128. await trackProgress(`Run mode: ${opts.commit ? 'COMMIT' : 'DRY RUN'}`)
  129. await trackProgress(`Throttle: ${opts.throttle}ms between requests`)
  130. const inputStream = opts.inputFile
  131. ? fs.createReadStream(opts.inputFile)
  132. : process.stdin
  133. const csvReader = getCsvReader(inputStream)
  134. const csvWriter = getCsvWriter(outputFile)
  135. await trackProgress(`Output: ${outputFile === '-' ? 'stdout' : outputFile}`)
  136. let processedCount = 0
  137. let successCount = 0
  138. let skippedCount = 0
  139. let errorCount = 0
  140. let lastLoopTimestamp = 0
  141. for await (const record of csvReader) {
  142. const timeSinceLastLoop = Date.now() - lastLoopTimestamp
  143. if (timeSinceLastLoop < opts.throttle) {
  144. await setTimeout(opts.throttle - timeSinceLastLoop)
  145. }
  146. lastLoopTimestamp = Date.now()
  147. processedCount++
  148. try {
  149. const result = await processRollback(record, opts.commit)
  150. if (opts.commit && result.subscription) {
  151. try {
  152. const userId = result.subscription.account.code
  153. await AnalyticsManager.recordEventForUser(
  154. userId,
  155. 'script_price_change_reversed',
  156. {
  157. subscriptionId: record.subscription_uuid,
  158. }
  159. )
  160. } catch (err) {
  161. await trackProgress(
  162. `Warning: failed to record analytics event after successful price rollback for ${record.subscription_uuid}: ${err.message}`
  163. )
  164. }
  165. }
  166. csvWriter.write({
  167. subscription_uuid: record.subscription_uuid,
  168. status: result.status,
  169. note: result.note || '',
  170. })
  171. if (result.status === 'skipped') {
  172. skippedCount++
  173. } else {
  174. successCount++
  175. }
  176. if (processedCount % 10 === 0) {
  177. await trackProgress(
  178. `Processed ${processedCount} subscriptions (${successCount} ${opts.commit ? 'rolled-back' : 'validated'}, ${skippedCount} skipped, ${errorCount} errors)`
  179. )
  180. }
  181. } catch (err) {
  182. errorCount++
  183. if (err instanceof ReportError) {
  184. csvWriter.write({
  185. subscription_uuid: record.subscription_uuid,
  186. status: err.status,
  187. note: err.message,
  188. })
  189. } else {
  190. csvWriter.write({
  191. subscription_uuid: record.subscription_uuid,
  192. status: 'error',
  193. note: err.message,
  194. })
  195. await trackProgress(
  196. `Error processing ${record.subscription_uuid}: ${err.message}`
  197. )
  198. }
  199. }
  200. }
  201. await trackProgress('\n✨ FINAL SUMMARY ✨')
  202. await trackProgress(`📊 Total processed: ${processedCount}`)
  203. if (opts.commit) {
  204. await trackProgress(`✅ Successfully rolled back: ${successCount}`)
  205. } else {
  206. await trackProgress(`✅ Successfully validated: ${successCount}`)
  207. await trackProgress('ℹ️ DRY RUN: No changes were applied to Recurly')
  208. }
  209. await trackProgress(`⏭️ Skipped: ${skippedCount}`)
  210. await trackProgress(`❌ Errors: ${errorCount}`)
  211. await trackProgress('🎉 Script completed!')
  212. csvWriter.end()
  213. }
  214. /**
  215. * Get a CSV parser configured for subscription change input
  216. * @param {ReadStream | NodeJS.ReadableStream} inputStream - The input stream to parse
  217. * @returns {Parser} The configured CSV parser
  218. */
  219. function getCsvReader(inputStream) {
  220. const parser = csv.parse({
  221. columns: true,
  222. cast: (value, context) => {
  223. if (context.header) {
  224. return value
  225. }
  226. switch (context.column) {
  227. case 'unit_amount':
  228. case 'new_unit_amount': {
  229. const parsed = parseFloat(value)
  230. if (Number.isNaN(parsed)) {
  231. throw new ReportError(
  232. 'mismatch',
  233. `Invalid number for ${context.column} at row ${context.lines}: "${value}"`
  234. )
  235. }
  236. return parsed
  237. }
  238. case 'subscription_add_on_unit_amount_in_cents':
  239. case 'new_subscription_add_on_unit_amount_in_cents': {
  240. if (value === '') {
  241. return null
  242. }
  243. const parsed = parseInt(value, 10)
  244. if (Number.isNaN(parsed)) {
  245. throw new ReportError(
  246. 'mismatch',
  247. `Invalid number for ${context.column} at row ${context.lines}: "${value}"`
  248. )
  249. }
  250. return parsed
  251. }
  252. default:
  253. return value
  254. }
  255. },
  256. })
  257. inputStream.pipe(parser)
  258. return parser
  259. }
  260. /**
  261. * Get a CSV stringifier configured for output
  262. * @param {string} outputFile - The output file path to write to, or '-' for stdout
  263. * @returns {Stringifier} The configured CSV stringifier
  264. */
  265. function getCsvWriter(outputFile) {
  266. let outputStream
  267. if (outputFile === '-') {
  268. outputStream = process.stdout
  269. } else {
  270. fs.mkdirSync(path.dirname(outputFile), { recursive: true })
  271. outputStream = fs.createWriteStream(outputFile)
  272. }
  273. const writer = csv.stringify({
  274. columns: ['subscription_uuid', 'status', 'note'],
  275. header: true,
  276. })
  277. writer.on('error', err => {
  278. console.error(err)
  279. process.exit(1)
  280. })
  281. writer.pipe(outputStream)
  282. return writer
  283. }
  284. /**
  285. * Process a single subscription rollback
  286. * @param {CSVSubscriptionChange} record - The subscription record to process
  287. * @param {boolean} commit - Whether to commit changes or run in dry-run mode
  288. * @returns {Promise<{status: string, note: string, subscription?: Subscription}>} The result of the rollback
  289. */
  290. async function processRollback(record, commit) {
  291. const subscription = await fetchSubscription(record.subscription_uuid)
  292. // Validate this is a price-only change that we created
  293. const validation = validatePriceOnlyChange(record, subscription)
  294. if (!validation.isPriceOnly) {
  295. return {
  296. status: 'skipped',
  297. note: `${validation.reason}: ${validation.detail || 'N/A'}`,
  298. }
  299. }
  300. if (!commit) {
  301. return {
  302. status: 'validated',
  303. note: `Would remove pending price change: ${subscription.unitAmount} -> ${subscription.pendingChange.unitAmount}`,
  304. }
  305. }
  306. // Safe to remove - this is a price-only change matching our expected values
  307. await recurlyClient.removeSubscriptionChange(
  308. `uuid-${record.subscription_uuid}`
  309. )
  310. return {
  311. status: 'rolled-back',
  312. note: `Removed pending price change: ${subscription.unitAmount} -> ${subscription.pendingChange.unitAmount}`,
  313. subscription,
  314. }
  315. }
  316. /**
  317. * Fetch a subscription from Recurly
  318. * @param {string} uuid - The Recurly subscription UUID
  319. * @returns {Promise<Subscription>} The subscription
  320. * @throws {ReportError} If subscription is not found
  321. */
  322. async function fetchSubscription(uuid) {
  323. try {
  324. const subscription = await recurlyClient.getSubscription(`uuid-${uuid}`)
  325. return subscription
  326. } catch (err) {
  327. if (err instanceof recurly.errors.NotFoundError) {
  328. throw new ReportError('not-found', 'subscription not found')
  329. } else {
  330. throw err
  331. }
  332. }
  333. }
  334. /**
  335. * Validate that the pending change is a price-only change created by our
  336. * price increase script, and not a user-initiated plan change or add-on modification.
  337. *
  338. * @param {CSVSubscriptionChange} record - The CSV record with expected values
  339. * @param {Subscription} subscription - The Recurly subscription
  340. * @returns {{ isPriceOnly: boolean, reason?: string, detail?: string }}
  341. */
  342. function validatePriceOnlyChange(record, subscription) {
  343. const pendingChange = subscription.pendingChange
  344. // Check 1: Must have a pending change
  345. if (pendingChange == null) {
  346. return {
  347. isPriceOnly: false,
  348. reason: 'no-pending-change',
  349. detail: 'subscription has no pending change to rollback',
  350. }
  351. }
  352. // Check 2: Subscription must be active
  353. if (subscription.state !== 'active') {
  354. return {
  355. isPriceOnly: false,
  356. reason: 'inactive',
  357. detail: `subscription state: ${subscription.state}`,
  358. }
  359. }
  360. // Check 3: Plan code must match expected (from CSV)
  361. if (subscription.plan.code !== record.plan_code) {
  362. return {
  363. isPriceOnly: false,
  364. reason: 'plan-mismatch',
  365. detail: `expected plan ${record.plan_code}, got ${subscription.plan.code}`,
  366. }
  367. }
  368. // Check 4: Pending change must be for the SAME plan (not a downgrade/upgrade)
  369. if (pendingChange.plan.code !== subscription.plan.code) {
  370. return {
  371. isPriceOnly: false,
  372. reason: 'plan-change-detected',
  373. detail: `pending plan change: ${subscription.plan.code} -> ${pendingChange.plan.code}`,
  374. }
  375. }
  376. // Check 5: Currency must match
  377. if (subscription.currency !== record.currency) {
  378. return {
  379. isPriceOnly: false,
  380. reason: 'currency-mismatch',
  381. detail: `expected ${record.currency}, got ${subscription.currency}`,
  382. }
  383. }
  384. // Check 6: Current price must match expected (from CSV)
  385. if (Math.abs(subscription.unitAmount - record.unit_amount) > 0.01) {
  386. return {
  387. isPriceOnly: false,
  388. reason: 'current-price-mismatch',
  389. detail: `expected current price ${record.unit_amount}, got ${subscription.unitAmount}`,
  390. }
  391. }
  392. // Check 7: Pending price must match expected new price (from CSV)
  393. if (Math.abs(pendingChange.unitAmount - record.new_unit_amount) > 0.01) {
  394. return {
  395. isPriceOnly: false,
  396. reason: 'pending-price-mismatch',
  397. detail: `expected pending price ${record.new_unit_amount}, got ${pendingChange.unitAmount}`,
  398. }
  399. }
  400. // Check 8: Add-on codes must be the same (no add-ons added or removed)
  401. const currentAddOnCodes = new Set(
  402. (subscription.addOns || []).map(a => a.addOn.code)
  403. )
  404. const pendingAddOnCodes = new Set(
  405. (pendingChange.addOns || []).map(a => a.addOn.code)
  406. )
  407. if (!setsEqual(currentAddOnCodes, pendingAddOnCodes)) {
  408. return {
  409. isPriceOnly: false,
  410. reason: 'addon-change-detected',
  411. detail: `current add-ons: [${[...currentAddOnCodes]}], pending: [${[...pendingAddOnCodes]}]`,
  412. }
  413. }
  414. // Check 9: Add-on quantities must be the same
  415. for (const currentAddOn of subscription.addOns || []) {
  416. const pendingAddOn = (pendingChange.addOns || []).find(
  417. a => a.addOn.code === currentAddOn.addOn.code
  418. )
  419. if (pendingAddOn && pendingAddOn.quantity !== currentAddOn.quantity) {
  420. return {
  421. isPriceOnly: false,
  422. reason: 'addon-quantity-change-detected',
  423. detail: `${currentAddOn.addOn.code}: quantity ${currentAddOn.quantity} -> ${pendingAddOn.quantity}`,
  424. }
  425. }
  426. }
  427. // Check 10: Validate add-on prices if provided in CSV
  428. if (record.subscription_add_on_unit_amount_in_cents != null) {
  429. const additionalLicenseAddOn = (subscription.addOns || []).find(
  430. a => a.addOn.code === 'additional-license'
  431. )
  432. if (additionalLicenseAddOn == null) {
  433. return {
  434. isPriceOnly: false,
  435. reason: 'addon-mismatch',
  436. detail: 'expected additional-license add-on but not found',
  437. }
  438. }
  439. const expectedCurrentAddOnPrice =
  440. record.subscription_add_on_unit_amount_in_cents / 100
  441. if (
  442. Math.abs(additionalLicenseAddOn.unitAmount - expectedCurrentAddOnPrice) >
  443. 0.01
  444. ) {
  445. return {
  446. isPriceOnly: false,
  447. reason: 'addon-price-mismatch',
  448. detail: `expected add-on price ${expectedCurrentAddOnPrice}, got ${additionalLicenseAddOn.unitAmount}`,
  449. }
  450. }
  451. // Verify pending add-on price matches expected new price
  452. if (record.new_subscription_add_on_unit_amount_in_cents != null) {
  453. const pendingAddOn = (pendingChange.addOns || []).find(
  454. a => a.addOn.code === 'additional-license'
  455. )
  456. const expectedNewAddOnPrice =
  457. record.new_subscription_add_on_unit_amount_in_cents / 100
  458. if (pendingAddOn == null) {
  459. return {
  460. isPriceOnly: false,
  461. reason: 'pending-addon-mismatch',
  462. detail:
  463. 'expected additional-license add-on in pending change but not found',
  464. }
  465. }
  466. if (Math.abs(pendingAddOn.unitAmount - expectedNewAddOnPrice) > 0.01) {
  467. return {
  468. isPriceOnly: false,
  469. reason: 'pending-addon-price-mismatch',
  470. detail: `expected pending add-on price ${expectedNewAddOnPrice}, got ${pendingAddOn.unitAmount}`,
  471. }
  472. }
  473. }
  474. }
  475. // All checks passed - this is a price-only change we created
  476. return { isPriceOnly: true }
  477. }
  478. /**
  479. * Check if two sets are equal
  480. * @param {Set<string>} a - First set
  481. * @param {Set<string>} b - Second set
  482. * @returns {boolean} True if sets are equal
  483. */
  484. function setsEqual(a, b) {
  485. return a.size === b.size && [...a].every(x => b.has(x))
  486. }
  487. const paramsSchema = z.object({
  488. output: z.string().optional(),
  489. commit: z.boolean().default(false),
  490. throttle: z
  491. .string()
  492. .optional()
  493. .transform(val => (val ? parseInt(val, 10) : DEFAULT_THROTTLE)),
  494. _: z.array(z.string()).max(1),
  495. help: z.boolean().optional(),
  496. })
  497. /**
  498. * Parse command line arguments
  499. * @returns {{inputFile: string | undefined, output: string | undefined, commit: boolean, throttle: number}} Parsed options
  500. */
  501. function parseArgs() {
  502. const argv = minimist(process.argv.slice(2), {
  503. string: ['throttle', 'output'],
  504. boolean: ['help', 'commit'],
  505. })
  506. if (argv.help) {
  507. usage()
  508. process.exit(0)
  509. }
  510. const parseResult = paramsSchema.safeParse(argv)
  511. if (!parseResult.success) {
  512. console.error(`Invalid parameters: ${parseResult.error.message}`)
  513. usage()
  514. process.exit(1)
  515. }
  516. const { output, commit, throttle, _ } = parseResult.data
  517. return {
  518. inputFile: _[0],
  519. output,
  520. commit,
  521. throttle,
  522. }
  523. }
  524. /**
  525. * Custom error class for reportable errors that should be written to CSV output
  526. */
  527. class ReportError extends Error {
  528. /**
  529. * @param {string} status - The error status code for CSV output
  530. * @param {string} message - The error message
  531. */
  532. constructor(status, message) {
  533. super(message)
  534. this.status = status
  535. }
  536. }
  537. try {
  538. await scriptRunner(main)
  539. process.exit(0)
  540. } catch (error) {
  541. console.error(error)
  542. process.exit(1)
  543. }