Begin cert alerting

This commit is contained in:
Tuan Dang
2024-08-07 07:08:47 -07:00
parent 5af53d3398
commit 1690a9429c
32 changed files with 972 additions and 7 deletions
+2
View File
@@ -0,0 +1,2 @@
export { useCreateAlert, useDeleteAlert,useUpdateAlert } from "./mutations";
export { useGetAlertById } from "./queries";
@@ -0,0 +1,48 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { apiRequest } from "@app/config/request";
import { workspaceKeys } from "../workspace/queries";
import { alertKeys } from "./queries";
import { TAlert, TCreateAlertDTO, TDeleteAlertDTO,TUpdateAlertDTO } from "./types";
export const useCreateAlert = () => {
const queryClient = useQueryClient();
return useMutation<TAlert, {}, TCreateAlertDTO>({
mutationFn: async (body) => {
const { data: alert } = await apiRequest.post<TAlert>("/api/v1/alerts", body);
return alert;
},
onSuccess: (_, { projectId }) => {
queryClient.invalidateQueries(workspaceKeys.getWorkspaceAlerts(projectId));
}
});
};
export const useUpdateAlert = () => {
const queryClient = useQueryClient();
return useMutation<TAlert, {}, TUpdateAlertDTO>({
mutationFn: async ({ alertId, ...body }) => {
const { data: alert } = await apiRequest.patch<TAlert>(`/api/v1/alerts/${alertId}`, body);
return alert;
},
onSuccess: (_, { projectId, alertId }) => {
queryClient.invalidateQueries(workspaceKeys.getWorkspaceAlerts(projectId));
queryClient.invalidateQueries(alertKeys.getAlertById(alertId));
}
});
};
export const useDeleteAlert = () => {
const queryClient = useQueryClient();
return useMutation<TAlert, {}, TDeleteAlertDTO>({
mutationFn: async ({ alertId }) => {
const { data: alert } = await apiRequest.delete<TAlert>(`/api/v1/alerts/${alertId}`);
return alert;
},
onSuccess: (_, { projectId, alertId }) => {
queryClient.invalidateQueries(workspaceKeys.getWorkspaceAlerts(projectId));
queryClient.invalidateQueries(alertKeys.getAlertById(alertId));
}
});
};
+20
View File
@@ -0,0 +1,20 @@
import { useQuery } from "@tanstack/react-query";
import { apiRequest } from "@app/config/request";
import { TAlert } from "./types";
export const alertKeys = {
getAlertById: (alertId: string) => [{ alertId }, "alert"]
};
export const useGetAlertById = (alertId: string) => {
return useQuery({
queryKey: alertKeys.getAlertById(alertId),
queryFn: async () => {
const { data: alert } = await apiRequest.get<TAlert>(`/api/v1/alerts/${alertId}`);
return alert;
},
enabled: Boolean(alertId)
});
};
+29
View File
@@ -0,0 +1,29 @@
export type TAlert = {
id: string;
name: string;
projectId: string;
alertBeforeDays: number;
recipientEmails: string;
createdAt: string;
updatedAt: string;
};
export type TCreateAlertDTO = {
projectId: string;
name: string;
alertBeforeDays: number;
emails: string[];
};
export type TUpdateAlertDTO = {
alertId: string;
projectId: string;
name?: string;
alertBeforeDays?: number;
emails?: string[];
};
export type TDeleteAlertDTO = {
alertId: string;
projectId: string;
};
+1
View File
@@ -1,5 +1,6 @@
export * from "./accessApproval";
export * from "./admin";
export * from "./alerts";
export * from "./apiKeys";
export * from "./auditLogs";
export * from "./auditLogStreams";
+4 -3
View File
@@ -3,7 +3,8 @@ export {
useDeleteGroupFromWorkspace,
useLeaveProject,
useMigrateProjectToV3,
useUpdateGroupWorkspaceRole} from "./mutations";
useUpdateGroupWorkspaceRole
} from "./mutations";
export {
useAddIdentityToWorkspace,
useCreateWorkspace,
@@ -22,6 +23,7 @@ export {
useGetWorkspaceIntegrations,
useGetWorkspaceSecrets,
useGetWorkspaceUsers,
useListWorkspaceAlerts,
useListWorkspaceCas,
useListWorkspaceCertificates,
useListWorkspaceGroups,
@@ -31,5 +33,4 @@ export {
useUpdateIdentityWorkspaceRole,
useUpdateUserWorkspaceRole,
useUpdateWsEnvironment,
useUpgradeProject
} from "./queries";
useUpgradeProject} from "./queries";
+17 -1
View File
@@ -2,6 +2,7 @@ import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { apiRequest } from "@app/config/request";
import { TAlert } from "../alerts/types";
import { CaStatus } from "../ca/enums";
import { TCertificateAuthority } from "../ca/types";
import { TCertificate } from "../certificates/types";
@@ -62,7 +63,8 @@ export const workspaceKeys = {
slug: string;
offset: number;
limit: number;
}) => [...workspaceKeys.forWorkspaceCertificates(slug), { offset, limit }] as const
}) => [...workspaceKeys.forWorkspaceCertificates(slug), { offset, limit }] as const,
getWorkspaceAlerts: (workspaceId: string) => [{ workspaceId }, "workspace-alerts"] as const
};
const fetchWorkspaceById = async (workspaceId: string) => {
@@ -601,3 +603,17 @@ export const useListWorkspaceCertificates = ({
enabled: Boolean(projectSlug)
});
};
export const useListWorkspaceAlerts = ({ workspaceId }: { workspaceId: string }) => {
return useQuery({
queryKey: workspaceKeys.getWorkspaceAlerts(workspaceId),
queryFn: async () => {
const {
data: { alerts }
} = await apiRequest.get<{ alerts: TAlert[] }>(`/api/v2/workspace/${workspaceId}/alerts`);
return { alerts };
},
enabled: Boolean(workspaceId)
});
};