diff --git a/src/helper/ChangeRequest.ts b/src/helper/ChangeRequest.ts index 7aac2ae..c04e67f 100644 --- a/src/helper/ChangeRequest.ts +++ b/src/helper/ChangeRequest.ts @@ -1,6 +1,11 @@ import _ from 'lodash'; import { toast } from 'react-toastify'; +interface ValueWithWeight { + type: string; + weight: number; +} + const isTarget = limitObject => { return limitObject.hasOwnProperty('approvalFrom'); }; @@ -55,6 +60,27 @@ const findSingleValue = (limit, value) => { return false; }; +const isWeightGreater = ( + limit: { valuesWithWeights?: ValueWithWeight[] }, + value: string, + previousValue: string, +): boolean => { + if (!limit.valuesWithWeights) return false; + + const weightsMap = new Map(); + + limit.valuesWithWeights.forEach(({ type, weight }) => { + weightsMap.set(type, weight); + }); + + const currentWeight = weightsMap.get(value); + const previousWeight = weightsMap.get(previousValue); + + return ( + currentWeight !== undefined && (previousWeight === undefined || currentWeight > previousWeight) + ); +}; + const isChangeRequestRequired = (limit, value, previousValue, manifestEnv) => { if (value === undefined) return false; const parsedValue = isString(value) ? parseValue(value) : value; @@ -85,6 +111,9 @@ const isChangeRequestRequired = (limit, value, previousValue, manifestEnv) => { if (findSingleValue(limit, value)) return true; } } + if (limit.hasOwnProperty('valuesWithWeights') && isWeightGreater(limit, value, previousValue)) { + return true; + } if (limit.hasOwnProperty('object')) { for (const json of limit.object) { const jsonObj = JSON.parse(json);