dow-prod-tracker/src/hooks/use-notification-rules.ts
Leivur R. Djurhuus 40028b7ced feat: add pipeline stage resolver and organization access control
- Implemented `stage-resolver.ts` to unify old and new pipeline stage definitions.
- Created `org-scope.ts` for organization access verification and scoping queries.
- Added role-based permissions management in `permissions.ts` and `rbac-service.ts`.
- Introduced invitation management in `invitation-service.ts` with validation schemas.
- Developed custom field and notification rule services with respective validators.
- Established pipeline template CRUD operations in `pipeline-template-service.ts`.
- Added Zustand store for managing pipeline builder state in `pipeline-builder-store.ts`.
2026-03-14 22:43:43 -05:00

78 lines
2.1 KiB
TypeScript

"use client";
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
async function fetchJson<T>(url: string, init?: RequestInit): Promise<T> {
const res = await fetch(url, init);
if (!res.ok) {
const body = await res.json().catch(() => ({}));
throw new Error(body.error || `Request failed: ${res.status}`);
}
return res.json();
}
export function useNotificationRules() {
return useQuery({
queryKey: ["notification-rules"],
queryFn: () => fetchJson("/api/org/notification-rules"),
});
}
export function useCreateNotificationRule() {
const qc = useQueryClient();
return useMutation({
mutationFn: (data: {
name: string;
event: string;
conditions?: { field: string; operator: string; value: any }[];
channels: string[];
recipientRoles: string[];
isEnabled?: boolean;
}) =>
fetchJson("/api/org/notification-rules", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
}),
onSuccess: () => {
qc.invalidateQueries({ queryKey: ["notification-rules"] });
},
});
}
export function useUpdateNotificationRule() {
const qc = useQueryClient();
return useMutation({
mutationFn: ({
id,
...data
}: {
id: string;
name?: string;
event?: string;
conditions?: { field: string; operator: string; value: any }[] | null;
channels?: string[];
recipientRoles?: string[];
isEnabled?: boolean;
}) =>
fetchJson(`/api/org/notification-rules/${id}`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
}),
onSuccess: () => {
qc.invalidateQueries({ queryKey: ["notification-rules"] });
},
});
}
export function useDeleteNotificationRule() {
const qc = useQueryClient();
return useMutation({
mutationFn: (id: string) =>
fetchJson(`/api/org/notification-rules/${id}`, { method: "DELETE" }),
onSuccess: () => {
qc.invalidateQueries({ queryKey: ["notification-rules"] });
},
});
}