|
| 1 | +import { ok, bad, handleError, enforceRateLimit } from "../../../_utils"; |
| 2 | +import { getAppUser } from "@/lib/auth"; |
| 3 | +import { buildDailyIntelligenceReport, formatReportPlainText } from "@/lib/analytics/dailyReport"; |
| 4 | +import { sendDailyObservabilityEmail } from "@/lib/analytics/emailReport"; |
| 5 | + |
| 6 | +export const runtime = "nodejs"; |
| 7 | +export const dynamic = "force-dynamic"; |
| 8 | +export const maxDuration = 60; |
| 9 | + |
| 10 | +/** Admin: view or email the daily intelligence report. */ |
| 11 | +export async function GET(req: Request) { |
| 12 | + try { |
| 13 | + const limited = await enforceRateLimit(req, "default"); |
| 14 | + if (limited) return limited; |
| 15 | + |
| 16 | + const user = await getAppUser(); |
| 17 | + if (!user || user.role !== "admin") return bad("Admin access required", 403); |
| 18 | + |
| 19 | + const url = new URL(req.url); |
| 20 | + const days = Math.min(90, Math.max(7, Number(url.searchParams.get("days") || 30))); |
| 21 | + const format = (url.searchParams.get("format") || "json").toLowerCase(); |
| 22 | + const report = await buildDailyIntelligenceReport(days); |
| 23 | + |
| 24 | + if (format === "text" || format === "txt") { |
| 25 | + return new Response(formatReportPlainText(report), { |
| 26 | + headers: { |
| 27 | + "Content-Type": "text/plain; charset=utf-8", |
| 28 | + "Cache-Control": "no-store", |
| 29 | + }, |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + return ok({ |
| 34 | + report, |
| 35 | + email: { |
| 36 | + configured: Boolean(process.env.RESEND_API_KEY), |
| 37 | + enabled: process.env.OBSERVABILITY_DAILY_EMAIL_ENABLED !== "false", |
| 38 | + to: |
| 39 | + process.env.OBSERVABILITY_REPORT_EMAIL || |
| 40 | + process.env.OWNER_EMAIL || |
| 41 | + "kubee3302@gmail.com", |
| 42 | + schedule: "0 6 * * * (06:00 UTC daily via Vercel Cron)", |
| 43 | + }, |
| 44 | + admin: { email: user.email }, |
| 45 | + }); |
| 46 | + } catch (e) { |
| 47 | + return handleError(e); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/** Admin: send the daily report email now. */ |
| 52 | +export async function POST(req: Request) { |
| 53 | + try { |
| 54 | + const limited = await enforceRateLimit(req, "default"); |
| 55 | + if (limited) return limited; |
| 56 | + |
| 57 | + const user = await getAppUser(); |
| 58 | + if (!user || user.role !== "admin") return bad("Admin access required", 403); |
| 59 | + |
| 60 | + const body = (await req.json().catch(() => ({}))) as { days?: number }; |
| 61 | + const days = Math.min(90, Math.max(7, Number(body.days || 30))); |
| 62 | + const result = await sendDailyObservabilityEmail({ rangeDays: days, force: true }); |
| 63 | + |
| 64 | + return ok({ |
| 65 | + ok: result.ok, |
| 66 | + skipped: result.skipped ?? false, |
| 67 | + reason: result.reason, |
| 68 | + to: result.to, |
| 69 | + subject: result.subject, |
| 70 | + providerId: result.providerId, |
| 71 | + headline: result.report.headline, |
| 72 | + reportDate: result.report.reportDate, |
| 73 | + }); |
| 74 | + } catch (e) { |
| 75 | + return handleError(e); |
| 76 | + } |
| 77 | +} |
0 commit comments