55 lines
1.7 KiB
JavaScript
55 lines
1.7 KiB
JavaScript
import { authenticatedRequest } from "@/components/session/authenticatedRequest.vue";
|
|
import {
|
|
emptyBookingCounts,
|
|
isExpectedBookingCountRequestFailure,
|
|
} from "@/components/models/navigation/items/bookingCountGuards.js";
|
|
|
|
const normalizePositiveInteger = (value) => {
|
|
const parsedValue = Number.parseInt(String(value ?? ""), 10);
|
|
return Number.isInteger(parsedValue) && parsedValue > 0 ? parsedValue : null;
|
|
};
|
|
|
|
const toNonNegativeInteger = (value) => {
|
|
const parsedValue = Number.parseInt(String(value ?? ""), 10);
|
|
return Number.isInteger(parsedValue) && parsedValue >= 0 ? parsedValue : 0;
|
|
};
|
|
|
|
const normalizeBookingCounts = (response) => {
|
|
const counts = response?.data?.data;
|
|
|
|
return {
|
|
past: toNonNegativeInteger(counts?.past),
|
|
current: toNonNegativeInteger(counts?.current),
|
|
future: toNonNegativeInteger(counts?.future),
|
|
};
|
|
};
|
|
|
|
export const fetchDepartmentOrderBookingCounts = async ({ departmentId }) => {
|
|
const normalizedDepartmentId = normalizePositiveInteger(departmentId);
|
|
|
|
if (!normalizedDepartmentId) {
|
|
return emptyBookingCounts();
|
|
}
|
|
|
|
try {
|
|
const response = await authenticatedRequest("/order-bookings/counts", "GET", {
|
|
department: normalizedDepartmentId,
|
|
});
|
|
|
|
return normalizeBookingCounts(response);
|
|
} catch (error) {
|
|
if (!isExpectedBookingCountRequestFailure(error)) {
|
|
console.error("Error fetching department order-booking counts:", error);
|
|
}
|
|
|
|
return emptyBookingCounts();
|
|
}
|
|
};
|
|
|
|
export const fetchDepartmentOrderBookingCount = async ({ departmentId }) => {
|
|
const counts = await fetchDepartmentOrderBookingCounts({ departmentId });
|
|
return counts.current;
|
|
};
|
|
|
|
export default fetchDepartmentOrderBookingCount;
|