setup-subscription-dash-context.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import _ from 'lodash'
  2. import { groupPriceByUsageTypeAndSize, plans } from '../fixtures/plans'
  3. import { MetaTag } from '@/utils/meta'
  4. export function setupSubscriptionDashContext(options?: {
  5. metaTags?: MetaTag[]
  6. recurlyNotLoaded?: boolean
  7. queryingRecurly?: boolean
  8. currencyCode?: string
  9. }) {
  10. options?.metaTags?.forEach(tag =>
  11. window.metaAttributesCache.set(tag!.name, tag!.value)
  12. )
  13. window.metaAttributesCache.set('ol-user', {})
  14. if (!options?.recurlyNotLoaded) {
  15. // @ts-ignore
  16. global.recurly = {
  17. configure: () => {},
  18. Pricing: {
  19. Subscription: () => {
  20. return {
  21. plan: (planCode: string) => {
  22. let plan
  23. const isGroupPlan = planCode.includes('group')
  24. if (isGroupPlan) {
  25. const [, planType, size, usage] = planCode.split('_')
  26. const currencyCode = options?.currencyCode || 'USD'
  27. plan = _.get(groupPriceByUsageTypeAndSize, [
  28. usage,
  29. planType,
  30. currencyCode,
  31. size,
  32. ])
  33. } else {
  34. plan = plans.find(p => p.planCode === planCode)
  35. }
  36. const response = {
  37. next: {
  38. total: plan?.price_in_cents
  39. ? plan.price_in_cents / 100
  40. : undefined,
  41. },
  42. }
  43. return {
  44. currency: () => {
  45. return {
  46. catch: () => {
  47. return {
  48. done: (callback: (response: object) => void) => {
  49. if (!options?.queryingRecurly) {
  50. return callback(response)
  51. }
  52. },
  53. }
  54. },
  55. }
  56. },
  57. }
  58. },
  59. }
  60. },
  61. },
  62. }
  63. }
  64. }