( org: SubscriptionStateInput )
| 26 | } |
| 27 | |
| 28 | export function getSubscriptionState( |
| 29 | org: SubscriptionStateInput |
| 30 | ): SubscriptionState { |
| 31 | if (process.env.SELF_HOSTED === 'true') { |
| 32 | return 'self_hosted'; |
| 33 | } |
| 34 | |
| 35 | const now = new Date(); |
| 36 | const { subscriptionStatus, subscriptionCanceledAt, subscriptionEndsAt } = org; |
| 37 | const endsInFuture = Boolean(subscriptionEndsAt && subscriptionEndsAt > now); |
| 38 | |
| 39 | switch (subscriptionStatus) { |
| 40 | case null: |
| 41 | case 'trialing': |
| 42 | return endsInFuture ? 'trialing' : 'trial_expired'; |
| 43 | case 'active': |
| 44 | // Cancel-at-period-end keeps the Polar status as `active` while |
| 45 | // `canceledAt` is set; the subscription stays usable until it expires. |
| 46 | if (subscriptionCanceledAt) { |
| 47 | return 'canceling'; |
| 48 | } |
| 49 | return subscriptionEndsAt && subscriptionEndsAt <= now |
| 50 | ? 'expired' |
| 51 | : 'active'; |
| 52 | case 'past_due': |
| 53 | return 'past_due'; |
| 54 | case 'unpaid': |
| 55 | return 'unpaid'; |
| 56 | case 'incomplete': |
| 57 | return 'incomplete'; |
| 58 | case 'incomplete_expired': |
| 59 | return 'expired'; |
| 60 | case 'canceled': |
| 61 | return 'canceled'; |
| 62 | default: |
| 63 | return 'expired'; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Whether a given state should block access to the dashboard (and force the |
no outgoing calls
no test coverage detected