Files
gruperly/apps/backend/src/app.ts
Jose Selesan aac142d324 feat(billing): configurable plans from the backoffice and MP payment notifications
Plans:
- Plan limits and Pro prices per country live in the database
  (plan_configs, plan_prices, seeded with the previous constants) and are
  read through PlanCatalog (30 s cache, defaults as fallback).
- /admin/plans edits limits (with the count of accounts that would become
  read-only) and prices, optionally pushing the new price to live
  subscriptions from their next charge (MP preapproval amount, Stripe item
  without proration) via the ApplyPriceChanges job, with an email and a
  notice on /billing.
- GET /billing/plans feeds the onboarding and the billing screen.

Mercado Pago:
- The Pro checkout returns through GET /billing/return on API_PUBLIC_URL,
  since MP refuses a back_url on localhost.
- The OAuth callback bounces from the tunnel to BETTER_AUTH_URL to find the
  session, requires the session to own the state, and redirects instead of
  answering JSON on unexpected errors.
- Checkout Pro links skip auto_return off public HTTPS and carry a
  notification_url to POST /payments/webhooks/mp/:paymentId, which confirms
  the payment with the owner's token (same rule as the polling job, which
  stays as fallback). The search now prefers the approved attempt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 07:50:14 -03:00

88 lines
3.4 KiB
TypeScript

import { Hono } from 'hono';
import type { BackendEnv } from '@/http/env';
import {
internalServerErrorProblem,
notFoundProblem,
problemJson,
} from '@/http/problem-details';
import { requestIdMiddleware } from '@/http/request-id';
import { requestLoggerMiddleware } from '@/http/request-logger';
import { corsMiddleware, securityHeadersMiddleware } from '@/http/security-headers';
import { sessionAuthMiddleware } from '@/http/session-auth';
import { logger } from '@/logger';
import { adminRoutes } from './modules/admin';
import {
groupsRiskOverviewRoute,
studentStatusRoutes,
} from './modules/analytics';
import { attendeesRoutes } from './modules/attendees';
import { attentionRoutes } from './modules/attention';
import { authRoutes } from './modules/auth';
import { billingRoutes, planReadOnlyMiddleware } from './modules/billing';
import { classesRoutes } from './modules/classes';
import { groupsRoutes } from './modules/groups';
import { healthCheckRoutes } from './modules/health-check';
import { homeRoutes } from './modules/home';
import { invitationsRoutes } from './modules/invitations';
import { mpRoutes } from './modules/mp';
import { mpCallbackOriginMiddleware } from './modules/mp/lib';
import { onboardingRoutes } from './modules/onboarding';
import { paymentsRoutes } from './modules/payments';
import { peopleRoutes } from './modules/people';
import { studentPortalRoutes } from './modules/student-portal';
import { waitlistRoutes } from './modules/waitlist';
import { whatsappRoutes } from './modules/whatsapp';
const app = new Hono<BackendEnv>();
app.use('*', corsMiddleware);
app.use('*', securityHeadersMiddleware);
app.use('*', requestIdMiddleware);
app.use('*', requestLoggerMiddleware);
app.get('/', (c) => c.json({ name: 'Gruperly API', status: 'ok' }));
const api = app.basePath('/api/v1');
// Local development: the MP callback comes back through the tunnel, without the
// session cookie of localhost. Replay it there before asking for a session.
api.use('/mp/callback', mpCallbackOriginMiddleware);
api.use('*', sessionAuthMiddleware);
// Read-only accounts (Free over its limits) cannot write domain data. Billing,
// onboarding and the MP/WhatsApp links stay open: that is how they get out of it.
const planReadOnly = planReadOnlyMiddleware();
for (const prefix of ['/groups', '/attendees', '/payments', '/classes', '/students', '/waitlist']) {
api.use(`${prefix}/*`, planReadOnly);
}
api.route('/admin', adminRoutes);
api.route('/auth', authRoutes);
api.route('/health', healthCheckRoutes);
api.route('/invitations', invitationsRoutes);
api.route('/groups', groupsRoutes);
api.route('/home', homeRoutes);
api.route('/onboarding', onboardingRoutes);
api.route('/mp', mpRoutes);
api.route('/whatsapp', whatsappRoutes);
api.route('/attendees', attendeesRoutes);
api.route('/payments', paymentsRoutes);
api.route('/billing', billingRoutes);
api.route('/waitlist', waitlistRoutes);
api.route('/classes', classesRoutes);
api.route('/students', studentStatusRoutes);
api.route('/groups-risk', groupsRiskOverviewRoute);
api.route('/attention', attentionRoutes);
api.route('/people', peopleRoutes);
api.route('/student-portal', studentPortalRoutes);
app.notFound((c) => {
return problemJson(c, notFoundProblem(c.req.path));
});
app.onError((error, c) => {
logger.error({ error, path: c.req.path }, 'Unhandled request error');
return problemJson(c, internalServerErrorProblem(c.req.path));
});
export default app;