generate_recurly_prices.mjs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // script to generate plan prices for recurly from a csv file
  2. //
  3. // Usage:
  4. //
  5. // $ node scripts/recurly/generate_recurly_prices.mjs -f input.csv -o prices.json
  6. //
  7. // The input csv file has the following format:
  8. //
  9. // plan_code,USD,EUR,GBP,...
  10. // student,9,8,7,...
  11. // student-annual,89,79,69,...
  12. // group_professional_2_educational,558,516,446,...
  13. //
  14. // The output file format is the JSON of the plans returned by recurly, with an
  15. // extra _addOns property for the addOns associated with that plan.
  16. //
  17. // The output can be used as input for the upload script `recurly_prices.js`.
  18. import minimist from 'minimist'
  19. // https://github.com/import-js/eslint-plugin-import/issues/1810
  20. // eslint-disable-next-line import/no-unresolved
  21. import * as csv from 'csv/sync'
  22. import _ from 'lodash'
  23. import fs from 'node:fs'
  24. const argv = minimist(process.argv.slice(2), {
  25. string: ['output', 'file'],
  26. alias: { o: 'output', f: 'file' },
  27. default: { output: '/dev/stdout' },
  28. })
  29. // All currency codes are 3 uppercase letters
  30. const CURRENCY_CODE_REGEX = /^[A-Z]{3}$/
  31. // Group plans have a plan code of the form group_name_size_type, e.g.
  32. const GROUP_SIZE_REGEX = /group_\w+_([0-9]+)_\w+/
  33. // Only group plans with more than 4 users can have additional licenses
  34. const SINGLE_LICENSE_MAX_GROUP_SIZE = 4
  35. // Compute prices for the base plan
  36. function computePrices(plan) {
  37. const prices = _.pickBy(plan, (value, key) => CURRENCY_CODE_REGEX.test(key))
  38. const result = []
  39. for (const currency in prices) {
  40. result.push({
  41. currency,
  42. setupFee: 0,
  43. unitAmount: parseInt(prices[currency], 10),
  44. })
  45. }
  46. return _.sortBy(result, 'currency')
  47. }
  48. // Handle prices for license add-ons associated with group plans
  49. function isGroupPlan(plan) {
  50. return plan.plan_code.startsWith('group_')
  51. }
  52. function getGroupSize(plan) {
  53. // extract the group size from the plan code group_name_size_type using a regex
  54. const match = plan.plan_code.match(GROUP_SIZE_REGEX)
  55. if (!match) {
  56. throw new Error(`cannot find group size in plan code: ${plan.plan_code}`)
  57. }
  58. const size = parseInt(match[1], 10)
  59. return size
  60. }
  61. function computeAddOnPrices(prices, size) {
  62. // The price of an additional license is the per-user cost of the base plan,
  63. // i.e. the price of the plan divided by the group size of the plan
  64. return prices.map(price => {
  65. return {
  66. currency: price.currency,
  67. unitAmount: Math.round((100 * price.unitAmount) / size) / 100,
  68. unitAmountDecimal: null,
  69. }
  70. })
  71. }
  72. // Convert the raw records into the output format
  73. function transformRecordToPlan(record) {
  74. const prices = computePrices(record)
  75. // The base plan has no add-ons
  76. const plan = {
  77. code: record.plan_code,
  78. currencies: prices,
  79. }
  80. // Large group plans have an add-on for additional licenses
  81. if (isGroupPlan(record)) {
  82. const size = getGroupSize(record)
  83. if (size > SINGLE_LICENSE_MAX_GROUP_SIZE) {
  84. const addOnPrices = computeAddOnPrices(prices, size)
  85. plan._addOns = [
  86. {
  87. code: 'additional-license',
  88. currencies: addOnPrices,
  89. },
  90. ]
  91. }
  92. }
  93. return plan
  94. }
  95. function generate(inputFile, outputFile) {
  96. const input = fs.readFileSync(inputFile, 'utf8')
  97. const rawRecords = csv.parse(input, { columns: true })
  98. // transform the raw records into the output format
  99. const plans = _.sortBy(rawRecords, 'plan_code').map(transformRecordToPlan)
  100. const output = JSON.stringify(plans, null, 2)
  101. fs.writeFileSync(outputFile, output)
  102. }
  103. if (argv.file) {
  104. generate(argv.file, argv.output)
  105. } else {
  106. console.log('usage:\n' + ' --file input.csv -o file.json\n')
  107. }