import { HttpException } from "@nestjs/common"; import { PrismaService } from "../../prisma.service.js"; export async function recordTemplateOperationFailure( prisma: PrismaService, input: { action: string; error: unknown; requestId?: string; actorId?: string; brandId?: string; packageKey?: string; version?: string; }, ): Promise { const status = input.error instanceof HttpException ? input.error.getStatus() : 500; try { await prisma.templateOperationFailureEvent.create({ data: { action: input.action, resultCode: `HTTP_${status}`, ...(input.requestId ? { requestId: input.requestId } : {}), ...(input.actorId ? { actorId: input.actorId } : {}), ...(input.brandId ? { brandId: input.brandId } : {}), ...(input.packageKey ? { packageKey: input.packageKey } : {}), ...(input.version ? { version: input.version } : {}), }, }); await prisma.templateOperationFailureEvent.deleteMany({ where: { createdAt: { lt: new Date(Date.now() - 180 * 24 * 60 * 60 * 1000) } }, }); } catch { // Failure telemetry must never replace the operation's original result. } }