DigitalConsumer.ISS/src/helpers/recal-helper.js

128 lines
4.2 KiB
JavaScript

import partNumberStrings from '@/constants/part-number-strings';
import partTypeStrings from '@/constants/part-type-strings';
import { deepClone, getNonFalseValuesOfPropertyInArrayOfObjects } from '@/helpers/object-helper';
const recalPartTypes = [partTypeStrings.RECALIBRATION, partTypeStrings.ADAS_RECALIBRATION];
export function isRecalPart(part) {
return recalPartTypes.some((type) => type === part.partType);
}
export function isRecalPartOrHasChildRecalPart(glassPart) {
const isGlassPartRecalPart = isRecalPart(glassPart);
if (!isGlassPartRecalPart && glassPart.childParts && glassPart.childParts.length > 0) {
return glassPart.childParts.some((cp) => isRecalPartOrHasChildRecalPart(cp));
}
return isGlassPartRecalPart;
}
export function getTopLevelGlassPartsWithRecal(glassParts) {
if (!glassParts) {
return null;
}
return glassParts.filter((gp) => isRecalPartOrHasChildRecalPart(gp));
}
export function getItemsWithoutRecalParts(lineItemsArray) {
if (!lineItemsArray || !Array.isArray(lineItemsArray)) return null;
const firstLevelFiltered = deepClone(lineItemsArray).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 getRecalPartNumbers(glassPartsArray) {
if (glassPartsArray && glassPartsArray.length > 0) {
const topLevel = glassPartsArray.filter((gp) => isRecalPart(gp)).map((gp) => gp.partNumber);
const children = glassPartsArray.map((gp) => getRecalPartNumbers(gp.childParts)).flat();
return [...topLevel, ...children];
} else {
return [];
}
}
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.otherParts ?? []),
...(lineItems.feeItems ?? []),
...(lineItems.vaps ?? []),
];
return flattened.some((li) => isRecalPartOrHasChildRecalPart(li));
}
}
export function isRecalOrder(lineItems) {
return (containsRecalParts(lineItems) && getHasRecalibrationPart(lineItems));
}
export function getHasRecalibrationPart(lineItems) {
const hasRequiresRecalibration = getNonFalseValuesOfPropertyInArrayOfObjects(lineItems.glassParts, 'requiresRecalibration')?.length > 0;
const hasRecalibrationType = getNonFalseValuesOfPropertyInArrayOfObjects(lineItems.glassParts, 'recalibrationType')?.length > 0;
if (hasRequiresRecalibration) {
if (hasRecalibrationType) {
// Has both 'requiresRecalibration' and 'recalibrationType' and 'recalibrationType'
return (
getNonFalseValuesOfPropertyInArrayOfObjects(
lineItems.glassParts,
'recalibrationType'
)[0].toLowerCase() !== 'unknown'
);
}
// Has 'requiresRecalibration' but no 'recalibrationType' at all
return true;
}
// Does not have 'requiresRecalibration'
return false;
}
export function anyPartWithRequiresRecalFlag(lineItems) {
if (!lineItems) {
return false;
}
if (Array.isArray(lineItems)) {
return lineItems.some((li) => li.requiresRecalibration === true);
}
const flattened = [
...(lineItems.glassParts ?? []),
...(lineItems.supportingItems ?? []),
...(lineItems.otherParts ?? []),
...(lineItems.feeItems ?? []),
...(lineItems.vaps ?? []),
];
return flattened.some((li) => li.requiresRecalibration === true);
}
export function hasMSRPart(lineItems) {
return lineItems?.feeItems?.some((item) => isMSRPart(item)) ?? false;
}
export function isMSRPart(part) {
return part.partNumber === partNumberStrings.RECAL_MOBILEDUAL || part.partNumber === partNumberStrings.RECAL_MOBILE;
}