plans.mjs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Creates data for localizedPlanPricing object in settings.overrides.saas.js
  2. // and group plans object in app/templates/plans/groups.json
  3. // https://github.com/import-js/eslint-plugin-import/issues/1810
  4. // eslint-disable-next-line import/no-unresolved
  5. import * as csv from 'csv/sync'
  6. import fs from 'node:fs'
  7. import path from 'node:path'
  8. import minimist from 'minimist'
  9. import { fileURLToPath } from 'node:url'
  10. const __dirname = path.dirname(fileURLToPath(import.meta.url))
  11. function readCSVFile(fileName) {
  12. // Pick the csv file
  13. const filePath = path.resolve(__dirname, fileName)
  14. const input = fs.readFileSync(filePath, 'utf8')
  15. const rawRecords = csv.parse(input, { columns: true })
  16. return rawRecords
  17. }
  18. function readJSONFile(fileName) {
  19. const filePath = path.resolve(__dirname, fileName)
  20. const file = fs.readFileSync(filePath)
  21. const plans = JSON.parse(file)
  22. // convert the plans JSON from recurly to an array of
  23. // objects matching the spreadsheet format
  24. const result = []
  25. for (const plan of plans) {
  26. const newRow = { plan_code: plan.code }
  27. for (const price of plan.currencies) {
  28. newRow[price.currency] = price.unitAmount
  29. }
  30. result.push(newRow)
  31. }
  32. return result
  33. }
  34. // Mapping of [output_keys]:[actual_keys]
  35. const plansMap = {
  36. student: 'student',
  37. personal: 'paid-personal',
  38. collaborator: 'collaborator',
  39. professional: 'professional',
  40. }
  41. const currencies = [
  42. 'AUD',
  43. 'BRL',
  44. 'CAD',
  45. 'CHF',
  46. 'CLP',
  47. 'COP',
  48. 'DKK',
  49. 'EUR',
  50. 'GBP',
  51. 'INR',
  52. 'MXN',
  53. 'NOK',
  54. 'NZD',
  55. 'PEN',
  56. 'SEK',
  57. 'SGD',
  58. 'USD',
  59. ]
  60. function roundUpToNearest5Cents(number) {
  61. return Math.ceil(number * 20) / 20
  62. }
  63. function generatePlans(workSheetJSON) {
  64. // localizedPlanPricing object for settings.overrides.saas.js
  65. const localizedPlanPricing = {}
  66. for (const currency of currencies) {
  67. localizedPlanPricing[currency] = {
  68. free: {
  69. monthly: 0,
  70. annual: 0,
  71. },
  72. }
  73. for (const [outputKey, actualKey] of Object.entries(plansMap)) {
  74. const monthlyPlan = workSheetJSON.find(
  75. data => data.plan_code === actualKey
  76. )
  77. if (!monthlyPlan) throw new Error(`Missing plan: ${actualKey}`)
  78. if (!(currency in monthlyPlan))
  79. throw new Error(
  80. `Missing currency "${currency}" for plan "${actualKey}"`
  81. )
  82. const actualKeyAnnual = `${actualKey}-annual`
  83. const annualPlan = workSheetJSON.find(
  84. data => data.plan_code === actualKeyAnnual
  85. )
  86. if (!annualPlan) throw new Error(`Missing plan: ${actualKeyAnnual}`)
  87. if (!(currency in annualPlan))
  88. throw new Error(
  89. `Missing currency "${currency}" for plan "${actualKeyAnnual}"`
  90. )
  91. const monthly = Number(monthlyPlan[currency])
  92. const monthlyTimesTwelve = Number(monthlyPlan[currency] * 12)
  93. const annual = Number(annualPlan[currency])
  94. const annualDividedByTwelve = Number(
  95. roundUpToNearest5Cents(annualPlan[currency] / 12)
  96. )
  97. localizedPlanPricing[currency] = {
  98. ...localizedPlanPricing[currency],
  99. [outputKey]: {
  100. monthly,
  101. monthlyTimesTwelve,
  102. annual,
  103. annualDividedByTwelve,
  104. },
  105. }
  106. }
  107. }
  108. return localizedPlanPricing
  109. }
  110. function generateGroupPlans(workSheetJSON) {
  111. // group plans object for app/templates/plans/groups.json
  112. const groupPlans = workSheetJSON.filter(data =>
  113. data.plan_code.startsWith('group')
  114. )
  115. const sizes = ['2', '3', '4', '5', '10', '20', '50']
  116. const result = {}
  117. for (const type1 of ['educational', 'enterprise']) {
  118. result[type1] = {}
  119. for (const type2 of ['professional', 'collaborator']) {
  120. result[type1][type2] = {}
  121. for (const currency of currencies) {
  122. result[type1][type2][currency] = {}
  123. for (const size of sizes) {
  124. const planCode = `group_${type2}_${size}_${type1}`
  125. const plan = groupPlans.find(data => data.plan_code === planCode)
  126. if (!plan) throw new Error(`Missing plan: ${planCode}`)
  127. result[type1][type2][currency][size] = {
  128. price_in_cents: plan[currency] * 100,
  129. }
  130. }
  131. }
  132. }
  133. }
  134. return result
  135. }
  136. const argv = minimist(process.argv.slice(2), {
  137. string: ['output', 'file'],
  138. alias: { o: 'output', f: 'file' },
  139. })
  140. let input
  141. if (argv.file) {
  142. const ext = path.extname(argv.file)
  143. switch (ext) {
  144. case '.csv':
  145. input = readCSVFile(argv.file)
  146. break
  147. case '.json':
  148. input = readJSONFile(argv.file)
  149. break
  150. default:
  151. console.log('Invalid file type: must be csv or json')
  152. }
  153. } else {
  154. console.log('usage: node plans.mjs -f <file.csv|file.json> -o <dir>')
  155. process.exit(1)
  156. }
  157. // removes quotes from object keys
  158. const formatJS = obj =>
  159. JSON.stringify(obj, null, 2).replace(/"([^"]+)":/g, '$1:')
  160. const formatJSON = obj => JSON.stringify(obj, null, 2)
  161. function writeFile(outputFile, data) {
  162. console.log(`Writing ${outputFile}`)
  163. fs.writeFileSync(outputFile, data)
  164. }
  165. const localizedPlanPricing = generatePlans(input)
  166. const groupPlans = generateGroupPlans(input)
  167. if (argv.output) {
  168. const dir = argv.output
  169. // check if output directory exists
  170. if (!fs.existsSync(dir)) {
  171. console.log(`Creating output directory ${dir}`)
  172. fs.mkdirSync(dir)
  173. }
  174. // check if output directory is a directory and report error if not
  175. if (!fs.lstatSync(dir).isDirectory()) {
  176. console.error(`Error: output dir ${dir} is not a directory`)
  177. process.exit(1)
  178. }
  179. writeFile(`${dir}/localizedPlanPricing.json`, formatJS(localizedPlanPricing))
  180. writeFile(`${dir}/groups.json`, formatJSON(groupPlans))
  181. } else {
  182. console.log('LOCALIZED', localizedPlanPricing)
  183. console.log('GROUP PLANS', JSON.stringify(groupPlans, null, 2))
  184. }
  185. console.log('Completed!')