DigitalConsumer.FixMyGlass/src/helpers/recal-helper.js
2024-10-15 17:02:03 -04:00

60 lines
1.7 KiB
JavaScript

import { deepClone } from "@/helpers/object-helper";
import { partTypeStrings } from "@/constants/part-type-strings";
const recalPartTypes = [partTypeStrings.RECALIBRATION, partTypeStrings.ADAS_RECALIBRATION];
export function isRecalPartOrHasChildRecalPart(lineItem) {
if (lineItem.childParts && lineItem.childParts.length > 0) {
return isRecalPart(lineItem) || containsRecalParts(lineItem.childParts);
} else {
return isRecalPart(lineItem);
}
}
export function isRecalPart(lineItem) {
return recalPartTypes.some((type) => lineItem.partType === type);
}
export function containsRecalParts(lineItems) {
if (!lineItems) {
return false;
}
if (Array.isArray(lineItems)) {
return lineItems.some((li) => isRecalPartOrHasChildRecalPart(li));
} else {
// complex object form -- flatten and re-call.
const flattened = [
...(lineItems.glassParts ?? []),
...(lineItems.supportingItems ?? []),
...(lineItems.vaps ?? []),
...(lineItems.promos ?? []),
];
return flattened.some((li) => isRecalPartOrHasChildRecalPart(li));
}
}
export function getItemsWithoutRecalParts(lineItems) {
const copy = deepClone(lineItems);
const firstLevelFiltered = copy.filter((li) => !isRecalPart(li));
const childrenFiltered = firstLevelFiltered.map((li) => {
if (li.childParts && li.childParts.length > 0) {
li.childParts = getItemsWithoutRecalParts(li.childParts);
}
return li;
});
return childrenFiltered;
}
export function getTopLevelPartsWithRecal(lineItems) {
if (!lineItems) {
return null;
}
return lineItems.filter((li) => isRecalPartOrHasChildRecalPart(li));
}