mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 17:38:12 +00:00
changes
This commit is contained in:
@@ -3,7 +3,11 @@ import toast from "react-hot-toast";
|
||||
|
||||
import { QUERY_KEYS } from "@/constants/QUERY_KEYS";
|
||||
import { api } from "@/services/api";
|
||||
import { ruleEngineService, type RuleEngineListParams } from "@/services/ruleEngine/ruleEngine.service";
|
||||
import {
|
||||
ruleEngineService,
|
||||
type RuleEngineListParams,
|
||||
type SubmitPriorityRuleChangePayload,
|
||||
} from "@/services/ruleEngine/ruleEngine.service";
|
||||
import { RULE_ENGINE_SELECT_NONE } from "@/pages/ruleEngine/config/resources";
|
||||
import type {
|
||||
RuleEngineRecord,
|
||||
@@ -239,6 +243,68 @@ export const useRuleEngineMutations = (resource: RuleEngineResourceSlug) => {
|
||||
return { create, update, remove };
|
||||
};
|
||||
|
||||
/**
|
||||
* Priority-rule approval workflow. Every create/update/delete of a priority
|
||||
* config is SUBMITTED as a change request; an approver applies or rejects it.
|
||||
* Error toasts surface the backend message so range-collision rejections
|
||||
* ("1–5 overlaps existing rule …") reach the user verbatim.
|
||||
*/
|
||||
export const usePriorityRuleWorkflow = (enabled: boolean) => {
|
||||
const qc = useQueryClient();
|
||||
|
||||
const backendMessage = (err: unknown, fallback: string) => {
|
||||
const msg = (err as { response?: { data?: { message?: string | string[] } } })
|
||||
?.response?.data?.message;
|
||||
if (Array.isArray(msg)) return msg.join(", ");
|
||||
return msg || fallback;
|
||||
};
|
||||
|
||||
const pending = useQuery({
|
||||
queryKey: QUERY_KEYS.RULE_ENGINE.priorityRuleChanges,
|
||||
queryFn: () => ruleEngineService.listPriorityRuleChanges("PENDING"),
|
||||
enabled,
|
||||
});
|
||||
|
||||
const invalidate = async () => {
|
||||
await qc.invalidateQueries({
|
||||
queryKey: QUERY_KEYS.RULE_ENGINE.priorityRuleChanges,
|
||||
});
|
||||
await invalidateRuleEngineList(qc, "priority-configs");
|
||||
};
|
||||
|
||||
const submit = useMutation({
|
||||
mutationFn: (payload: SubmitPriorityRuleChangePayload) =>
|
||||
ruleEngineService.submitPriorityRuleChange(payload),
|
||||
onSuccess: async () => {
|
||||
toast.success("Change submitted for approval — the team has been notified");
|
||||
await invalidate();
|
||||
},
|
||||
onError: (err) => toast.error(backendMessage(err, "Failed to submit change")),
|
||||
});
|
||||
|
||||
const approve = useMutation({
|
||||
mutationFn: ({ id, decisionNote }: { id: string; decisionNote?: string }) =>
|
||||
ruleEngineService.approvePriorityRuleChange(id, decisionNote),
|
||||
onSuccess: async () => {
|
||||
toast.success("Change approved and applied");
|
||||
await invalidate();
|
||||
},
|
||||
onError: (err) => toast.error(backendMessage(err, "Failed to approve change")),
|
||||
});
|
||||
|
||||
const reject = useMutation({
|
||||
mutationFn: ({ id, decisionNote }: { id: string; decisionNote?: string }) =>
|
||||
ruleEngineService.rejectPriorityRuleChange(id, decisionNote),
|
||||
onSuccess: async () => {
|
||||
toast.success("Change rejected");
|
||||
await invalidate();
|
||||
},
|
||||
onError: (err) => toast.error(backendMessage(err, "Failed to reject change")),
|
||||
});
|
||||
|
||||
return { pending, submit, approve, reject };
|
||||
};
|
||||
|
||||
export const useRateWorkflow = () => {
|
||||
const qc = useQueryClient();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user