mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 15:18:11 +00:00
fix rate edit
This commit is contained in:
@@ -7,6 +7,7 @@ import {
|
||||
ruleEngineService,
|
||||
type RuleEngineListParams,
|
||||
type SubmitPriorityRuleChangePayload,
|
||||
type SubmitRateChangePayload,
|
||||
} from "@/services/ruleEngine/ruleEngine.service";
|
||||
import { RULE_ENGINE_SELECT_NONE } from "@/pages/ruleEngine/config/resources";
|
||||
import type {
|
||||
@@ -318,6 +319,71 @@ export const usePriorityRuleWorkflow = (
|
||||
return { pending, submit, approve, reject };
|
||||
};
|
||||
|
||||
/**
|
||||
* Approval workflow for edits to LIVE rates. The live rate keeps its current
|
||||
* value until a change is approved, so the rates list is invalidated on every
|
||||
* outcome — including reject, which restores the row's "no pending" state.
|
||||
*/
|
||||
export const useRateChangeWorkflow = (
|
||||
enabled: boolean,
|
||||
onErrorMessage?: (message: string) => void,
|
||||
) => {
|
||||
const qc = useQueryClient();
|
||||
|
||||
const showError = (err: unknown, fallback: string) => {
|
||||
const raw = (err as { response?: { data?: { message?: string | string[] } } })
|
||||
?.response?.data?.message;
|
||||
const message = (Array.isArray(raw) ? raw.join(", ") : raw) || fallback;
|
||||
if (onErrorMessage) onErrorMessage(message);
|
||||
else toast.error(message);
|
||||
};
|
||||
|
||||
const pending = useQuery({
|
||||
queryKey: QUERY_KEYS.RULE_ENGINE.rateChanges,
|
||||
queryFn: () => ruleEngineService.listRateChanges("PENDING"),
|
||||
enabled,
|
||||
});
|
||||
|
||||
const invalidate = async () => {
|
||||
await qc.invalidateQueries({ queryKey: QUERY_KEYS.RULE_ENGINE.rateChanges });
|
||||
await invalidateRuleEngineList(qc, "rates");
|
||||
};
|
||||
|
||||
const submit = useMutation({
|
||||
mutationFn: (payload: SubmitRateChangePayload) =>
|
||||
ruleEngineService.submitRateChange(payload),
|
||||
onSuccess: async () => {
|
||||
toast.success(
|
||||
"Change submitted for approval — the rate keeps its current value until approved",
|
||||
);
|
||||
await invalidate();
|
||||
},
|
||||
onError: (err) => showError(err, "Failed to submit rate change"),
|
||||
});
|
||||
|
||||
const approve = useMutation({
|
||||
mutationFn: ({ id, decisionNote }: { id: string; decisionNote?: string }) =>
|
||||
ruleEngineService.approveRateChange(id, decisionNote),
|
||||
onSuccess: async () => {
|
||||
toast.success("Rate change approved — the new rate is now live");
|
||||
await invalidate();
|
||||
},
|
||||
onError: (err) => showError(err, "Failed to approve rate change"),
|
||||
});
|
||||
|
||||
const reject = useMutation({
|
||||
mutationFn: ({ id, decisionNote }: { id: string; decisionNote?: string }) =>
|
||||
ruleEngineService.rejectRateChange(id, decisionNote),
|
||||
onSuccess: async () => {
|
||||
toast.success("Rate change rejected — the rate keeps its current value");
|
||||
await invalidate();
|
||||
},
|
||||
onError: (err) => showError(err, "Failed to reject rate change"),
|
||||
});
|
||||
|
||||
return { pending, submit, approve, reject };
|
||||
};
|
||||
|
||||
export const useRateWorkflow = () => {
|
||||
const qc = useQueryClient();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user