feat(notificaciones): sweep one aseguradora at a time, and stop the robot quoting a premium
The office works GMX and ANA as separate batches, so the manual barrido now takes a "Compañía" selection. It filters the pending list as well as the sweep, so what is on screen is exactly what "Ejecutar barrido" will mail, and the confirmation names the carrier — running GMX when ANA was meant is the mistake the filter exists to prevent, and it is not reversible once the mail is out. The carrier is chosen by InsuranceProvider id, from the same /lookups the policy form reads, so a renamed or newly added aseguradora needs no code change here. A carrier-scoped run deliberately does NOT advance `lastSuccessfulAt`. The sweep's catch-up window is computed from it, so advancing after a run that looked at every day but mailed only one carrier would push every OTHER carrier's letters out of tomorrow's window and they would never be sent. Same reasoning that already keeps a debug run from advancing it. Separately, the letter's "Prima" row is now dropped from the unattended scheduled sweep only. A premium can still be re-rated at renewal, and an amount a robot mailed out is one the office has to walk back; every send a person triggers — the manual barrido and the per-row "Enviar aviso" — still quotes it. `recordLog` renders with the same flag, so `bodySnapshot` cannot show the office a letter the customer never received. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -46,6 +46,20 @@ describe("renderRenewalEmail", () => {
|
||||
expect(result.html).toContain("Calle Uno 123");
|
||||
});
|
||||
|
||||
it("omits the premium when the sender did not ask for it", () => {
|
||||
// The unattended sweep quotes no amount: the premium can still be
|
||||
// re-rated at renewal, and a number a robot mailed out is one the office
|
||||
// has to walk back.
|
||||
const result = renderRenewalEmail(letter(), { includePremium: false });
|
||||
|
||||
expect(result.html).not.toContain("Prima");
|
||||
expect(result.html).not.toContain("1,392.00");
|
||||
// Everything else the customer needs is still there.
|
||||
expect(result.html).toContain("POL-123");
|
||||
expect(result.html).toContain("01/09/2026");
|
||||
expect(result.html).toContain("Ana Pérez");
|
||||
});
|
||||
|
||||
it("uses overdue wording for generation three", () => {
|
||||
const result = renderRenewalEmail(letter({ generation: 3 }));
|
||||
|
||||
|
||||
@@ -34,10 +34,23 @@ function row(label: string, value: string): string {
|
||||
return `<tr><th style="padding:8px 12px;text-align:left;background:#f4f4f4;border:1px solid #ddd">${escapeHtml(label)}</th><td style="padding:8px 12px;border:1px solid #ddd">${escapeHtml(value)}</td></tr>`;
|
||||
}
|
||||
|
||||
export function renderRenewalEmail(letter: RenewalLetterRow): {
|
||||
/**
|
||||
* Render one renewal letter.
|
||||
*
|
||||
* `includePremium` decides whether the "Prima" row appears. The unattended
|
||||
* sweep sends without it — an amount quoted by a robot, on a premium that may
|
||||
* still be re-rated at renewal, is a number the office has to walk back — and
|
||||
* every staff-triggered send (the manual barrido and the per-row "Enviar
|
||||
* aviso") keeps it, because a person chose to quote it.
|
||||
*/
|
||||
export function renderRenewalEmail(
|
||||
letter: RenewalLetterRow,
|
||||
options: { includePremium?: boolean } = {},
|
||||
): {
|
||||
subject: string;
|
||||
html: string;
|
||||
} {
|
||||
const includePremium = options.includePremium !== false;
|
||||
const expired = letter.generation === 3;
|
||||
const subject = expired
|
||||
? `Póliza vencida: ${letter.policyNumber}`
|
||||
@@ -51,7 +64,7 @@ export function renderRenewalEmail(letter: RenewalLetterRow): {
|
||||
row("Tipo de póliza", letter.policyType),
|
||||
row("Aseguradora", letter.provider),
|
||||
row("Fecha de vencimiento", displayDate(letter.policyTo)),
|
||||
row("Prima", money(premium, letter.currency)),
|
||||
...(includePremium ? [row("Prima", money(premium, letter.currency))] : []),
|
||||
row("Cliente", letter.customerName),
|
||||
row("Correo", letter.customerEmail ?? "No disponible"),
|
||||
row("Teléfono", phone),
|
||||
|
||||
@@ -183,6 +183,50 @@ describe("renewal notices write the shared notification log", () => {
|
||||
expect(prisma.renewalNotice.upsert).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("quotes the premium on a staff-triggered sweep but not the scheduled one", async () => {
|
||||
const manual = build({});
|
||||
await manual.service.sweep("user-1");
|
||||
expect(manual.record.mock.calls[0][0].bodySnapshot).toContain("Prima");
|
||||
|
||||
const automatic = build({});
|
||||
await automatic.service.scheduledSweep();
|
||||
const body = automatic.record.mock.calls[0][0].bodySnapshot;
|
||||
// The snapshot has to match the mail that actually went out, or the
|
||||
// office reads a letter the customer never received.
|
||||
expect(body).not.toContain("Prima");
|
||||
expect(body).toContain("700442181");
|
||||
});
|
||||
|
||||
it("scopes a sweep to one aseguradora without advancing the catch-up window", async () => {
|
||||
const { service, prisma, send } = build({});
|
||||
|
||||
const result = await service.sweep("user-1", { providerId: "gmx-id" });
|
||||
|
||||
expect(result.sent).toBe(1);
|
||||
expect(result.providerId).toBe("gmx-id");
|
||||
expect(prisma.policy.findMany.mock.calls[0][0].where).toMatchObject({
|
||||
insuranceProviderId: "gmx-id",
|
||||
});
|
||||
expect(send).toHaveBeenCalledTimes(1);
|
||||
// Only one carrier was mailed, so the days this run covered are still owed
|
||||
// to every other carrier: advancing `lastSuccessfulAt` would move them out
|
||||
// of tomorrow's window and they would never be sent.
|
||||
const release = prisma.scheduledJobState.update.mock.calls.at(-1)?.[0];
|
||||
expect(release.data.lastSuccessfulAt).toBeUndefined();
|
||||
});
|
||||
|
||||
it("advances the catch-up window on a clean unfiltered sweep", async () => {
|
||||
const { service, prisma } = build({});
|
||||
|
||||
await service.sweep("user-1");
|
||||
|
||||
expect(prisma.policy.findMany.mock.calls[0][0].where).not.toHaveProperty(
|
||||
"insuranceProviderId",
|
||||
);
|
||||
const release = prisma.scheduledJobState.update.mock.calls.at(-1)?.[0];
|
||||
expect(release.data.lastSuccessfulAt).toBeInstanceOf(Date);
|
||||
});
|
||||
|
||||
it("does not fail a delivered notice when the log write throws", async () => {
|
||||
const { service, record } = build({});
|
||||
record.mockRejectedValue(new Error("log table gone"));
|
||||
|
||||
@@ -25,6 +25,13 @@ class RenewalFlagsDto {
|
||||
debug?: boolean;
|
||||
}
|
||||
|
||||
class SweepRenewalsDto extends RenewalFlagsDto {
|
||||
/** Sweep one aseguradora only (GMX, ANA, …). Omitted = todas. */
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
providerId?: string;
|
||||
}
|
||||
|
||||
class SendRenewalDto extends RenewalFlagsDto {
|
||||
@IsString()
|
||||
policyId!: string;
|
||||
@@ -43,17 +50,22 @@ export class RenewalsController {
|
||||
constructor(private readonly renewals: RenewalsService) {}
|
||||
|
||||
@Get("pending")
|
||||
pending(@Query("days") days?: string) {
|
||||
pending(
|
||||
@Query("days") days?: string,
|
||||
@Query("providerId") providerId?: string,
|
||||
) {
|
||||
return this.renewals.pending(
|
||||
Math.min(365, Math.max(1, Number(days) || 30)),
|
||||
providerId?.trim() || undefined,
|
||||
);
|
||||
}
|
||||
|
||||
@Post("sweep")
|
||||
@RequireAbility("renewal:send")
|
||||
sweep(@Body() dto: RenewalFlagsDto, @Req() req: Request) {
|
||||
sweep(@Body() dto: SweepRenewalsDto, @Req() req: Request) {
|
||||
return this.renewals.sweep((req.user as { id: string }).id, {
|
||||
debug: dto?.debug,
|
||||
providerId: dto?.providerId,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -84,10 +84,14 @@ export class RenewalsService implements OnModuleInit {
|
||||
|
||||
/** The unattended run always sends for real: `debug` is a per-click switch
|
||||
* in the UI, never persisted, so the schedule cannot inherit a forgotten
|
||||
* test toggle and silently stop mailing customers. */
|
||||
* test toggle and silently stop mailing customers.
|
||||
*
|
||||
* `automatic` is what drops the premium from the letter — see
|
||||
* `renderRenewalEmail`. It is set here and nowhere else, so every sweep a
|
||||
* person clicks still quotes the amount. */
|
||||
async scheduledSweep(): Promise<void> {
|
||||
try {
|
||||
await this.sweep();
|
||||
await this.sweep(undefined, { automatic: true });
|
||||
} catch (error) {
|
||||
this.logger.error(
|
||||
`Falló el barrido de renovaciones: ${(error as Error).message}`,
|
||||
@@ -95,7 +99,10 @@ export class RenewalsService implements OnModuleInit {
|
||||
}
|
||||
}
|
||||
|
||||
async pending(days = 30) {
|
||||
/** @param providerId Restrict to one aseguradora. The list has to agree
|
||||
* with what a sweep would send, or the carrier-scoped barrido shows rows it
|
||||
* will not mail. */
|
||||
async pending(days = 30, providerId?: string) {
|
||||
const today = dateInTimeZone(new Date());
|
||||
const state = await this.prisma.scheduledJobState.findUnique({
|
||||
where: { name: JOB_NAME },
|
||||
@@ -111,6 +118,7 @@ export class RenewalsService implements OnModuleInit {
|
||||
item,
|
||||
today,
|
||||
state?.lastSuccessfulAt ?? null,
|
||||
providerId,
|
||||
),
|
||||
})),
|
||||
);
|
||||
@@ -122,8 +130,20 @@ export class RenewalsService implements OnModuleInit {
|
||||
);
|
||||
}
|
||||
|
||||
async sweep(userId?: string, flags: { debug?: boolean } = {}) {
|
||||
/**
|
||||
* @param flags.providerId Sweep only one aseguradora. GMX and ANA are worked
|
||||
* as separate batches by the office, so mixing them in one run is what this
|
||||
* exists to prevent.
|
||||
* @param flags.automatic Set only by the scheduler. Drops the premium from
|
||||
* the letter.
|
||||
*/
|
||||
async sweep(
|
||||
userId?: string,
|
||||
flags: { debug?: boolean; providerId?: string; automatic?: boolean } = {},
|
||||
) {
|
||||
const debug = !!flags.debug;
|
||||
const providerId = flags.providerId?.trim() || undefined;
|
||||
const includePremium = !flags.automatic;
|
||||
const now = new Date();
|
||||
const state = await this.acquireLock(now);
|
||||
|
||||
@@ -145,6 +165,7 @@ export class RenewalsService implements OnModuleInit {
|
||||
cadence,
|
||||
today,
|
||||
state.lastSuccessfulAt,
|
||||
providerId,
|
||||
);
|
||||
eligible += policies.length;
|
||||
|
||||
@@ -157,13 +178,17 @@ export class RenewalsService implements OnModuleInit {
|
||||
await this.recordLog(policy, cadence.generation, "", {
|
||||
status: "SKIPPED_NO_EMAIL",
|
||||
debug,
|
||||
includePremium,
|
||||
});
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
await this.deliver(policy, cadence.generation, to, userId, debug);
|
||||
await this.deliver(policy, cadence.generation, to, userId, {
|
||||
debug,
|
||||
includePremium,
|
||||
});
|
||||
sent++;
|
||||
} catch (error) {
|
||||
failures.push({
|
||||
@@ -182,11 +207,18 @@ export class RenewalsService implements OnModuleInit {
|
||||
failed: failures.length,
|
||||
failures,
|
||||
debug,
|
||||
providerId: providerId ?? null,
|
||||
};
|
||||
// A debug run must not advance `lastSuccessfulAt`: it wrote no
|
||||
// RenewalNotice rows, so the days it "covered" are still owed, and
|
||||
// narrowing tomorrow's window back to a single day would drop them.
|
||||
await this.releaseLock(!debug && failures.length === 0 ? now : null);
|
||||
//
|
||||
// A carrier-scoped run must not advance it either, for the same reason
|
||||
// one step out: it looked at the whole window but only mailed one
|
||||
// aseguradora, so every other carrier's letters in those days would fall
|
||||
// outside tomorrow's window and never be sent at all.
|
||||
const complete = !debug && !providerId && failures.length === 0;
|
||||
await this.releaseLock(complete ? now : null);
|
||||
void this.audit.log(userId, "renewalNotice.sweep", result);
|
||||
return result;
|
||||
} catch (error) {
|
||||
@@ -233,12 +265,14 @@ export class RenewalsService implements OnModuleInit {
|
||||
throw new BadRequestException("El cliente no tiene correo registrado.");
|
||||
}
|
||||
|
||||
// A person clicked this, so the premium stays in the letter — only the
|
||||
// scheduler's unattended run omits it.
|
||||
const { sentAt, providerMessageId, addressedTo } = await this.deliver(
|
||||
policy,
|
||||
generation,
|
||||
to,
|
||||
userId,
|
||||
debug,
|
||||
{ debug, includePremium: true },
|
||||
);
|
||||
return {
|
||||
policyId,
|
||||
@@ -270,10 +304,12 @@ export class RenewalsService implements OnModuleInit {
|
||||
generation: number,
|
||||
to: string,
|
||||
userId?: string,
|
||||
debug = false,
|
||||
options: { debug?: boolean; includePremium?: boolean } = {},
|
||||
) {
|
||||
const debug = !!options.debug;
|
||||
const includePremium = options.includePremium !== false;
|
||||
const letter = toRenewalLetterRow(policy, generation);
|
||||
const message = renderRenewalEmail(letter);
|
||||
const message = renderRenewalEmail(letter, { includePremium });
|
||||
const addressedTo = debug ? DEBUG_RECIPIENT : to;
|
||||
|
||||
let result: Awaited<ReturnType<MailService["send"]>>;
|
||||
@@ -291,6 +327,7 @@ export class RenewalsService implements OnModuleInit {
|
||||
status: "FAILED",
|
||||
error: detail,
|
||||
debug,
|
||||
includePremium,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
@@ -324,6 +361,7 @@ export class RenewalsService implements OnModuleInit {
|
||||
providerResponse: result.response || undefined,
|
||||
sendDate: sentAt,
|
||||
debug,
|
||||
includePremium,
|
||||
});
|
||||
void this.audit.log(userId, "renewalNotice.send", {
|
||||
policyId: policy.id,
|
||||
@@ -355,10 +393,15 @@ export class RenewalsService implements OnModuleInit {
|
||||
error?: string;
|
||||
sendDate?: Date;
|
||||
debug?: boolean;
|
||||
/** Must match what `deliver` rendered, or `bodySnapshot` shows the
|
||||
* office a letter the customer never received. */
|
||||
includePremium?: boolean;
|
||||
},
|
||||
): Promise<void> {
|
||||
const letter = toRenewalLetterRow(policy, generation);
|
||||
const message = renderRenewalEmail(letter);
|
||||
const message = renderRenewalEmail(letter, {
|
||||
includePremium: outcome.includePremium,
|
||||
});
|
||||
try {
|
||||
await this.notificationLog.record({
|
||||
notificationType: "RENEWAL_NOTICE",
|
||||
@@ -391,11 +434,13 @@ export class RenewalsService implements OnModuleInit {
|
||||
cadence: (typeof RENEWAL_CADENCE)[number],
|
||||
today: Date,
|
||||
lastSuccessfulAt: Date | null,
|
||||
providerId?: string,
|
||||
) {
|
||||
const window = renewalWindow(today, cadence.offsetDays, lastSuccessfulAt);
|
||||
return this.prisma.policy.findMany({
|
||||
where: {
|
||||
archivedAt: null,
|
||||
...(providerId && { insuranceProviderId: providerId }),
|
||||
policyTo: { gte: window.from, lte: window.to },
|
||||
customer: {
|
||||
archivedAt: null,
|
||||
|
||||
Reference in New Issue
Block a user