Merge subscriptions
This commit is contained in:
@@ -5,6 +5,7 @@ import ky, {KyInstance} from 'ky'
|
|||||||
import Stripe from 'stripe'
|
import Stripe from 'stripe'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
mergeSubscriptions,
|
||||||
normalizeEntitlements,
|
normalizeEntitlements,
|
||||||
normalizeSubscriptions,
|
normalizeSubscriptions,
|
||||||
RevenueCatSubscription,
|
RevenueCatSubscription,
|
||||||
@@ -147,8 +148,10 @@ export default new Hono<{Bindings: Bindings; Variables: Variables}>()
|
|||||||
}
|
}
|
||||||
|
|
||||||
return c.json({
|
return c.json({
|
||||||
active: activeSubscriptions,
|
subscriptions: mergeSubscriptions(
|
||||||
available: platformSubscriptions,
|
platformSubscriptions,
|
||||||
|
activeSubscriptions,
|
||||||
|
),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
+28
-1
@@ -101,6 +101,7 @@ export type Entitlement = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export type SubscriptionState = {
|
export type SubscriptionState = {
|
||||||
|
platform: 'web' | 'android' | 'ios'
|
||||||
active: boolean
|
active: boolean
|
||||||
entitlements: {id: EntitlementId}[]
|
entitlements: {id: EntitlementId}[]
|
||||||
status:
|
status:
|
||||||
@@ -186,7 +187,9 @@ export function normalizeSubscriptions(
|
|||||||
const id = normalizeSubscriptionId(product.store_identifier)
|
const id = normalizeSubscriptionId(product.store_identifier)
|
||||||
if (!id) continue
|
if (!id) continue
|
||||||
const platform = getSubscriptionPlatform(product.store_identifier)
|
const platform = getSubscriptionPlatform(product.store_identifier)
|
||||||
const state = subscription ? getSubscriptionState(subscription) : undefined
|
const state = subscription
|
||||||
|
? getSubscriptionState(subscription, {platform})
|
||||||
|
: undefined
|
||||||
const interval = getSubscriptionInterval(id)
|
const interval = getSubscriptionInterval(id)
|
||||||
|
|
||||||
if (platform === 'web') {
|
if (platform === 'web') {
|
||||||
@@ -237,6 +240,26 @@ export function normalizeSubscriptions(
|
|||||||
return normalized
|
return normalized
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function mergeSubscriptions(
|
||||||
|
all: Subscription[],
|
||||||
|
active: Subscription[],
|
||||||
|
): Subscription[] {
|
||||||
|
const merged: Subscription[] = []
|
||||||
|
|
||||||
|
for (const a of active) {
|
||||||
|
for (const s of all) {
|
||||||
|
if (a.id === s.id) {
|
||||||
|
// @ts-ignore
|
||||||
|
merged.push({...s, state: a.state})
|
||||||
|
} else {
|
||||||
|
merged.push(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return merged
|
||||||
|
}
|
||||||
|
|
||||||
export function getSubscriptionInterval(
|
export function getSubscriptionInterval(
|
||||||
id: SubscriptionId,
|
id: SubscriptionId,
|
||||||
): 'monthly' | 'annual' {
|
): 'monthly' | 'annual' {
|
||||||
@@ -254,8 +277,12 @@ export function getSubscriptionInterval(
|
|||||||
|
|
||||||
export function getSubscriptionState(
|
export function getSubscriptionState(
|
||||||
subscription: RevenueCatSubscription,
|
subscription: RevenueCatSubscription,
|
||||||
|
options: {
|
||||||
|
platform: 'web' | 'android' | 'ios'
|
||||||
|
},
|
||||||
): SubscriptionState {
|
): SubscriptionState {
|
||||||
return {
|
return {
|
||||||
|
platform: options.platform,
|
||||||
active: subscription.gives_access,
|
active: subscription.gives_access,
|
||||||
status: subscription.status,
|
status: subscription.status,
|
||||||
renewalStatus: subscription.auto_renewal_status,
|
renewalStatus: subscription.auto_renewal_status,
|
||||||
|
|||||||
Reference in New Issue
Block a user