- 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>
72 lines
2.3 KiB
TypeScript
72 lines
2.3 KiB
TypeScript
import type { PrismaClient } from '@generated/prisma/client';
|
|
import type {
|
|
GroupAutomationsDto,
|
|
ProblemDetails,
|
|
Result,
|
|
UpdateGroupAutomations,
|
|
} from '@gruperly/shared';
|
|
import { err, ok } from '@gruperly/shared';
|
|
import { forbiddenProblem, notFoundResourceProblem } from '@/http/problem-builders';
|
|
import prisma from '@/lib/prisma';
|
|
import { PlanGuard } from '@/modules/billing/lib/entitlements';
|
|
|
|
type UpdateGroupAutomationsDeps = {
|
|
db?: Pick<PrismaClient, 'group'>;
|
|
planGuard?: Pick<PlanGuard, 'requireFeature'>;
|
|
};
|
|
|
|
/**
|
|
* Turns the group's weekly WhatsApp automations on or off. Only the owner can:
|
|
* the messages go out from their own WhatsApp.
|
|
*
|
|
* Turning one on does not require a linked WhatsApp. The weekly job skips
|
|
* groups whose owner has nothing usable, and the UI explains why.
|
|
*/
|
|
export class UpdateGroupAutomationsUseCase {
|
|
constructor(private readonly deps: UpdateGroupAutomationsDeps = {}) {}
|
|
|
|
async execute(
|
|
groupId: string,
|
|
userId: string,
|
|
input: UpdateGroupAutomations,
|
|
): Promise<Result<GroupAutomationsDto, ProblemDetails>> {
|
|
const db = this.deps.db ?? prisma;
|
|
|
|
const group = await db.group.findUnique({
|
|
where: { id: groupId },
|
|
select: { id: true, createdById: true },
|
|
});
|
|
if (!group) return err(notFoundResourceProblem('Group', groupId));
|
|
if (group.createdById !== userId) {
|
|
return err(
|
|
forbiddenProblem({
|
|
detail: 'Solo quien creó el grupo puede cambiar sus automatizaciones.',
|
|
code: 'group_owner_required',
|
|
}),
|
|
);
|
|
}
|
|
|
|
// Turning them off always works, so a Free professor can clean up.
|
|
if (input.autoPaymentReminders === true || input.autoReengagement === true) {
|
|
const allowed = await (this.deps.planGuard ?? new PlanGuard()).requireFeature(
|
|
userId,
|
|
'whatsappAutomations',
|
|
);
|
|
if (!allowed.ok) return allowed;
|
|
}
|
|
|
|
const updated = await db.group.update({
|
|
where: { id: groupId },
|
|
data: {
|
|
...(input.autoPaymentReminders !== undefined
|
|
? { autoPaymentReminders: input.autoPaymentReminders }
|
|
: {}),
|
|
...(input.autoReengagement !== undefined ? { autoReengagement: input.autoReengagement } : {}),
|
|
},
|
|
select: { autoPaymentReminders: true, autoReengagement: true },
|
|
});
|
|
|
|
return ok(updated);
|
|
}
|
|
}
|