fix(webhooks): emision idempotente de pago.vencido con vencidoNotificadoAt
This commit is contained in:
@@ -0,0 +1,207 @@
|
||||
import { describe, it } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import {
|
||||
emitirPagosVencidosIdempotente,
|
||||
type ClientePagoVencido,
|
||||
type PagoVencidoCandidato,
|
||||
} from "../src/lib/pago-vencido-core";
|
||||
|
||||
function crearStoreSimulado(pagosIniciales: PagoVencidoCandidato[]) {
|
||||
const filas = new Map(pagosIniciales.map((p) => [p.id, { ...p }]));
|
||||
const emitidos: Array<{ evento: string; payload: Record<string, unknown> }> = [];
|
||||
|
||||
const deps = {
|
||||
reclamarPagoVencido: async (pagoId: string, fechaSello: Date): Promise<boolean> => {
|
||||
const fila = filas.get(pagoId);
|
||||
if (!fila || fila.vencidoNotificadoAt !== null) return false;
|
||||
fila.vencidoNotificadoAt = fechaSello;
|
||||
return true;
|
||||
},
|
||||
emitir: async (evento: "pago.vencido", payload: Record<string, unknown>) => {
|
||||
emitidos.push({ evento, payload });
|
||||
},
|
||||
};
|
||||
|
||||
const listar = () => Array.from(filas.values()).map((p) => ({ ...p }));
|
||||
return { filas, emitidos, deps, listar };
|
||||
}
|
||||
|
||||
describe("Idempotencia de webhook pago.vencido", () => {
|
||||
const clienteSinEmail: ClientePagoVencido = {
|
||||
id: "cli_insta_001",
|
||||
nombre: "Jose Saul Escobedo Sierra",
|
||||
empresa: "Instituto Santiago Tapia",
|
||||
email: null,
|
||||
telefono: "+52 443 142 2031",
|
||||
};
|
||||
|
||||
const hoy = new Date("2026-11-25T00:00:00.000Z");
|
||||
|
||||
it("emite exactamente 1 evento en la corrida 1 y 0 en corridas 2 y 3 (incluso sin email/push)", async () => {
|
||||
const store = crearStoreSimulado([
|
||||
{
|
||||
id: "pago_01",
|
||||
programaId: "prog_01",
|
||||
concepto: "Pago semanal",
|
||||
fecha: new Date("2026-11-11T00:00:00.000Z"),
|
||||
monto: 3000,
|
||||
moneda: "MXN",
|
||||
estado: "pendiente",
|
||||
vencidoNotificadoAt: null,
|
||||
},
|
||||
{
|
||||
id: "pago_02",
|
||||
programaId: "prog_01",
|
||||
concepto: "Pago semanal",
|
||||
fecha: new Date("2026-11-18T00:00:00.000Z"),
|
||||
monto: 3000,
|
||||
moneda: "MXN",
|
||||
estado: "pendiente",
|
||||
vencidoNotificadoAt: null,
|
||||
},
|
||||
]);
|
||||
|
||||
// Corrida 1 (scheduler inicial)
|
||||
const r1 = await emitirPagosVencidosIdempotente(
|
||||
{
|
||||
clienteId: clienteSinEmail.id,
|
||||
cliente: clienteSinEmail,
|
||||
pagos: store.listar(),
|
||||
hoy,
|
||||
ahora: new Date("2026-11-25T16:00:00.000Z"),
|
||||
recordatorioEntregado: false,
|
||||
},
|
||||
store.deps
|
||||
);
|
||||
assert.equal(r1.emitido, true);
|
||||
assert.equal(r1.emitidos, 2);
|
||||
assert.deepEqual(r1.pagoIds, ["pago_01", "pago_02"]);
|
||||
assert.equal(store.emitidos.length, 1);
|
||||
|
||||
const payload = store.emitidos[0].payload;
|
||||
assert.equal(payload.clienteId, "cli_insta_001");
|
||||
assert.deepEqual(payload.cliente, {
|
||||
id: "cli_insta_001",
|
||||
nombre: "Jose Saul Escobedo Sierra",
|
||||
empresa: "Instituto Santiago Tapia",
|
||||
email: null,
|
||||
telefono: "+52 443 142 2031",
|
||||
});
|
||||
assert.deepEqual(payload.totales, { MXN: 6000 });
|
||||
|
||||
// Corrida 2 (60 minutos despues o reinicio del contenedor)
|
||||
const r2 = await emitirPagosVencidosIdempotente(
|
||||
{
|
||||
clienteId: clienteSinEmail.id,
|
||||
cliente: clienteSinEmail,
|
||||
pagos: store.listar(),
|
||||
hoy,
|
||||
ahora: new Date("2026-11-25T17:00:00.000Z"),
|
||||
recordatorioEntregado: false,
|
||||
},
|
||||
store.deps
|
||||
);
|
||||
assert.equal(r2.emitido, false);
|
||||
assert.equal(r2.emitidos, 0);
|
||||
assert.equal(store.emitidos.length, 1);
|
||||
|
||||
// Corrida 3 (recordatorio forzado manual)
|
||||
const r3 = await emitirPagosVencidosIdempotente(
|
||||
{
|
||||
clienteId: clienteSinEmail.id,
|
||||
cliente: clienteSinEmail,
|
||||
pagos: store.listar(),
|
||||
hoy,
|
||||
ahora: new Date("2026-11-25T18:00:00.000Z"),
|
||||
recordatorioEntregado: true,
|
||||
},
|
||||
store.deps
|
||||
);
|
||||
assert.equal(r3.emitido, false);
|
||||
assert.equal(r3.emitidos, 0);
|
||||
assert.equal(store.emitidos.length, 1);
|
||||
});
|
||||
|
||||
it("cuando vence un cobro nuevo adicional, emite solo el nuevo y no re-emite los ya sellados", async () => {
|
||||
const store = crearStoreSimulado([
|
||||
{
|
||||
id: "pago_viejo",
|
||||
programaId: "prog_01",
|
||||
concepto: "Pago semanal",
|
||||
fecha: new Date("2026-11-18T00:00:00.000Z"),
|
||||
monto: 3000,
|
||||
moneda: "MXN",
|
||||
estado: "pendiente",
|
||||
vencidoNotificadoAt: new Date("2026-11-20T15:00:00.000Z"),
|
||||
},
|
||||
{
|
||||
id: "pago_nuevo",
|
||||
programaId: "prog_01",
|
||||
concepto: "Pago semanal",
|
||||
fecha: new Date("2026-11-25T00:00:00.000Z"),
|
||||
monto: 3000,
|
||||
moneda: "MXN",
|
||||
estado: "pendiente",
|
||||
vencidoNotificadoAt: null,
|
||||
},
|
||||
]);
|
||||
|
||||
const r = await emitirPagosVencidosIdempotente(
|
||||
{
|
||||
clienteId: clienteSinEmail.id,
|
||||
cliente: clienteSinEmail,
|
||||
pagos: store.listar(),
|
||||
hoy,
|
||||
ahora: new Date("2026-11-25T16:00:00.000Z"),
|
||||
recordatorioEntregado: false,
|
||||
},
|
||||
store.deps
|
||||
);
|
||||
assert.equal(r.emitido, true);
|
||||
assert.deepEqual(r.pagoIds, ["pago_nuevo"]);
|
||||
assert.equal(store.emitidos.length, 1);
|
||||
assert.deepEqual(store.emitidos[0].payload.totales, { MXN: 3000 });
|
||||
});
|
||||
|
||||
it("resuelve carreras concurrentes reclamando atomicamente 1 sola emision", async () => {
|
||||
const store = crearStoreSimulado([
|
||||
{
|
||||
id: "pago_race",
|
||||
programaId: "prog_01",
|
||||
concepto: "Mensualidad",
|
||||
fecha: new Date("2026-11-24T00:00:00.000Z"),
|
||||
monto: 5000,
|
||||
moneda: "MXN",
|
||||
estado: "pendiente",
|
||||
vencidoNotificadoAt: null,
|
||||
},
|
||||
]);
|
||||
|
||||
const snapshot = store.listar();
|
||||
const [a, b] = await Promise.all([
|
||||
emitirPagosVencidosIdempotente(
|
||||
{
|
||||
clienteId: clienteSinEmail.id,
|
||||
cliente: clienteSinEmail,
|
||||
pagos: snapshot,
|
||||
hoy,
|
||||
recordatorioEntregado: false,
|
||||
},
|
||||
store.deps
|
||||
),
|
||||
emitirPagosVencidosIdempotente(
|
||||
{
|
||||
clienteId: clienteSinEmail.id,
|
||||
cliente: clienteSinEmail,
|
||||
pagos: snapshot,
|
||||
hoy,
|
||||
recordatorioEntregado: false,
|
||||
},
|
||||
store.deps
|
||||
),
|
||||
]);
|
||||
|
||||
assert.equal(Number(a.emitido) + Number(b.emitido), 1);
|
||||
assert.equal(store.emitidos.length, 1);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user