Skip to content

Commit 873f4ca

Browse files
committed
fix(revenuecat): handle entitlement grace periods, guard one-of body params
1 parent f7d9c4f commit 873f4ca

3 files changed

Lines changed: 16 additions & 4 deletions

File tree

apps/sim/tools/revenuecat/defer_google_subscription.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,12 @@ export const revenuecatDeferGoogleSubscriptionTool: ToolConfig<
6060
'Content-Type': 'application/json',
6161
}),
6262
body: (params) => {
63+
if (params.extendByDays === undefined && params.expiryTimeMs === undefined) {
64+
throw new Error('Provide either extendByDays or expiryTimeMs to defer a subscription')
65+
}
6366
const body: Record<string, unknown> = {}
64-
if (params.extendByDays !== undefined) body.extend_by_days = params.extendByDays
6567
if (params.expiryTimeMs !== undefined) body.expiry_time_ms = params.expiryTimeMs
68+
else if (params.extendByDays !== undefined) body.extend_by_days = params.extendByDays
6669
return body
6770
},
6871
},

apps/sim/tools/revenuecat/get_customer.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,14 @@ export const revenuecatGetCustomerTool: ToolConfig<GetCustomerParams, CustomerRe
4747

4848
const now = requestDate ? new Date(requestDate).getTime() : Date.now()
4949
const activeEntitlements = Object.values(entitlements).filter((e: unknown) => {
50-
const expires = (e as Record<string, unknown>).expires_date as string | null | undefined
51-
return !expires || new Date(expires).getTime() > now
50+
const ent = e as Record<string, unknown>
51+
if (typeof ent.is_active === 'boolean') return ent.is_active
52+
const expires = ent.expires_date as string | null | undefined
53+
const grace = ent.grace_period_expires_date as string | null | undefined
54+
if (!expires) return true
55+
if (new Date(expires).getTime() > now) return true
56+
if (grace && new Date(grace).getTime() > now) return true
57+
return false
5258
}).length
5359
const activeSubscriptions = Object.keys(subscriptions).length
5460

apps/sim/tools/revenuecat/grant_entitlement.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,12 @@ export const revenuecatGrantEntitlementTool: ToolConfig<
6262
'Content-Type': 'application/json',
6363
}),
6464
body: (params) => {
65+
if (!params.duration && params.endTimeMs === undefined) {
66+
throw new Error('Provide either duration or endTimeMs to grant a promotional entitlement')
67+
}
6568
const body: Record<string, unknown> = {}
66-
if (params.duration) body.duration = params.duration
6769
if (params.endTimeMs !== undefined) body.end_time_ms = params.endTimeMs
70+
else if (params.duration) body.duration = params.duration
6871
if (params.startTimeMs !== undefined) body.start_time_ms = params.startTimeMs
6972
return body
7073
},

0 commit comments

Comments
 (0)