Improve /subscriptions/:offering and /entitlements endpoints
This commit is contained in:
+42
-20
@@ -7,7 +7,6 @@ import Stripe from 'stripe'
|
|||||||
import {
|
import {
|
||||||
getMainSubscriptionProducts,
|
getMainSubscriptionProducts,
|
||||||
normalizeEntitlements,
|
normalizeEntitlements,
|
||||||
RevenueCatProduct,
|
|
||||||
RevenueCatSubscription,
|
RevenueCatSubscription,
|
||||||
} from './util'
|
} from './util'
|
||||||
|
|
||||||
@@ -59,35 +58,51 @@ export default new Hono<{Bindings: Bindings; Variables: Variables}>()
|
|||||||
})
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get main subscription products
|
* Get subscription products for a given offering
|
||||||
*/
|
*/
|
||||||
.get('/subscriptions/main', async c => {
|
.get('/subscriptions/:offering_lookup_key', async c => {
|
||||||
const rc = c.get('rcv2')
|
const rc = c.get('rcv2')
|
||||||
const {user, platform} = c.req.query()
|
const {user, platform} = c.req.query()
|
||||||
|
const {offering_lookup_key} = c.req.param()
|
||||||
|
|
||||||
const allProducts = await rc
|
const {items} = await rc
|
||||||
.get<{items: RevenueCatProduct[]}>(`products?expand=items.app`)
|
.get<{items: any[]}>(`offerings?expand=items.package.product`)
|
||||||
.json()
|
.json()
|
||||||
|
const offering = items.find(i => i.lookup_key == offering_lookup_key)
|
||||||
|
|
||||||
|
if (!offering) {
|
||||||
|
throw new HTTPException(404, {message: `Offering not found`})
|
||||||
|
}
|
||||||
|
|
||||||
|
const products = offering.packages.items
|
||||||
|
.flatMap((i: any) => i.products.items)
|
||||||
|
.map((i: any) => i.product)
|
||||||
|
|
||||||
let subscriptions: any[] = []
|
let subscriptions: any[] = []
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const {items} = await rc
|
const {items} = await rc
|
||||||
.get<{items: RevenueCatSubscription[]}>(
|
.get<{
|
||||||
`customers/${user}/subscriptions`,
|
items: RevenueCatSubscription[]
|
||||||
)
|
}>(`customers/${user}/subscriptions`)
|
||||||
.json()
|
.json()
|
||||||
subscriptions = items
|
subscriptions = items
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
|
|
||||||
let products = getMainSubscriptionProducts(allProducts.items, subscriptions)
|
let normalizedProducts = getMainSubscriptionProducts(
|
||||||
const activeProducts = products.filter(p => p.active)
|
products,
|
||||||
|
subscriptions,
|
||||||
|
)
|
||||||
|
const activeProducts = normalizedProducts.filter(p => p.active)
|
||||||
|
|
||||||
if (platform === 'web') {
|
if (platform === 'web') {
|
||||||
const stripe = c.get('stripe')
|
const stripe = c.get('stripe')
|
||||||
|
|
||||||
products = products.filter(p => p.provider === 'stripe')
|
normalizedProducts = normalizedProducts.filter(
|
||||||
|
p => p.provider === 'stripe',
|
||||||
|
)
|
||||||
|
|
||||||
const ids = products.map(p => p.storeId)
|
const ids = normalizedProducts.map(p => p.storeId)
|
||||||
const {data} = await stripe.products.list({ids})
|
const {data} = await stripe.products.list({ids})
|
||||||
const prices = await Promise.all(
|
const prices = await Promise.all(
|
||||||
data.map(p => {
|
data.map(p => {
|
||||||
@@ -101,7 +116,7 @@ export default new Hono<{Bindings: Bindings; Variables: Variables}>()
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
for (const product of products) {
|
for (const product of normalizedProducts) {
|
||||||
if (product.provider !== 'stripe') continue
|
if (product.provider !== 'stripe') continue
|
||||||
for (const price of prices) {
|
for (const price of prices) {
|
||||||
if (!price) continue
|
if (!price) continue
|
||||||
@@ -114,10 +129,12 @@ export default new Hono<{Bindings: Bindings; Variables: Variables}>()
|
|||||||
}
|
}
|
||||||
} else if (platform === 'ios') {
|
} else if (platform === 'ios') {
|
||||||
} else if (platform === 'android') {
|
} else if (platform === 'android') {
|
||||||
products = products.filter(p => p.provider === 'play_store')
|
normalizedProducts = normalizedProducts.filter(
|
||||||
|
p => p.provider === 'play_store',
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.json({active: activeProducts, available: products})
|
return c.json({active: activeProducts, available: normalizedProducts})
|
||||||
})
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -158,14 +175,19 @@ export default new Hono<{Bindings: Bindings; Variables: Variables}>()
|
|||||||
const rc = c.get('rcv2')
|
const rc = c.get('rcv2')
|
||||||
const user = c.req.query('user')
|
const user = c.req.query('user')
|
||||||
|
|
||||||
const {items} = await rc
|
const {items: activeEntitlements} = await rc
|
||||||
.get<{items: RevenueCatSubscription[]}>(`customers/${user}/subscriptions`)
|
.get<{
|
||||||
|
items: {entitlement_id: string}[]
|
||||||
|
}>(`customers/${user}/active_entitlements`)
|
||||||
.json()
|
.json()
|
||||||
const entitlements = normalizeEntitlements(
|
const entitlements = await Promise.all(
|
||||||
items.flatMap(sub => sub.entitlements.items),
|
activeEntitlements.map(e => {
|
||||||
|
return rc.get(`entitlements/${e.entitlement_id}?expand=product`).json()
|
||||||
|
}),
|
||||||
)
|
)
|
||||||
|
const normalized = normalizeEntitlements(entitlements)
|
||||||
|
|
||||||
return c.json({entitlements})
|
return c.json({entitlements: normalized})
|
||||||
})
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -477,9 +477,7 @@ export enum EntitlementId {
|
|||||||
Main2 = 'main:2',
|
Main2 = 'main:2',
|
||||||
}
|
}
|
||||||
|
|
||||||
export function normalizeEntitlements(
|
export function normalizeEntitlements(entitlements: any[]) {
|
||||||
entitlements: RevenueCatSubscription['entitlements']['items'],
|
|
||||||
) {
|
|
||||||
return entitlements.map(entitlement => {
|
return entitlements.map(entitlement => {
|
||||||
let id = EntitlementId.Main0
|
let id = EntitlementId.Main0
|
||||||
|
|
||||||
@@ -500,6 +498,7 @@ export function normalizeEntitlements(
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
id,
|
id,
|
||||||
|
products: entitlement.products.items.map((p: any) => p.store_identifier),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user