plans.mjs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 additionalLicenseAddOnLegacyPricesFilePath = path.resolve(
  117. __dirname,
  118. 'additional-license-add-on-legacy-prices.json'
  119. )
  120. const additionalLicenseAddOnLegacyPricesFile = fs.readFileSync(
  121. additionalLicenseAddOnLegacyPricesFilePath
  122. )
  123. const additionalLicenseAddOnLegacyPrices = JSON.parse(
  124. additionalLicenseAddOnLegacyPricesFile
  125. )
  126. const result = {}
  127. for (const type1 of ['educational', 'enterprise']) {
  128. result[type1] = {}
  129. for (const type2 of ['professional', 'collaborator']) {
  130. result[type1][type2] = {}
  131. for (const currency of currencies) {
  132. result[type1][type2][currency] = {}
  133. for (const size of sizes) {
  134. const planCode = `group_${type2}_${size}_${type1}`
  135. const plan = groupPlans.find(data => data.plan_code === planCode)
  136. if (!plan) throw new Error(`Missing plan: ${planCode}`)
  137. result[type1][type2][currency][size] = {
  138. price_in_cents: plan[currency] * 100,
  139. }
  140. const additionalLicenseAddOnLegacyPrice =
  141. additionalLicenseAddOnLegacyPrices[type1][type2][size]?.[currency]
  142. if (additionalLicenseAddOnLegacyPrice) {
  143. Object.assign(result[type1][type2][currency][size], {
  144. additional_license_legacy_price_in_cents:
  145. additionalLicenseAddOnLegacyPrice * 100,
  146. })
  147. }
  148. }
  149. }
  150. }
  151. }
  152. return result
  153. }
  154. const argv = minimist(process.argv.slice(2), {
  155. string: ['output', 'file'],
  156. alias: { o: 'output', f: 'file' },
  157. })
  158. let input
  159. if (argv.file) {
  160. const ext = path.extname(argv.file)
  161. switch (ext) {
  162. case '.csv':
  163. input = readCSVFile(argv.file)
  164. break
  165. case '.json':
  166. input = readJSONFile(argv.file)
  167. break
  168. default:
  169. console.log('Invalid file type: must be csv or json')
  170. }
  171. } else {
  172. console.log('usage: node plans.mjs -f <file.csv|file.json> -o <dir>')
  173. process.exit(1)
  174. }
  175. // removes quotes from object keys
  176. const formatJS = obj =>
  177. JSON.stringify(obj, null, 2).replace(/"([^"]+)":/g, '$1:')
  178. const formatJSON = obj => JSON.stringify(obj, null, 2)
  179. function writeFile(outputFile, data) {
  180. console.log(`Writing ${outputFile}`)
  181. fs.writeFileSync(outputFile, data)
  182. }
  183. const localizedPlanPricing = generatePlans(input)
  184. const groupPlans = generateGroupPlans(input)
  185. if (argv.output) {
  186. const dir = argv.output
  187. // check if output directory exists
  188. if (!fs.existsSync(dir)) {
  189. console.log(`Creating output directory ${dir}`)
  190. fs.mkdirSync(dir)
  191. }
  192. // check if output directory is a directory and report error if not
  193. if (!fs.lstatSync(dir).isDirectory()) {
  194. console.error(`Error: output dir ${dir} is not a directory`)
  195. process.exit(1)
  196. }
  197. writeFile(`${dir}/localizedPlanPricing.json`, formatJS(localizedPlanPricing))
  198. writeFile(`${dir}/groups.json`, formatJSON(groupPlans))
  199. } else {
  200. console.log('LOCALIZED', localizedPlanPricing)
  201. console.log('GROUP PLANS', JSON.stringify(groupPlans, null, 2))
  202. }
  203. console.log('Completed!')