MCPcopy Index your code
hub / github.com/simstudioai/sim / handleSubscriptionCreated

Function handleSubscriptionCreated

apps/sim/lib/billing/webhooks/subscription.ts:202–270  ·  view source on GitHub ↗
(subscriptionData: {
  id: string
  referenceId: string
  plan: string | null
  status: string
  periodStart?: Date | null
  periodEnd?: Date | null
})

Source from the content-addressed store, hash-verified

200 * Handle new subscription creation - reset usage if transitioning from free to paid
201 */
202export async function handleSubscriptionCreated(subscriptionData: {
203 id: string
204 referenceId: string
205 plan: string | null
206 status: string
207 periodStart?: Date | null
208 periodEnd?: Date | null
209}) {
210 try {
211 const otherActiveSubscriptions = await db
212 .select()
213 .from(subscription)
214 .where(
215 and(
216 eq(subscription.referenceId, subscriptionData.referenceId),
217 inArray(subscription.status, ENTITLED_SUBSCRIPTION_STATUSES),
218 ne(subscription.id, subscriptionData.id) // Exclude current subscription
219 )
220 )
221
222 const wasFreePreviously = otherActiveSubscriptions.length === 0
223 const isPaidPlan = isPaid(subscriptionData.plan)
224
225 if (wasFreePreviously && isPaidPlan) {
226 logger.info('Detected free -> paid transition, resetting usage', {
227 subscriptionId: subscriptionData.id,
228 referenceId: subscriptionData.referenceId,
229 plan: subscriptionData.plan,
230 })
231
232 await resetUsageForSubscription({
233 plan: subscriptionData.plan,
234 referenceId: subscriptionData.referenceId,
235 periodStart: subscriptionData.periodStart ?? null,
236 periodEnd: subscriptionData.periodEnd ?? null,
237 })
238
239 logger.info('Successfully reset usage for free -> paid transition', {
240 subscriptionId: subscriptionData.id,
241 referenceId: subscriptionData.referenceId,
242 plan: subscriptionData.plan,
243 })
244 } else {
245 logger.info('No usage reset needed', {
246 subscriptionId: subscriptionData.id,
247 referenceId: subscriptionData.referenceId,
248 plan: subscriptionData.plan,
249 wasFreePreviously,
250 isPaidPlan,
251 otherActiveSubscriptionsCount: otherActiveSubscriptions.length,
252 })
253 }
254
255 if (wasFreePreviously && isPaidPlan) {
256 captureServerEvent(subscriptionData.referenceId, 'subscription_created', {
257 plan: subscriptionData.plan ?? 'unknown',
258 status: subscriptionData.status,
259 reference_id: subscriptionData.referenceId,

Callers 1

auth.tsFile · 0.90

Calls 6

isPaidFunction · 0.90
captureServerEventFunction · 0.90
infoMethod · 0.80
errorMethod · 0.80
eqFunction · 0.50

Tested by

no test coverage detected