feat(notificaciones): one send log across servicios and pólizas
Build and Push Images / Build jorgecuadros-web (push) Successful in 2m30s
Build and Push Images / Build jorgecuadros-api (push) Failing after 3h13m42s

Renewal avisos left behind only a `RenewalNotice` row, whose sole job is
gating: a row with `sentAt` drops the policy off the pending list. It
cannot represent a failed send or a customer with no address, so the
Pólizas tab had no "Registro de envíos" to show and a sent notice simply
vanished from the list.

Renewals now write `email_notification_log` — the same table the four
bulk jobs write — as `RENEWAL_NOTICE` / `POLICIES`, with rows for
failures and no-email skips too. `RenewalNotice` keeps its gating role
unchanged; the two are complementary, not redundant.

- extend `EmailNotificationType` (+RENEWAL_NOTICE) and
  `EmailNotificationServicio` (+POLICIES); `level` now carries the aviso
  generation on renewal rows, so every reader must branch on the type
  first (`notificationLevelLabel()` is the one place that lives)
- backfill emailed notices (`channel = 'EMAIL'`) into the log; MAIL-channel
  rows are legacy printed letters and are deliberately left out
- extract `NotificationLogService`/`NotificationLogModule` as the single
  writer, so a feature that sends mail records it without pulling the
  bulk-job pipelines into its module
- `GET /notifications/log` and `/stats` take a comma-separated `servicio`
  list; each tab reads its own slice. This also fixes the "Omitidos"
  view, which mapped to no filter at all and showed every row
