Spaces:
Paused
Paused
Download apps/prevision-api/src/webhooks/webhooks.service.ts from modventures/prevision: direct link, hf CLI and curl.
- Browser
- Download file 1.31 kB
-
https://huggingface.co/spaces/modventures/prevision/resolve/main/apps/prevision-api/src/webhooks/webhooks.service.ts
- Command line
-
hf download hf://spaces/modventures/prevision/apps/prevision-api/src/webhooks/webhooks.service.ts
-
curl -L -o webhooks.service.ts https://huggingface.co/spaces/modventures/prevision/resolve/main/apps/prevision-api/src/webhooks/webhooks.service.ts
1.31 kB
| import { Injectable } from '@nestjs/common'; | |
| import type Stripe from 'stripe'; | |
| import { SubscriptionsService } from '../subscriptions/subscriptions.service.js'; | |
| export const HANDLED_EVENTS = [ | |
| 'checkout.session.completed', | |
| 'customer.subscription.created', | |
| 'customer.subscription.updated', | |
| 'customer.subscription.deleted', | |
| ] as const; | |
| () | |
| export class WebhooksService { | |
| constructor(private readonly subscriptions: SubscriptionsService) {} | |
| /** Every handled event resolves to "sync this subscription from Stripe", so retries and reordering are harmless. */ | |
| async handle(event: Stripe.Event): Promise<void> { | |
| const subscriptionId = this.subscriptionIdOf(event); | |
| if (subscriptionId) await this.subscriptions.syncFromStripe(subscriptionId); | |
| } | |
| private subscriptionIdOf(event: Stripe.Event): string | null { | |
| switch (event.type) { | |
| case 'checkout.session.completed': { | |
| const { subscription } = event.data.object; | |
| if (!subscription) return null; | |
| return typeof subscription === 'string' ? subscription : subscription.id; | |
| } | |
| case 'customer.subscription.created': | |
| case 'customer.subscription.updated': | |
| case 'customer.subscription.deleted': | |
| return event.data.object.id; | |
| default: | |
| return null; | |
| } | |
| } | |
| } | |