114 lines
3.6 KiB
JavaScript
114 lines
3.6 KiB
JavaScript
import { resolveReleaseApiUrl } from "@/services/releaseTimeline.js";
|
|
import {
|
|
SELF_SERVE_TASK_BUTTON_PROGRAM_PICKER,
|
|
normalizeSelfServeTaskButtons,
|
|
} from "@/components/session/token/SessionUser/Objects/selfServeTaskButtons.js";
|
|
|
|
const normalizeTaskServices = (task) => (
|
|
Array.isArray(task?.services)
|
|
? task.services.map((service) => String(service || "").trim().toUpperCase()).filter(Boolean)
|
|
: []
|
|
);
|
|
|
|
export const selfServeTaskUsesProgramPicker = (task) => (
|
|
normalizeTaskServices(task).includes("PROGRAM_PICKER")
|
|
);
|
|
|
|
export const getSelfServeTaskDynamicImageButtons = (task) => {
|
|
const buttons = normalizeSelfServeTaskButtons(
|
|
task?.buttons ?? task?.button_ids ?? task?.machine_buttons ?? task?.dynamic_image_buttons
|
|
);
|
|
|
|
if (!selfServeTaskUsesProgramPicker(task)) {
|
|
return buttons;
|
|
}
|
|
|
|
return normalizeSelfServeTaskButtons([SELF_SERVE_TASK_BUTTON_PROGRAM_PICKER, ...buttons]);
|
|
};
|
|
|
|
export const getSelfServeDynamicImageButtonsToPress = (tasks = []) => (
|
|
Array.isArray(tasks) ? tasks : []
|
|
).flatMap((task) => getSelfServeTaskDynamicImageButtons(task));
|
|
|
|
export const getSelfServeCompletedDynamicImageStep = (tasks = [], completedTasks = {}) => {
|
|
let totalButtonsCompleted = 0;
|
|
|
|
(Array.isArray(tasks) ? tasks : []).forEach((task) => {
|
|
if (completedTasks?.[task?.id] || completedTasks?.[task?.task_id]) {
|
|
totalButtonsCompleted += getSelfServeTaskDynamicImageButtons(task).length;
|
|
}
|
|
});
|
|
|
|
return totalButtonsCompleted;
|
|
};
|
|
|
|
export const parseSelfServeDynamicImageThumbPosition = (value) => {
|
|
if (value === null || value === undefined || value === "") {
|
|
return null;
|
|
}
|
|
|
|
const thumbPosition = Number.parseInt(String(value), 10);
|
|
return Number.isInteger(thumbPosition) && thumbPosition >= 1 && thumbPosition <= 12
|
|
? thumbPosition
|
|
: null;
|
|
};
|
|
|
|
export const getSelfServeDynamicImageThumbPosition = (tasks = []) => {
|
|
for (const task of Array.isArray(tasks) ? tasks : []) {
|
|
const thumbPosition = parseSelfServeDynamicImageThumbPosition(
|
|
task?.dynamic_images_vehicle_type ?? task?.dynamic_image_vehicle_type ?? task?.dynamicImagesVehicleType
|
|
);
|
|
if (thumbPosition !== null) {
|
|
return thumbPosition;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
};
|
|
|
|
export const buildSelfServeDynamicImageUrl = ({
|
|
departmentId,
|
|
laneId,
|
|
dynamicImageId = null,
|
|
buttons = null,
|
|
currentStep = 0,
|
|
vehicleTypeId = null,
|
|
thumbPosition = null,
|
|
onlyCurrentStep = false,
|
|
}) => {
|
|
const normalizedDepartmentId = Number.parseInt(String(departmentId ?? ""), 10);
|
|
const normalizedLaneId = Number.parseInt(String(laneId ?? ""), 10);
|
|
if (!Number.isInteger(normalizedDepartmentId) || normalizedDepartmentId <= 0 || !Number.isInteger(normalizedLaneId) || normalizedLaneId <= 0) {
|
|
return null;
|
|
}
|
|
|
|
const params = new URLSearchParams({
|
|
department: String(normalizedDepartmentId),
|
|
lane: String(normalizedLaneId),
|
|
current_step: String(Math.max(0, Number.parseInt(String(currentStep ?? 0), 10) || 0)),
|
|
});
|
|
|
|
if (dynamicImageId !== null && dynamicImageId !== undefined && dynamicImageId !== "") {
|
|
params.set("dynamic_image_id", String(dynamicImageId));
|
|
}
|
|
|
|
if (Array.isArray(buttons)) {
|
|
params.set("buttons", JSON.stringify(buttons));
|
|
}
|
|
|
|
if (vehicleTypeId !== null && vehicleTypeId !== undefined && vehicleTypeId !== "") {
|
|
params.set("vehicle_type", String(vehicleTypeId));
|
|
}
|
|
|
|
const normalizedThumbPosition = parseSelfServeDynamicImageThumbPosition(thumbPosition);
|
|
if (normalizedThumbPosition !== null) {
|
|
params.set("thumb_position", String(normalizedThumbPosition));
|
|
}
|
|
|
|
if (onlyCurrentStep) {
|
|
params.set("only_current_step", "1");
|
|
}
|
|
|
|
return resolveReleaseApiUrl(`/department/lanes/dynamic-image?${params.toString()}`);
|
|
};
|