geo/frontend/lib/api/alerts.ts

73 lines
2.0 KiB
TypeScript

import { fetchWithAuth } from "./client";
function buildQuery(params: Record<string, string | number | boolean | undefined>): string {
const qs = Object.entries(params)
.filter(([, v]) => v !== undefined)
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(String(v))}`)
.join("&");
return qs ? `?${qs}` : "";
}
export const alertsApi = {
/** 获取未读告警数量 */
getUnreadCount: (token?: string) =>
fetchWithAuth("/api/v1/alerts/unread-count", {}, token),
/** 获取告警列表 */
getAlerts: (
token?: string,
params?: { limit?: number; skip?: number; is_read?: boolean }
) => fetchWithAuth(`/api/v1/alerts/${buildQuery(params || {})}`, {}, token),
/** 标记单条告警已读 */
markRead: (alertId: string, token?: string) =>
fetchWithAuth(`/api/v1/alerts/${alertId}/read`, { method: "PATCH" }, token),
/** 标记所有告警已读 */
markAllRead: (token?: string) =>
fetchWithAuth("/api/v1/alerts/read-all", { method: "PATCH" }, token),
/** 获取告警设置 */
getSettings: (brandId: string, token?: string) =>
fetchWithAuth(`/api/v1/alerts/settings?brand_id=${encodeURIComponent(brandId)}`, {}, token),
/** 更新告警设置 */
updateSettings: (
data: Array<{
brand_id: string;
alert_type: string;
enabled: boolean;
threshold?: number;
}>,
token?: string
) =>
fetchWithAuth(
"/api/v1/alerts/settings",
{ method: "PUT", body: JSON.stringify({ settings: data }) },
token
),
/** 更新单条告警设置 */
updateSingleSetting: (
settingId: string,
data: {
enabled?: boolean;
threshold?: number;
},
token?: string
) =>
fetchWithAuth(
`/api/v1/alerts/settings/${settingId}`,
{ method: "PATCH", body: JSON.stringify(data) },
token
),
/** 删除告警设置 */
deleteSetting: (settingId: string, token?: string) =>
fetchWithAuth(
`/api/v1/alerts/settings/${settingId}`,
{ method: "DELETE" },
token
),
};