wulf-pulse/app/api/openclaw/datto-rmm/alerts/route.ts
lorentz 679fe3871c feat: add OpenClaw read-only Datto RMM API endpoints
GET /api/openclaw/datto-rmm/sites
GET /api/openclaw/datto-rmm/devices         (filters: siteUid, online, deleted, page, limit)
GET /api/openclaw/datto-rmm/devices/[uid]
GET /api/openclaw/datto-rmm/devices/[uid]/audit  (always live)
GET /api/openclaw/datto-rmm/alerts          (filters: resolved, siteUid, deviceUid, limit)
GET /api/openclaw/datto-rmm/alerts/open

- All protected by x-openclaw-key header
- Default: queries Pulse DB (datto_rmm_devices/alerts/sites tables)
- ?live=true: proxies to Datto RMM API via DattoRMMClient
- Responses include source:'db'|'live' for data freshness awareness
2026-03-21 17:45:19 -04:00

96 lines
3.1 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
import { validateOpenClawKey } from '@/lib/utils/openclaw-auth';
import { Pool } from 'pg';
import { getDattoRMMClient } from '@/lib/services/datto-rmm-factory';
const pool = new Pool({
host: process.env.POSTGRES_HOST,
port: parseInt(process.env.POSTGRES_PORT || '5432'),
database: process.env.POSTGRES_DB,
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
});
export async function GET(request: NextRequest) {
const authError = validateOpenClawKey(request);
if (authError) return authError;
const params = request.nextUrl.searchParams;
const live = params.get('live') === 'true';
const resolved = params.get('resolved');
const siteUid = params.get('siteUid');
const deviceUid = params.get('deviceUid');
const limit = Math.min(1000, Math.max(1, parseInt(params.get('limit') || '200', 10)));
try {
if (live) {
const client = getDattoRMMClient();
const openAlerts = await client.getAllOpenAlerts();
let alerts: typeof openAlerts = [];
if (resolved === 'true') {
const resolvedAlerts = await client.getRecentResolvedAlerts();
alerts = resolvedAlerts as typeof openAlerts;
} else if (resolved === 'false' || resolved === null) {
alerts = openAlerts;
} else {
const resolvedAlerts = await client.getRecentResolvedAlerts();
alerts = [...openAlerts, ...(resolvedAlerts as typeof openAlerts)];
}
if (siteUid) alerts = alerts.filter((a: any) => a.siteUid === siteUid);
if (deviceUid) alerts = alerts.filter((a: any) => a.deviceUid === deviceUid);
return NextResponse.json({
data: alerts.slice(0, limit),
total: alerts.length,
source: 'live',
});
}
const conditions: string[] = [];
const values: unknown[] = [];
if (resolved !== null) {
values.push(resolved === 'true');
conditions.push(`resolved = $${values.length}`);
}
if (siteUid) {
values.push(siteUid);
conditions.push(`site_uid = $${values.length}`);
}
if (deviceUid) {
values.push(deviceUid);
conditions.push(`device_uid = $${values.length}`);
}
const where = conditions.length ? `WHERE ${conditions.join(' AND ')}` : '';
values.push(limit);
const result = await pool.query(
`SELECT
alert_uid, alert_category, alert_type, alert_message_en,
device_uid, device_hostname, device_ip, device_os, device_description,
site_uid, site_name,
resolved, resolved_by, resolved_on, muted,
ticket_number, timestamp, platform, triggered,
alert_context, alert_monitor_info,
last_user, ping_target,
synced_at, updated_at
FROM datto_rmm_alerts
${where}
ORDER BY timestamp DESC
LIMIT $${values.length}`,
values
);
return NextResponse.json({
data: result.rows,
total: result.rowCount,
source: 'db',
});
} catch (error) {
const msg = error instanceof Error ? error.message : String(error);
return NextResponse.json({ error: msg }, { status: 500 });
}
}