101 lines
2.9 KiB
TypeScript
101 lines
2.9 KiB
TypeScript
/**
|
|
* Configura la suscripción del tenant Horux 360 (HTS240708LJA) como Plan Custom:
|
|
* - amount: $10
|
|
* - currentPeriodEnd: hoy + 300 días
|
|
* - status: authorized
|
|
*
|
|
* Idempotente — actualiza la suscripción existente o crea una nueva si no hay.
|
|
* Resetea `lastReminderDay`/`lastReminderSentAt` para que el cron de avisos
|
|
* arranque limpio respecto al nuevo período.
|
|
*/
|
|
import { prisma } from '../src/config/database.js';
|
|
|
|
async function main() {
|
|
const RFC = 'HTS240708LJA';
|
|
const AMOUNT = 10;
|
|
const DAYS_AHEAD = 7;
|
|
|
|
const tenant = await prisma.tenant.findUnique({ where: { rfc: RFC } });
|
|
if (!tenant) {
|
|
console.error(`Tenant ${RFC} no encontrado.`);
|
|
process.exit(1);
|
|
}
|
|
|
|
const now = new Date();
|
|
const periodEnd = new Date(now.getTime() + DAYS_AHEAD * 24 * 60 * 60 * 1000);
|
|
|
|
const existing = await prisma.subscription.findFirst({
|
|
where: { tenantId: tenant.id },
|
|
orderBy: { createdAt: 'desc' },
|
|
});
|
|
|
|
console.log('Tenant:', { id: tenant.id, nombre: tenant.nombre, plan: tenant.plan });
|
|
console.log('Subscription previa:', existing ? {
|
|
id: existing.id,
|
|
plan: existing.plan,
|
|
status: existing.status,
|
|
amount: existing.amount.toString(),
|
|
currentPeriodEnd: existing.currentPeriodEnd,
|
|
} : null);
|
|
|
|
let sub;
|
|
if (existing) {
|
|
sub = await prisma.subscription.update({
|
|
where: { id: existing.id },
|
|
data: {
|
|
plan: 'custom',
|
|
amount: AMOUNT,
|
|
status: 'authorized',
|
|
currentPeriodStart: now,
|
|
currentPeriodEnd: periodEnd,
|
|
// Limpiar pending/upgrade residuales del estado anterior.
|
|
pendingPlan: null,
|
|
pendingFrequency: null,
|
|
pendingEffectiveAt: null,
|
|
upgradePreferenceId: null,
|
|
upgradeTargetPlan: null,
|
|
upgradeTargetAmount: null,
|
|
// Reset del tracker de avisos — período nuevo, ningún bucket notificado.
|
|
lastReminderDay: null,
|
|
lastReminderSentAt: null,
|
|
},
|
|
});
|
|
} else {
|
|
sub = await prisma.subscription.create({
|
|
data: {
|
|
tenantId: tenant.id,
|
|
plan: 'custom',
|
|
amount: AMOUNT,
|
|
status: 'authorized',
|
|
frequency: 'monthly',
|
|
currentPeriodStart: now,
|
|
currentPeriodEnd: periodEnd,
|
|
},
|
|
});
|
|
}
|
|
|
|
// El tenant también tiene un campo `plan` propio — alinearlo con la sub.
|
|
if (tenant.plan !== 'custom') {
|
|
await prisma.tenant.update({ where: { id: tenant.id }, data: { plan: 'custom' } });
|
|
console.log(`Tenant.plan actualizado: ${tenant.plan} → custom`);
|
|
}
|
|
|
|
console.log('Subscription final:', {
|
|
id: sub.id,
|
|
plan: sub.plan,
|
|
status: sub.status,
|
|
amount: sub.amount.toString(),
|
|
currentPeriodStart: sub.currentPeriodStart,
|
|
currentPeriodEnd: sub.currentPeriodEnd,
|
|
});
|
|
|
|
console.log(`\n✓ Plan Custom activo. Próximo cobro: ${periodEnd.toLocaleDateString('es-MX', { dateStyle: 'long' })} ($${AMOUNT})`);
|
|
|
|
await prisma.$disconnect();
|
|
}
|
|
|
|
main().catch(err => {
|
|
console.error(err);
|
|
process.exit(1);
|
|
});
|