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>
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import type { Prisma, PrismaClient } from '@generated/prisma/client';
|
||||
import type { Plan, PlanLimitsImpactDto, PlanLimitsImpactQuery, ProblemDetails, Result } from '@gruperly/shared';
|
||||
import { ok } from '@gruperly/shared';
|
||||
import prisma from '@/lib/prisma';
|
||||
import { getGraceDays, proSubscriptionWhere } from '@/modules/billing/lib';
|
||||
|
||||
type PlanLimitsImpactDeps = {
|
||||
db?: Pick<PrismaClient, 'group' | 'person' | 'user'>;
|
||||
graceDays?: number;
|
||||
now?: () => Date;
|
||||
};
|
||||
|
||||
/**
|
||||
* How many accounts on a plan would exceed new limits, i.e. become read only
|
||||
* as soon as they are saved (limits apply at once). Usage is the PlanGuard's:
|
||||
* groups owned, and people with at least one ACTIVE membership.
|
||||
*/
|
||||
export class PlanLimitsImpact {
|
||||
constructor(private readonly deps: PlanLimitsImpactDeps = {}) {}
|
||||
|
||||
async execute(plan: Plan, query: PlanLimitsImpactQuery): Promise<Result<PlanLimitsImpactDto, ProblemDetails>> {
|
||||
const db = this.deps.db ?? prisma;
|
||||
const now = this.deps.now?.() ?? new Date();
|
||||
const graceDays = this.deps.graceDays ?? getGraceDays();
|
||||
|
||||
const [overGroups, overAttendees] = await Promise.all([
|
||||
query.maxGroups === null
|
||||
? []
|
||||
: db.group.groupBy({
|
||||
by: ['createdById'],
|
||||
having: { id: { _count: { gt: query.maxGroups } } },
|
||||
}),
|
||||
query.maxAttendees === null
|
||||
? []
|
||||
: db.person.groupBy({
|
||||
by: ['createdById'],
|
||||
where: { attendees: { some: { status: 'ACTIVE' } } },
|
||||
having: { id: { _count: { gt: query.maxAttendees } } },
|
||||
}),
|
||||
]);
|
||||
|
||||
const candidates = [...new Set([...overGroups, ...overAttendees].map((row) => row.createdById))];
|
||||
if (candidates.length === 0) return ok({ affectedAccounts: 0 });
|
||||
|
||||
// Same split as the PlanGuard: anyone without a live Pro is held to Free.
|
||||
const isPro: Prisma.UserWhereInput = { subscription: { is: proSubscriptionWhere(now, graceDays) } };
|
||||
const affectedAccounts = await db.user.count({
|
||||
where: { id: { in: candidates }, ...(plan === 'PRO' ? isPro : { NOT: isPro }) },
|
||||
});
|
||||
return ok({ affectedAccounts });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user