mirror of
https://github.com/Sea-Haven-Industries/shoc-frontend-new.git
synced 2026-10-06 10:02:04 +00:00
Dispatchers see a one-line banner and a one-time toast while a Reactive/Emergency work order is past half of its SEV response window; the banner opens the board on exactly those work orders and dismisses for the session. Missed deadlines stay in the notification center until acknowledged, and Clear all skips them.
44 lines
1.6 KiB
TypeScript
44 lines
1.6 KiB
TypeScript
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
|
import { notificationsApi } from "@/domain/notifications/api/notifications-api";
|
|
import type {
|
|
NotificationFeed,
|
|
NotificationItem,
|
|
} from "@/domain/notifications/types/notification-feed";
|
|
import { queryKeys } from "@/infra/query-key/query-key";
|
|
|
|
function isBreachRowFor(workOrderId: number) {
|
|
return (item: NotificationItem) =>
|
|
item.reason === "sla" &&
|
|
item.rowType === "acknowledge" &&
|
|
item.target.workOrderId === workOrderId;
|
|
}
|
|
|
|
/** The feed without the acknowledged work order's breach row, until the next poll confirms it. */
|
|
export function withoutSlaBreach(feed: NotificationFeed, workOrderId: number): NotificationFeed {
|
|
const matches = isBreachRowFor(workOrderId);
|
|
return {
|
|
...feed,
|
|
sections: feed.sections.map((section) => {
|
|
const removed = section.items.filter(matches);
|
|
if (removed.length === 0) return section;
|
|
return {
|
|
...section,
|
|
items: section.items.filter((item) => !matches(item)),
|
|
count: Math.max(section.count - removed.reduce((total, item) => total + item.count, 0), 0),
|
|
};
|
|
}),
|
|
};
|
|
}
|
|
|
|
export function useAcknowledgeSlaBreach() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (workOrderId: number) => notificationsApi.acknowledgeSlaBreach(workOrderId),
|
|
onSuccess: (_result, workOrderId) => {
|
|
queryClient.setQueryData<NotificationFeed>(queryKeys.notifications.feed(), (feed) =>
|
|
feed ? withoutSlaBreach(feed, workOrderId) : feed,
|
|
);
|
|
},
|
|
onSettled: () => queryClient.invalidateQueries({ queryKey: queryKeys.notifications.feed() }),
|
|
});
|
|
}
|