Spaces:
Paused
Paused
|
Download apps/prevision-api/README.md from modventures/prevision: direct link, hf CLI and curl.
- Browser
- Download file 6.12 kB
-
https://huggingface.co/spaces/modventures/prevision/resolve/main/apps/prevision-api/README.md
- Command line
-
hf download hf://spaces/modventures/prevision/apps/prevision-api/README.md
-
curl -L -o README.md https://huggingface.co/spaces/modventures/prevision/resolve/main/apps/prevision-api/README.md
6.12 kB
| # Prevision API | |
| NestJS 12 + Prisma 7 (PostgreSQL). Serves the Prevision web app at `/api/*`: | |
| | Route | | | |
| |---|---| | |
| | `GET /api/me` | the signed-in account and its free tries `{limit, used, remaining}` | | |
| | `GET /api/tiles`, `/api/tiles/facets`, `/api/tiles/:slug` | the studio's tile library (paginated list, facet counts, one tile) | | |
| | `POST /api/processing-token` | a 10-minute token for the renderer (issuer `prevision-api`) | | |
| | `POST /internal/generations`, `/internal/generations/:id/refund` | the renderer charging and refunding a free try (internal token) | | |
| | `GET /api/subscriptions/current` | the active plan, or null | | |
| | `POST /api/subscriptions/checkout` `{plan}` | a Stripe Checkout URL for Starter or Growth | | |
| | `POST /api/subscriptions/portal` | a Stripe billing portal URL | | |
| | `POST /api/webhooks/stripe` | Stripe events (signature checked), keeps `subscriptions` in sync | | |
| | `GET /health`, `/ready` | liveness, database readiness | | |
| ## Responses | |
| Success: `{ "success": true, "data": ..., "meta"?: { "page", "pageSize", "total", "totalPages" } }` (`meta` on paginated lists). Error: `{ "success": false, "error": { "code", "message" } }`. `ResponseInterceptor` and `HttpExceptionFilter` in `src/common` produce both. | |
| ## Layout | |
| ``` | |
| src/ | |
| common/ interceptors, filters, exceptions, pagination DTOs, shared interfaces | |
| config/ env loading (APP_CONFIG) and its interface | |
| prisma/ PrismaService, soft-delete extension | |
| clerk/ Clerk token verification and profile lookup | |
| users/ account from a Clerk sign-in, ClerkAuthGuard, GET /me | |
| generations/ free tries (renderer charges/refunds via /internal) | |
| catalog/ seeds the tiles table from prisma/catalog.json | |
| tiles/ tile library reads | |
| scripts/ seed-catalog, set-user-role (run from dist/) | |
| rendering/ renderer processing tokens | |
| stripe/ the shared Stripe client | |
| subscriptions/ checkout, billing portal, current plan; the only writer of subscriptions | |
| webhooks/ Stripe webhook: signature guard, event → subscription sync | |
| health/ /health, /ready | |
| test/ e2e specs per feature (real Postgres) | |
| ``` | |
| ## Auth | |
| Clerk owns sign-in. Every `/api` route needs `Authorization: Bearer <Clerk session token>`; `ClerkAuthGuard` verifies it (signature, expiry, `azp` in `PREVISION_ALLOWED_ORIGINS`) and loads the account, creating it on the first request. No waitlist, approval or onboarding. | |
| `CLERK_SECRET_KEY` is a comma-separated list, one key per Clerk instance (staging: `sk_live_…,sk_test_…`). `ClerkService` accepts a token signed by any of them and looks a new user up in whichever instance owns it, so the custom domain (production instance) and `vercel.app` previews (development instance) share one API. Accounts are matched by Clerk id, then by verified email, so the same person signing in through both instances gets one account. | |
| ## Free tries | |
| Each account gets `FREE_GENERATIONS` (10) room-photo visualizations; admins are unlimited. The renderer calls `POST /internal/generations` before accepting a job from a Prevision token; `GenerationsService.create` locks the user row, counts `generations` without `refunded_at` and answers 402 `NO_FREE_TRIES` at the limit. A failed job is refunded once (`refunded_at`). Sample rooms are never charged. | |
| ## Billing (Stripe) | |
| Plans: Starter $70/month (`STARTER_MONTHLY_GENERATIONS`, 150) and Growth $140/month (`GROWTH_MONTHLY_GENERATIONS`, 600). `subscriptions/` creates Checkout sessions (Prevision branding, USD only) and billing-portal sessions; `webhooks/` verifies the signature on the raw body and re-syncs the subscription from Stripe for `checkout.session.completed` and `customer.subscription.created|updated|deleted`. The allowance (`generations/allowance.ts`) is: admin unlimited, active subscription = monthly plan limit since `current_period_start`, otherwise the lifetime free tries. | |
| Local testing: `stripe listen --forward-to localhost:<port>/api/webhooks/stripe` and put the printed `whsec_…` in `STRIPE_WEBHOOK_SECRET`; pay with card `4242 4242 4242 4242`. Staging's endpoint is `we_1UMUCpFuPzalTB3Q6226EaDs` (pod :8001); its secrets come from `STRIPE_SECRET_KEY_STAGING` / `STRIPE_WEBHOOK_SECRET_STAGING`. | |
| ## Database | |
| Prevision has its own database (`PREVISION_DATABASE_URL`, Supabase project `mceapsmuhgupvksxmtis` on staging, session pooler URL), fully owned by Prisma: `users` (with `role`: `user` or `admin`, `stripe_customer_id`), `subscriptions`, `generations` and `tiles`. Migrations live in `prisma/migrations`; RLS is enabled on every table. The tiles are the 19 shared demo tiles in `prisma/catalog.json`, upserted on every start. Users and tiles are soft-deleted (`deleted_at`), never removed; `PrismaService.db` hides them automatically. | |
| ## Admins | |
| No admin panel: an admin is a user with `role = admin`, which removes the free-try limit. After the person has signed in once: `pnpm build && pnpm user:role <email> admin` (or `user` to revert), with `PREVISION_DATABASE_URL` set. | |
| ## Commands | |
| ```bash | |
| pnpm dev # nest start --watch, port PREVISION_API_PORT (8001) | |
| pnpm generate # prisma generate (turbo runs it before build, lint and test) | |
| pnpm build # nest build | |
| pnpm lint # oxlint + tsc | |
| pnpm test # unit + e2e (real Postgres; TEST_DATABASE_URL, default localhost:55432/prevision_api_test) | |
| pnpm db:migrate # prisma migrate dev (review the SQL; enable RLS on new tables) | |
| pnpm db:deploy # prisma migrate deploy (the pod runs this on start) | |
| ``` | |
| Local `.env` (git-ignored): `PREVISION_DATABASE_URL`, `CLERK_SECRET_KEY` (comma-separated to accept several Clerk instances, e.g. `sk_live_…,sk_test_…`), `PROCESSING_TOKEN_SECRET`, `PREVISION_ALLOWED_ORIGINS`, `STRIPE_SECRET_KEY`, `STRIPE_WEBHOOK_SECRET`, `STRIPE_PRICE_STARTER`, `STRIPE_PRICE_GROWTH`, optionally `PREVISION_API_PORT`, `PREVISION_APP_URL`, `STRIPE_LOGO_URL`, `STRIPE_PORTAL_CONFIGURATION_ID`, `CLERK_JWT_KEY`. Full list: `src/config/app-config.ts`. | |
| ## Deployment | |
| See [the deployment runbook](../../docs/DEPLOYMENT.md). Backend builds and deployments are manual. | |