feat(renovaciones): send renewal notices from the list, drop manual marking
Build and Push Images / Build jorgecuadros-web (push) Successful in 1m45s
Build and Push Images / Build jorgecuadros-api (push) Successful in 2m24s

The Pólizas tab now sends. Each pending row gets an "Enviar aviso" button
backed by POST /renewals/send, which renders, mails and records the notice
through the same path the daily sweep uses — so a hand-sent letter is
marked exactly like a swept one and drops off the pending list.

Sending is now the only way a notice gets marked as sent. Remove the
manual "Marcar impreso" / "Marcar EMAIL" buttons and the endpoint behind
them (POST /policies/:id/renewal-notices, PoliciesService.markRenewalNotice,
MarkRenewalNoticeDto): they wrote a sentAt with no mail behind it, which
let the list claim a customer was notified when nothing was sent.

sendOne refuses a generation that already has a sentAt (409) so a double
click cannot mail the customer twice, and 400s when the customer has no
email on file. Sweep and single send share the new deliver() helper.
This commit is contained in:
2026-08-02 02:40:38 -07:00
parent 53a5fe8076
commit c0cc0d2ac2
6 changed files with 155 additions and 141 deletions
@@ -7,8 +7,10 @@ import { apiFetch } from "@/lib/api";
/**
* Renewal notices — the "Pólizas" half of /notificaciones. Shows which
* renewal letters are pending in a window and lets staff run the sweep by
* hand or mark a notice as delivered. Gated on `renewal:send`.
* renewal letters are pending in a window and lets staff send them, either
* one row at a time or as a whole sweep. Sending is what marks a notice as
* delivered — there is no manual "mark as sent", so the list can never claim
* a letter went out when no mail was ever sent. Gated on `renewal:send`.
*/
export interface RenewalLetter {
@@ -34,11 +36,12 @@ export interface RenewalSweepResult {
failures: { policyId: string; generation: number; error: string }[];
}
export interface RenewalMarkInput {
export interface RenewalSendResult {
policyId: string;
generation: number;
channel: "MAIL" | "EMAIL";
sentAt?: string;
notes?: string;
to: string;
sentAt: string;
providerMessageId?: string;
}
const GENERATION_LABEL: Record<number, string> = {
@@ -47,11 +50,6 @@ const GENERATION_LABEL: Record<number, string> = {
3: "Tercer aviso (7 días después)",
};
const CHANNEL_LABEL: Record<"MAIL" | "EMAIL", string> = {
MAIL: "Impreso",
EMAIL: "Correo electrónico",
};
export function NotificacionesPolizas() {
const allowed = useCan("renewal:send");
const [days, setDays] = useState(30);
@@ -60,6 +58,8 @@ export function NotificacionesPolizas() {
const [actionError, setActionError] = useState<string | null>(null);
const [notice, setNotice] = useState<string | null>(null);
const [sweeping, setSweeping] = useState(false);
/** `policyId-generation` of the row currently being sent, if any. */
const [sendingKey, setSendingKey] = useState<string | null>(null);
const refresh = useCallback(async () => {
setPendingError(null);
@@ -97,21 +97,29 @@ export function NotificacionesPolizas() {
}
}
async function handleMark(letter: RenewalLetter, channel: "MAIL" | "EMAIL") {
/**
* Send this one notice now. The API records it as sent on success, so the
* row leaves the pending list — that disappearance IS the "sent" signal,
* backed by the confirmation line above the table.
*/
async function handleSend(letter: RenewalLetter) {
setActionError(null);
setNotice(null);
setSendingKey(`${letter.policyId}-${letter.generation}`);
try {
await apiFetch(`/policies/${letter.policyId}/renewal-notices`, {
const result = await apiFetch<RenewalSendResult>("/renewals/send", {
method: "POST",
body: JSON.stringify({
policyId: letter.policyId,
generation: letter.generation,
channel,
} satisfies RenewalMarkInput),
}),
});
setNotice(`Aviso marcado como enviado (${CHANNEL_LABEL[channel]}).`);
setNotice(`Aviso enviado a ${result.to}.`);
await refresh();
} catch (e) {
setActionError((e as Error)?.message ?? "No se pudo registrar el aviso.");
setActionError((e as Error)?.message ?? "No se pudo enviar el aviso.");
} finally {
setSendingKey(null);
}
}
@@ -224,17 +232,17 @@ export function NotificacionesPolizas() {
<button
type="button"
className="btn btn-outline btn-sm"
onClick={() => handleMark(item, "EMAIL")}
disabled={!item.customerEmail}
onClick={() => handleSend(item)}
disabled={
!item.customerEmail ||
sweeping ||
sendingKey !== null
}
>
Marcar EMAIL
</button>
<button
type="button"
className="btn btn-outline btn-sm"
onClick={() => handleMark(item, "MAIL")}
>
Marcar impreso
{sendingKey ===
`${item.policyId}-${item.generation}`
? "Enviando…"
: "Enviar aviso"}
</button>
</div>
</td>