Add cálculo de trial y badge de plan
This commit is contained in:
19
mvp/b2c/src/lib/billing/plan.ts
Normal file
19
mvp/b2c/src/lib/billing/plan.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
export function trialDaysRemaining(trialEndsAt: Date | null, now: Date = new Date()): number {
|
||||||
|
if (!trialEndsAt) return 0;
|
||||||
|
const ms = trialEndsAt.getTime() - now.getTime();
|
||||||
|
if (ms <= 0) return 0;
|
||||||
|
return Math.ceil(ms / (24 * 60 * 60 * 1000));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function formatPlanBadge(
|
||||||
|
planNombre: string | null,
|
||||||
|
status: string,
|
||||||
|
trialEndsAt: Date | null,
|
||||||
|
now: Date = new Date()
|
||||||
|
): string {
|
||||||
|
const plan = planNombre ? `Plan ${planNombre}` : 'Sin plan';
|
||||||
|
if (status === 'trial') {
|
||||||
|
return `${plan} · trial, ${trialDaysRemaining(trialEndsAt, now)} días restantes`;
|
||||||
|
}
|
||||||
|
return `${plan} · ${status}`;
|
||||||
|
}
|
||||||
21
mvp/b2c/tests/billing/plan.test.ts
Normal file
21
mvp/b2c/tests/billing/plan.test.ts
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import { describe, it, expect } from 'vitest';
|
||||||
|
import { trialDaysRemaining, formatPlanBadge } from '@/lib/billing/plan';
|
||||||
|
|
||||||
|
describe('plan', () => {
|
||||||
|
it('calcula días de trial restantes (redondeo hacia arriba, nunca negativo)', () => {
|
||||||
|
const now = new Date('2026-05-30T12:00:00Z');
|
||||||
|
expect(trialDaysRemaining(new Date('2026-06-05T12:00:00Z'), now)).toBe(6);
|
||||||
|
expect(trialDaysRemaining(new Date('2026-05-30T18:00:00Z'), now)).toBe(1);
|
||||||
|
expect(trialDaysRemaining(new Date('2026-05-29T12:00:00Z'), now)).toBe(0);
|
||||||
|
expect(trialDaysRemaining(null, now)).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('formatea el badge según estado', () => {
|
||||||
|
const now = new Date('2026-05-30T12:00:00Z');
|
||||||
|
expect(formatPlanBadge('Pro', 'trial', new Date('2026-06-05T12:00:00Z'), now))
|
||||||
|
.toBe('Plan Pro · trial, 6 días restantes');
|
||||||
|
expect(formatPlanBadge('Pro', 'activo', null, now)).toBe('Plan Pro · activo');
|
||||||
|
expect(formatPlanBadge(null, 'trial', new Date('2026-06-05T12:00:00Z'), now))
|
||||||
|
.toBe('Sin plan · trial, 6 días restantes');
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user