- 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>
67 lines
1.8 KiB
TypeScript
67 lines
1.8 KiB
TypeScript
import type { Prisma } from '@generated/prisma/client';
|
|
import { Role } from '@generated/prisma/client';
|
|
import type {
|
|
CreateFirstGroup as CreateGroupInput,
|
|
CreateGroupResult,
|
|
ProblemDetails,
|
|
Result,
|
|
} from '@gruperly/shared';
|
|
import { ok } from '@gruperly/shared';
|
|
import { default as prisma, UnitOfWork } from '@/lib/prisma';
|
|
import { PlanGuard } from '@/modules/billing/lib/entitlements';
|
|
import { type GroupRecord, toGroupDto } from '../../lib';
|
|
|
|
type CreateGroupDeps = {
|
|
unitOfWork?: UnitOfWork;
|
|
planGuard?: Pick<PlanGuard, 'canCreateGroup'>;
|
|
};
|
|
|
|
export class CreateGroup {
|
|
constructor(private readonly deps: CreateGroupDeps = {}) {}
|
|
|
|
async execute(
|
|
data: CreateGroupInput,
|
|
userId: string,
|
|
): Promise<Result<CreateGroupResult, ProblemDetails>> {
|
|
const unitOfWork = this.deps.unitOfWork ?? new UnitOfWork(prisma);
|
|
|
|
const allowed = await (this.deps.planGuard ?? new PlanGuard()).canCreateGroup(userId);
|
|
if (!allowed.ok) {
|
|
return allowed;
|
|
}
|
|
|
|
const transaction = unitOfWork.executeResult(
|
|
async (tx: Prisma.TransactionClient) => {
|
|
const created = await tx.group.create({
|
|
data: {
|
|
name: data.name,
|
|
createdById: userId,
|
|
days: data.days,
|
|
time: data.time,
|
|
capacity: data.capacity,
|
|
price: data.price,
|
|
billingType: data.billingType,
|
|
dueDay: data.dueDay,
|
|
},
|
|
});
|
|
|
|
await tx.groupMember.create({
|
|
data: {
|
|
groupId: created.id,
|
|
userId,
|
|
role: Role.OWNER,
|
|
},
|
|
});
|
|
|
|
return ok(created);
|
|
},
|
|
);
|
|
|
|
const result = await transaction;
|
|
if (!result.ok) {
|
|
return result;
|
|
}
|
|
|
|
return ok({ group: toGroupDto(result.value as GroupRecord) });
|
|
}
|
|
} |