- share one `NotificationLogPanel` between both tabs
- pass SES_* / NOTIFICATION_ADMIN_EMAILS through the galactus compose,
  which was missing them entirely — mail is runtime config, not a CI
  secret, and the prod image sets NODE_ENV=production so a blank config
  fails loudly instead of falling back to stdout

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-02 03:01:03 -07:00
co-authored by Claude Opus 5
parent c0cc0d2ac2
commit 33833c3af9
20 changed files with 813 additions and 194 deletions
@@ -0,0 +1,14 @@
import { Module } from "@nestjs/common";
import { NotificationLogService } from "./notification-log.service";
/**
* Just the log writer, so a feature that sends mail can record it without
* importing `NotificationsModule` (which carries the four bulk-job pipelines
* and their controller). Imported by `NotificationsModule` and
* `RenewalsModule`.
*/
@Module({
providers: [NotificationLogService],
exports: [NotificationLogService],
})
export class NotificationLogModule {}
@@ -0,0 +1,74 @@
import { Injectable } from "@nestjs/common";
import {
EmailNotificationServicio,
EmailNotificationStatus,
EmailNotificationType,
} from "@jorgecuadros/database";
import { PrismaService } from "../prisma/prisma.service";
import { AttemptStatus } from "./notification.types";
/**
* The single writer for `email_notification_log`.
*
* Extracted out of `NotificationsService` so the renewal sweep can write the
* same rows as the four bulk jobs without pulling that service (and its four
* job pipelines) into `RenewalsModule`. Every outbound email the platform
* sends goes through here, which is what makes /notificaciones' "Registro de
* envíos" complete rather than per-feature.
*/
export interface NotificationLogEntry {
notificationType: EmailNotificationType;
servicio: EmailNotificationServicio;
/** Defaults to now(). Pass it when the row must line up exactly with
* another record of the same send (the renewal sweep pins it to
* `RenewalNotice.sentAt`). */
sendDate?: Date;
/** Type-dependent discriminator — see the `level` doc on the Prisma model.
* 0/1 for ACCOUNT_STATUS, the generation for RENEWAL_NOTICE. */
level?: number | null;
customerId: string | null;
customerName: string;
customerEmail: string;
subject: string;
bodySnapshot: string;
bodyRequestUrl?: string;
status: AttemptStatus;
debug: boolean;
providerMessageId?: string;
providerResponse?: string;
error?: string;
}
/** `providerResponse` is a VARCHAR(191); anything longer is a provider dump
* we only need the head of. Errors go to the TEXT `error` column and get
* the 4k cap the schema documents. */
const PROVIDER_RESPONSE_MAX = 180;
const ERROR_MAX = 4096;
@Injectable()
export class NotificationLogService {
constructor(private readonly prisma: PrismaService) {}
async record(entry: NotificationLogEntry): Promise<void> {
await this.prisma.emailNotificationLog.create({
data: {
notificationType: entry.notificationType,
servicio: entry.servicio,
...(entry.sendDate && { sendDate: entry.sendDate }),
level: entry.level ?? null,
customerId: entry.customerId,
customerName: entry.customerName,
customerEmail: entry.customerEmail,
subject: entry.subject,
bodySnapshot: entry.bodySnapshot,
bodyRequestUrl: entry.bodyRequestUrl ?? null,
debug: entry.debug,
providerMessageId: entry.providerMessageId ?? null,
providerResponse:
entry.providerResponse?.slice(0, PROVIDER_RESPONSE_MAX) ?? null,
status: entry.status as EmailNotificationStatus,
error: entry.error?.slice(0, ERROR_MAX) ?? null,
},
});
}
}
@@ -31,7 +31,17 @@ class ListLogDto {
@IsOptional() @Type(() => Number) @IsInt() @Min(1) page?: number;
@IsOptional() @Type(() => Number) @IsInt() @Min(1) @Max(200) pageSize?: number;
@IsOptional() @IsEnum(EmailNotificationType) type?: EmailNotificationType;
@IsOptional() @IsEnum(EmailNotificationServicio) servicio?: EmailNotificationServicio;
/** One or more servicios, comma-separated. The /notificaciones tabs each
* read their own slice of the one log: Servicios passes
* `CUSTOMERS,TRUST`, Pólizas passes `POLICIES`. Omitted = every servicio. */
@IsOptional()
@Transform(({ value }) =>
typeof value === "string"
? value.split(",").map((s) => s.trim()).filter(Boolean)
: value,
)
@IsEnum(EmailNotificationServicio, { each: true })
servicio?: EmailNotificationServicio[];
@IsOptional() @IsEnum(EmailNotificationStatus) status?: EmailNotificationStatus;
@IsOptional() @IsEnum(["sent", "failed", "skipped", "all"]) view?: "sent" | "failed" | "skipped" | "all";
}
@@ -185,19 +195,27 @@ export class NotificationsController {
}
@Get("stats")
stats() {
return this.svc.stats();
stats(@Query() q: ListLogDto) {
return this.svc.stats(q.servicio);
}
/** Resolve the UI's coarse view tabs to concrete statuses. An explicit
* `status` wins. "Omitidos" covers both SKIPPED_* variants, which is why
* this returns a list rather than a single value. */
private mapViewStatus(
view: ListLogDto["view"],
status: ListLogDto["status"],
): EmailNotificationStatus | undefined {
if (status) return status;
): EmailNotificationStatus[] | undefined {
if (status) return [status];
if (!view || view === "all") return undefined;
if (view === "sent") return EmailNotificationStatus.SENT;
if (view === "failed") return EmailNotificationStatus.FAILED;
if (view === "skipped") return undefined; // both SKIPPED_* variants
if (view === "sent") return [EmailNotificationStatus.SENT];
if (view === "failed") return [EmailNotificationStatus.FAILED];
if (view === "skipped") {
return [
EmailNotificationStatus.SKIPPED_NO_EMAIL,
EmailNotificationStatus.SKIPPED_GATE,
];
}
return undefined;
}
}
@@ -1,4 +1,5 @@
import { Module } from "@nestjs/common";
import { NotificationLogModule } from "./notification-log.module";
import { NotificationsController } from "./notifications.controller";
import { NotificationsService } from "./notifications.service";
@@ -11,6 +12,7 @@ import { NotificationsService } from "./notifications.service";
* service methods are already the entry points they would call.
*/
@Module({
imports: [NotificationLogModule],
controllers: [NotificationsController],
providers: [NotificationsService],
exports: [NotificationsService],
@@ -10,6 +10,7 @@ import {
} from "@jorgecuadros/database";
import { MailService } from "../mail/mail.service";
import { PrismaService } from "../prisma/prisma.service";
import { NotificationLogService } from "./notification-log.service";
import {
SendAttempt,
NotificationJobKind,
@@ -81,6 +82,7 @@ export class NotificationsService {
constructor(
private readonly prisma: PrismaService,
private readonly mail: MailService,
private readonly log: NotificationLogService,
config: ConfigService,
) {
const csv = config.get<string>("NOTIFICATION_ADMIN_EMAILS");
@@ -721,14 +723,16 @@ export class NotificationsService {
page: number;
pageSize: number;
type?: EmailNotificationType;
servicio?: EmailNotificationServicio;
status?: EmailNotificationStatus;
/** Empty/omitted = every servicio. The /notificaciones tabs pass their
* own slice (Servicios: CUSTOMERS+TRUST, Pólizas: POLICIES). */
servicio?: EmailNotificationServicio[];
status?: EmailNotificationStatus[];
customerId?: string;
}) {
const where: Prisma.EmailNotificationLogWhereInput = {};
if (params.type) where.notificationType = params.type;
if (params.servicio) where.servicio = params.servicio;
if (params.status) where.status = params.status;
if (params.servicio?.length) where.servicio = { in: params.servicio };
if (params.status?.length) where.status = { in: params.status };
if (params.customerId) where.customerId = params.customerId;
const [total, rows] = await this.prisma.$transaction([
@@ -765,22 +769,32 @@ export class NotificationsService {
};
}
/** Per-type + per-status counts for the dashboard header. */
async stats() {
/** Per-type + per-status counts for the dashboard header. Scoped by
* servicio so each /notificaciones tab reports its own totals instead of
* the whole platform's. */
async stats(servicio?: EmailNotificationServicio[]) {
const where: Prisma.EmailNotificationLogWhereInput = servicio?.length
? { servicio: { in: servicio } }
: {};
const [byType, byStatus, byServicio, lastRun] = await Promise.all([
this.prisma.emailNotificationLog.groupBy({
by: ["notificationType", "status"],
where,
_count: { _all: true },
}),
this.prisma.emailNotificationLog.groupBy({
by: ["status"],
where,
_count: { _all: true },
}),
this.prisma.emailNotificationLog.groupBy({
by: ["servicio", "status"],
where,
_count: { _all: true },
}),
this.prisma.emailNotificationLog.findFirst({
where,
orderBy: { sendDate: "desc" },
select: { sendDate: true, notificationType: true },
}),
@@ -917,7 +931,9 @@ export class NotificationsService {
}
}
/** Persist one notification log row. */
/** Persist one notification log row. Thin pass-through to the shared
* writer — the renewal sweep writes the same rows through the same
* service, which is what keeps /notificaciones' log complete. */
private async recordAttempt(args: {
notificationType: EmailNotificationType;
servicio: EmailNotificationServicio;
@@ -934,24 +950,7 @@ export class NotificationsService {
providerResponse?: string;
error?: string;
}) {
await this.prisma.emailNotificationLog.create({
data: {
notificationType: args.notificationType,
servicio: args.servicio,
level: args.level ?? null,
customerId: args.customerId,
customerName: args.customerName,
customerEmail: args.customerEmail,
subject: args.subject,
bodySnapshot: args.bodySnapshot,
bodyRequestUrl: args.bodyRequestUrl ?? null,
debug: args.debug,
providerMessageId: args.providerMessageId ?? null,
providerResponse: args.providerResponse ?? null,
status: args.status as EmailNotificationStatus,
error: args.error ?? null,
},
});
await this.log.record(args);
}
/** Send the admin summary email after every job. The PHP sent one to