Files
gruperly/apps/backend/src/modules/group-preregistrations/features/approve/use-case.ts
Jose Selesan f5e77c99f6 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>
2026-10-23 15:49:27 -03:00

125 lines
3.9 KiB
TypeScript

import type { PrismaClient } from '@generated/prisma/client';
import type {
ApproveGroupPreRegistrationResult,
ProblemDetails,
Result,
} from '@gruperly/shared';
import { err, ok } from '@gruperly/shared';
import { conflictProblem, notFoundResourceProblem } from '@/http/problem-builders';
import { default as prisma, UnitOfWork } from '@/lib/prisma';
import { PlanGuard } from '@/modules/billing/lib/entitlements';
import {
findGroupOwnedByUser,
type GroupWaitlistEntryRecord,
toGroupWaitlistEntryDto,
} from '../../../group-waitlist/lib/helpers';
import { promoteWaitlistEntryRecord } from '../../../group-waitlist/lib/promote';
import { type GroupPreRegistrationRecord } from '../../lib/helpers';
type ApproveGroupPreRegistrationDeps = {
db?: Pick<PrismaClient, 'group' | 'groupPreRegistration'>;
unitOfWork?: UnitOfWork;
planGuard?: Pick<PlanGuard, 'canAddAttendees'>;
};
export class ApproveGroupPreRegistration {
constructor(private readonly deps: ApproveGroupPreRegistrationDeps = {}) {}
async execute(
groupId: string,
entryId: string,
userId: string,
): Promise<Result<ApproveGroupPreRegistrationResult, ProblemDetails>> {
const db = this.deps.db ?? prisma;
const unitOfWork = this.deps.unitOfWork ?? new UnitOfWork(prisma);
const groupResult = await findGroupOwnedByUser(db, groupId, userId);
if (!groupResult.ok) {
return groupResult;
}
const entry = await db.groupPreRegistration.findFirst({
where: { id: entryId, groupId },
});
if (!entry) {
return err(notFoundResourceProblem('GroupPreRegistration', entryId));
}
if (entry.status !== 'PENDING') {
return err(
conflictProblem({
detail: 'Esta pre-inscripción ya fue revisada.',
code: 'pre_registration_already_reviewed',
}),
);
}
const allowed = await (this.deps.planGuard ?? new PlanGuard()).canAddAttendees(userId);
if (!allowed.ok) {
return allowed;
}
const record = entry as unknown as GroupPreRegistrationRecord;
return unitOfWork.executeResult(
async (tx): Promise<Result<ApproveGroupPreRegistrationResult, ProblemDetails>> => {
const promoteResult = await promoteWaitlistEntryRecord(
tx,
groupResult.value,
{
id: record.id,
groupId: record.groupId,
fullName: record.fullName,
phone: record.phone,
email: record.email,
notes: null,
status: 'PENDING',
createdAt: record.createdAt,
updatedAt: record.updatedAt,
},
);
if (promoteResult.ok) {
await tx.groupPreRegistration.update({
where: { id: record.id },
data: { status: 'APPROVED', reviewedAt: new Date() },
});
return ok({
outcome: 'added',
attendee: promoteResult.value,
waitlistEntry: null,
});
}
if (promoteResult.error.code !== 'group_capacity_reached') {
return promoteResult;
}
const existingWaitlistEntry = await tx.groupWaitlistEntry.findFirst({
where: { groupId: record.groupId, phone: record.phone },
});
const waitlistEntryRecord = existingWaitlistEntry
? (existingWaitlistEntry as unknown as GroupWaitlistEntryRecord)
: ((await tx.groupWaitlistEntry.create({
data: {
groupId: record.groupId,
fullName: record.fullName,
phone: record.phone,
email: record.email,
},
})) as unknown as GroupWaitlistEntryRecord);
await tx.groupPreRegistration.update({
where: { id: record.id },
data: { status: 'APPROVED', reviewedAt: new Date() },
});
return ok({
outcome: 'waitlisted',
attendee: null,
waitlistEntry: toGroupWaitlistEntryDto(waitlistEntryRecord),
});
},
);
}
}