generate_addon_prices.mjs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // @ts-check
  2. import settings from '@overleaf/settings'
  3. import recurly from 'recurly'
  4. import { scriptRunner } from '../lib/ScriptRunner.mjs'
  5. const ADD_ON_CODE = process.argv[2]
  6. async function main() {
  7. if (!ADD_ON_CODE) {
  8. console.error('Missing add-on code')
  9. console.error(
  10. 'Usage: node scripts/recurly/generate_addon_prices.mjs ADD_ON_CODE'
  11. )
  12. process.exit(1)
  13. }
  14. /** @type {Record<string, any>} */
  15. const localizedAddOnsPricing = {}
  16. const monthlyPlan = await getPlan(ADD_ON_CODE)
  17. if (monthlyPlan == null) {
  18. console.error(`Monthly plan missing in Recurly: ${ADD_ON_CODE}`)
  19. process.exit(1)
  20. }
  21. for (const { currency, unitAmount } of monthlyPlan.currencies ?? []) {
  22. /** @type {any} */
  23. const curr = currency
  24. if (!localizedAddOnsPricing[curr]) {
  25. localizedAddOnsPricing[curr] = { [ADD_ON_CODE]: {} }
  26. }
  27. localizedAddOnsPricing[curr][ADD_ON_CODE].monthly = unitAmount
  28. }
  29. const annualPlan = await getPlan(`${ADD_ON_CODE}-annual`)
  30. if (annualPlan == null) {
  31. console.error(`Annual plan missing in Recurly: ${ADD_ON_CODE}-annual`)
  32. process.exit(1)
  33. }
  34. for (const { currency, unitAmount } of annualPlan.currencies ?? []) {
  35. /** @type {any} */
  36. const curr = currency
  37. if (!localizedAddOnsPricing[curr]) {
  38. localizedAddOnsPricing[curr] = { [ADD_ON_CODE]: {} }
  39. }
  40. localizedAddOnsPricing[curr][ADD_ON_CODE].annual = unitAmount
  41. localizedAddOnsPricing[curr][ADD_ON_CODE].annualDividedByTwelve =
  42. (unitAmount || 0) / 12
  43. }
  44. console.log(JSON.stringify({ localizedAddOnsPricing }, null, 2))
  45. }
  46. /**
  47. * Get a plan configuration from Recurly
  48. *
  49. * @param {string} planCode
  50. */
  51. async function getPlan(planCode) {
  52. const recurlyClient = new recurly.Client(settings.apis.recurly.apiKey)
  53. return await recurlyClient.getPlan(`code-${planCode}`)
  54. }
  55. await scriptRunner(main)