feat(billing): planes Free/Pro con Suscripciones de Mercado Pago
- Módulo billing: suscripción Pro mensual vía /preapproval con credenciales de plataforma por país, webhook firmado y reconciliación por polling. - PlanGuard en los use-cases que crean grupos/alumnos, cobran con MP o activan automatizaciones (402 plan_limit_reached / plan_feature_required / plan_read_only), detrás de BILLING_ENFORCEMENT. - Onboarding: paso Plan obligatorio (POST /onboarding/plan); con Free se saltean Cobros y WhatsApp. - Web: pantalla /billing, aviso de Pro desde el MutationCache. - Migración billing_subscriptions y docs/billing.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
19
apps/backend/src/modules/billing/features/charges/route.ts
Normal file
19
apps/backend/src/modules/billing/features/charges/route.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import type { BillingChargesQuery } from '@gruperly/shared';
|
||||
import { BillingChargesQuerySchema } from '@gruperly/shared';
|
||||
import { Hono } from 'hono';
|
||||
import { problemJson, resultJson, unauthorizedProblem } from '@/http/problem-details';
|
||||
import { validate } from '@/http/validate';
|
||||
import { ListBillingCharges } from './use-case';
|
||||
|
||||
const route = new Hono();
|
||||
|
||||
route.get('/charges', validate.query(BillingChargesQuerySchema), async (c) => {
|
||||
const user = c.get('user');
|
||||
if (!user) return problemJson(c, unauthorizedProblem(c.req.path));
|
||||
|
||||
const query = c.req.valid('query') as BillingChargesQuery;
|
||||
const result = await new ListBillingCharges().execute(query, user.id);
|
||||
return resultJson(c, result);
|
||||
});
|
||||
|
||||
export default route;
|
||||
@@ -0,0 +1,36 @@
|
||||
import type { PrismaClient } from '@generated/prisma/client';
|
||||
import type { BillingChargeList, BillingChargesQuery, ProblemDetails, Result } from '@gruperly/shared';
|
||||
import { ok } from '@gruperly/shared';
|
||||
import { getPaginationMetadata, getPaginationOffset } from '@/lib/pagination';
|
||||
import prisma from '@/lib/prisma';
|
||||
import { toBillingChargeDto } from '../../lib';
|
||||
|
||||
type ListBillingChargesDeps = { db?: Pick<PrismaClient, 'billingCharge'> };
|
||||
|
||||
/** The professor's own Pro charges, newest first. */
|
||||
export class ListBillingCharges {
|
||||
constructor(private readonly deps: ListBillingChargesDeps = {}) {}
|
||||
|
||||
async execute(
|
||||
query: BillingChargesQuery,
|
||||
userId: string,
|
||||
): Promise<Result<BillingChargeList, ProblemDetails>> {
|
||||
const db = this.deps.db ?? prisma;
|
||||
const where = { subscription: { userId } };
|
||||
|
||||
const [charges, total] = await Promise.all([
|
||||
db.billingCharge.findMany({
|
||||
where,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
skip: getPaginationOffset(query),
|
||||
take: query.pageSize,
|
||||
}),
|
||||
db.billingCharge.count({ where }),
|
||||
]);
|
||||
|
||||
return ok({
|
||||
data: charges.map(toBillingChargeDto),
|
||||
pagination: getPaginationMetadata(query, total),
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user