roleIds are numbers

This commit is contained in:
Owen
2026-04-20 17:19:44 -07:00
parent 9f5f89c9eb
commit 5a09062070
7 changed files with 27 additions and 153 deletions

View File

@@ -49,7 +49,6 @@ export default function EditAlertRulePage() {
});
setFormValues(null);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [orgId, alertRuleId]);
useEffect(() => {

View File

@@ -66,7 +66,7 @@ export type AlertRuleApiPayload = {
siteIds: number[];
healthCheckIds: number[];
userIds: string[];
roleIds: string[];
roleIds: number[];
emails: string[];
webhookActions: {
webhookUrl: string;
@@ -91,7 +91,7 @@ export type AlertRuleApiResponse = {
recipients: {
recipientId: number;
userId: string | null;
roleId: string | null;
roleId: number | null;
email: string | null;
}[];
webhookActions: {
@@ -297,7 +297,7 @@ export function apiResponseToFormValues(
.map((r) => ({ id: r.userId!, text: r.userId! }));
const roleTags = rule.recipients
.filter((r) => r.roleId != null)
.map((r) => ({ id: r.roleId!, text: r.roleId! }));
.map((r) => ({ id: String(r.roleId!), text: String(r.roleId!) }));
const emailTags = rule.recipients
.filter((r) => r.email != null)
.map((r) => ({ id: r.email!, text: r.email! }));
@@ -358,7 +358,7 @@ export function formValuesToApiPayload(
// Collect all notify-type actions and merge their recipient lists
const allUserIds: string[] = [];
const allRoleIds: string[] = [];
const allRoleIds: number[] = [];
const allEmails: string[] = [];
const webhookActions: AlertRuleApiPayload["webhookActions"] = [];
@@ -366,7 +366,7 @@ export function formValuesToApiPayload(
for (const action of values.actions) {
if (action.type === "notify") {
allUserIds.push(...action.userTags.map((t) => t.id));
allRoleIds.push(...action.roleTags.map((t) => t.id));
allRoleIds.push(...action.roleTags.map((t) => Number(t.id)));
allEmails.push(
...action.emailTags
.map((t) => t.text.trim())
@@ -391,7 +391,7 @@ export function formValuesToApiPayload(
// Deduplicate
const uniqueUserIds = [...new Set(allUserIds)];
const uniqueRoleIds = [...new Set(allRoleIds)];
const uniqueRoleIds: number[] = [...new Set(allRoleIds)];
const uniqueEmails = [...new Set(allEmails)];
return {

View File

@@ -1,129 +0,0 @@
import { z } from "zod";
const STORAGE_PREFIX = "pangolin:alert-rules:";
export const webhookHeaderEntrySchema = z.object({
key: z.string(),
value: z.string()
});
export const alertActionSchema = z.discriminatedUnion("type", [
z.object({
type: z.literal("notify"),
userIds: z.array(z.string()),
roleIds: z.array(z.number()),
emails: z.array(z.string())
}),
z.object({
type: z.literal("webhook"),
url: z.string().url(),
method: z.string().min(1),
headers: z.array(webhookHeaderEntrySchema),
secret: z.string().optional()
})
]);
export const alertSourceSchema = z.discriminatedUnion("type", [
z.object({
type: z.literal("site"),
siteIds: z.array(z.number())
}),
z.object({
type: z.literal("health_check"),
targetIds: z.array(z.number())
})
]);
export const alertTriggerSchema = z.enum([
"site_online",
"site_offline",
"health_check_healthy",
"health_check_unhealthy"
]);
export const alertRuleSchema = z.object({
id: z.string().uuid(),
name: z.string().min(1).max(255),
enabled: z.boolean(),
createdAt: z.string(),
updatedAt: z.string(),
source: alertSourceSchema,
trigger: alertTriggerSchema,
actions: z.array(alertActionSchema).min(1)
});
export type AlertRule = z.infer<typeof alertRuleSchema>;
export type AlertAction = z.infer<typeof alertActionSchema>;
export type AlertTrigger = z.infer<typeof alertTriggerSchema>;
function storageKey(orgId: string) {
return `${STORAGE_PREFIX}${orgId}`;
}
export function getRule(orgId: string, ruleId: string): AlertRule | undefined {
return loadRules(orgId).find((r) => r.id === ruleId);
}
export function loadRules(orgId: string): AlertRule[] {
if (typeof window === "undefined") {
return [];
}
try {
const raw = localStorage.getItem(storageKey(orgId));
if (!raw) {
return [];
}
const parsed = JSON.parse(raw) as unknown;
if (!Array.isArray(parsed)) {
return [];
}
const out: AlertRule[] = [];
for (const item of parsed) {
const r = alertRuleSchema.safeParse(item);
if (r.success) {
out.push(r.data);
}
}
return out;
} catch {
return [];
}
}
export function saveRules(orgId: string, rules: AlertRule[]) {
if (typeof window === "undefined") {
return;
}
localStorage.setItem(storageKey(orgId), JSON.stringify(rules));
}
export function upsertRule(orgId: string, rule: AlertRule) {
const rules = loadRules(orgId);
const i = rules.findIndex((r) => r.id === rule.id);
if (i >= 0) {
rules[i] = rule;
} else {
rules.push(rule);
}
saveRules(orgId, rules);
}
export function deleteRule(orgId: string, ruleId: string) {
const rules = loadRules(orgId).filter((r) => r.id !== ruleId);
saveRules(orgId, rules);
}
export function newRuleId() {
if (typeof crypto !== "undefined" && crypto.randomUUID) {
return crypto.randomUUID();
}
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0;
const v = c === "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
export function isoNow() {
return new Date().toISOString();
}