const beneficiaryArray = updatedBen.filter(
(el: any) => el.parentProfileGUID === '',
);
console.log('beneficiaryArray', beneficiaryArray);
// Substitute validation for all other willtypes (PW,FAW)
// Get Array of all substitutes in the payload
const substitutesArray = updatedBen.filter(
(el: any) => el.parentProfileGUID !== '',
); // Filter out substitutes from payload
console.log('substitutesArray', substitutesArray);
// Check if the list of substitutes is updated for each iteration
const substituteListForTest = checkSubstitutePresent(
substitutesArray,
beneficiaryList,
);
console.log('substituteListForTest', substituteListForTest);
// New Method testing
function filterPayloadByProperty(data:any) {
const groups = data.reduce((result:any, item:any) => {
const key = item.propertyGUID;
if (!result[key]) {
result[key] = [];
}
result[key].push(item);
return result;
}, {});
return Object.values(groups);
}
function getSharesSum(arr:any) {
return arr.map((subArray:any) => subArray.reduce((sum:any, num:any) => sum + num, 0));
}
const filteredData = filterPayloadByProperty(beneficiaryArray); // filter by propertyGUID
console.log('filteredData', filteredData);
const sharePercentages = filteredData.map((subArray:any) => subArray.map((obj:any) => obj.sharePercentage));
console.log('sharePercentages', sharePercentages);
const sums = getSharesSum(sharePercentages); // Sum of shares
console.log('sums', sums);
const validationTrue = sums.every((value:any) => value === 100); // Check if shares === 100
console.log('valid', validationTrue);
// if (validationTrue) {
// dispatch<any>(submitshareDetails(isSpouseSelected ? spouseShares : husbandShares));
// dispatch(setBySharesSubmitted(false));
// handleNext(6, 'by-specific', isCompleted);
// }
// New Method testing end
// Filter substitutes by property/account/company
const filteredSubArrays = filterDataByPropertyGUID(substituteListForTest);
console.log('filteredSubArrays', filteredSubArrays);
// Calculate total sum of shares of the substitutes
const substituteSharesTotalSum = calculateShareSum(filteredSubArrays);
console.log('substituteSharesTotalSum', substituteSharesTotalSum);
const substringsToRemove: string[] = ['undefined', 'null'];
// Remove undefined and null values from the list
const filteredObject = removeUndefinedAndNull(
substituteSharesTotalSum,
substringsToRemove,
);
console.log('filteredSubObject', filteredObject);
// Remove Zeros from the list
const removedZeros = removeZeroValues(filteredObject);
console.log('removedZeros', removedZeros);
// Condition to check substitute validation
const subShareValidation = Object.values(removedZeros).every(
(value) => value === 100,
);
// Substitute validation ends for all other WillTypes
// Case 8 test ---
// Normal Working case
// const benCountList: any[] = updatedBenList1.filter(
// (ben: any) => ben.subBeneficiaryList.length !== 0 && ben.sharePercentage !== 0,
// );
const hus = updatedBenList1.filter(
(ben: any) => ben.bookedForProfileGUID === profileGuid,
);
const spouse = updatedBenList1.filter(
(ben: any) => ben.bookedForProfileGUID === spouseGuid,
);
const benCountList: any[] = (isSpouseSelected ? spouse : hus).filter(
(ben: any) => ben.subBeneficiaryList?.length !== 0 && ben?.sharePercentage !== 0,
);
console.log('benCountList', benCountList);
// Buisness Owners Will Substitute Validation
// Check substitute validation in Buisness Owners Will
// const substituteValidationBOW = checkSubstituteValidationBOW(
// subSharesBow,
// // benCountList,
// removedZeros,
// );
// console.log('substituteValidationBOW', substituteValidationBOW);
// New test case --> Working but needs thorough testing
// Add combined guids of propertyGUID and profileGUID
const combinedArray = benCountList.map((obj: any) => ({
...obj,
combinedGUID: `${obj.propertyGUID}-${obj.profileGUID}`,
}));
console.log('combinedArray', combinedArray);
// Returns a list of shares and their corresponding combinedGUID's (propertyGUID + profileGUID)
const subSharesList = getSharePercentages(combinedArray, removedZeros);
console.log('subSharesArray', subSharesList);
// Validate the substitute shares with the beneficiary shares
const isSubstituteSharesValidBOW = validateShares(
subSharesList,
removedZeros,
);
console.log('isValid', isSubstituteSharesValidBOW);
// Check if combinedGUID === key of substituteArray
// const hasMatchingSharePercentage = checkSharePercentage(combinedArray, removedZeros);
// console.log('hasMatchingSharePercentage', hasMatchingSharePercentage);
// Check if substitute is present
const isSubPresent = beneficiaryList.some(
(el: any) => el.subBeneficiaryList.length !== 0,
);
const latestBenArray = updatedBen.filter((item1: any) => updatedBen.some(
(item2: any) => item1.profileGUID === item2.parentProfileGUID,
));
// console.log('latestBenArray', latestBenArray);
const filteredLatestBenList = latestBenArray.filter(
(e: any) => e.propertyGUID !== null,
);
console.log('filteredLatestBenList', filteredLatestBenList);
const removeZeroFromBen = filteredLatestBenList.filter(
(e: any) => e.sharePercentage !== 0,
);
console.log('removeZeroFromBen', removeZeroFromBen);
// Substitute validation ends ---------------------------------------------------------------
// Beneficiary Validation 2.0 (current version)
// Get the total count of beneficiaries with substitutes
// Check if the list of beneficiaries is updated for each iteration
const beneficiaryListForTest = checkBeneficiaryPresent(
beneficiaryArray,
beneficiaryList,
);
console.log('beneficiaryListForTest', beneficiaryListForTest);
// Filter beneficiaries by Property/Account/Company
const filteredBenArrays = filterDataByPropertyGUID(beneficiaryListForTest);
console.log('filteredBenArrays', filteredBenArrays);
// Calculate total sum of shares of the beneficiaries of each property/account/company
const benSharesTotalSum = calculateShareSum(filteredBenArrays);
console.log('benSharesTotalSum', benSharesTotalSum);
// Filter each beneficiary by property/account/company (Remove undefined and null values)
const filteredObj = removeUndefinedAndNullForBen(benSharesTotalSum);
console.log('filteredBenObj', filteredObj);
// Condition to check beneficiary validation (Total shares === 100)
const benShareValidation = Object.values(filteredObj).every(
(value) => value === 100,
);
console.log('benShareValidation', benShareValidation);
// Buisness Owners Will Beneficiary Validation
const sharesForbow = getSharesForBOW(filteredObj);
const sharesIterator = sharesForbow[Symbol.iterator]();
console.log('sharesIterator', sharesIterator);
// Beneficiary Validation for BOW
const benShareValidationBOW = Object.values(filteredObj).every(
(value) => value === sharesIterator.next().value,
);
console.log('benShareValidationBOW', benShareValidationBOW);
const getListType = () => {
if (willTypeID === 3) return propertyList;
if (willTypeID === 5) return accountList;
if (willTypeID === 4) return companyShareInformationList;
return null;
};
const list = getListType();
console.log('listType', list);
// Check property/account/company list length
const propertySharesObject = filterByPropertyList(
filteredObj,
list,
willTypeID,
);
console.log('propertySharesObject', propertySharesObject);
// propertyList || accountList || companyShareInformationList
// Check if all properties/accounts/companies are allocated shares
let checkLength: boolean;
if (
(accountList.length
|| propertyList.length
|| companyShareInformationList.length)
=== Object.keys(propertySharesObject).length
) {
// Add Company List when working on BOW
checkLength = true;
}
// Allow submission only if the shares have been allocated
if (updatedBen.length !== 0 && checkLength) {
// Property Will && Financial Assets Will
if (willTypeID === 5 || willTypeID === 3) {
// Financial Assets Will && Property Will
if (isSubPresent) {
// IF substitute is present check validation for beneficiaries & substitutes
const checkSubLength = benCountList.length === Object.keys(removedZeros).length;
// const checkSubLength = benCountList.length === subCount * accountList.length;
console.log('checkSubLength', checkSubLength);
// if (substituteSharesList.length !== 0) {
// Check beneficiary && substitute validation here
if (
benShareValidation
&& subShareValidation
&& checkSubLength
// && filteredLatestBenList.length !== 0
) {
// dispatch<any>(submitshareDetails(updatedBen));
dispatch<any>(
submitshareDetails(
isSpouseSelected ? spouseShares : husbandShares,
),
);
dispatch(setBySharesSubmitted(false));
handleNext(6, 'by-specific', isCompleted);
} else {
await dispatch(resetErrorState());
await dispatch(
setErrorInfo('Please enter shares for all substitutes'),
);
}
// }
} else if (!isSubPresent) {
// Working smoothly -- Beneficiary Validation working with single & multiple accounts/properties
// Beneficiary validation only
if (benShareValidation) {
dispatch<any>(
submitshareDetails(
isSpouseSelected ? spouseShares : husbandShares,
),
);
dispatch(clearUpdatedBenList());
dispatch(setBySharesSubmitted(false));
handleNext(6, 'by-specific', isCompleted);
} else {
await dispatch(resetErrorState());
await dispatch(
setErrorInfo('Please enter shares for all beneficiaries'),
);
}
}
} else {
// Buisness Owners Will
if (!isSubPresent) {
if (benShareValidationBOW) {
dispatch<any>(
submitshareDetails(
isSpouseSelected ? spouseShares : husbandShares,
),
);
dispatch(setBySharesSubmitted(false));
handleNext(6, 'by-specific', isCompleted);
} else {
await dispatch(resetErrorState());
await dispatch(
setErrorInfo('Please enter shares for all beneficiaries'),
);
}
} else if (isSubPresent) {
// Substitute is present
console.log('Substitute Present');
const checkSubLength = benCountList.length === Object.keys(removedZeros).length;
if (
benShareValidationBOW
// && substituteValidationBOW
&& isSubstituteSharesValidBOW
&& checkSubLength
// && filteredLatestBenList.length !== 0
) {
dispatch<any>(
submitshareDetails(
isSpouseSelected ? spouseShares : husbandShares,
),
);
dispatch(setBySharesSubmitted(false));
handleNext(6, 'by-specific', isCompleted);
} else {
await dispatch(resetErrorState());
await dispatch(
setErrorInfo('Please enter shares for all substitutes'),
);
}
}
}
} else {
await dispatch(resetErrorState());
await dispatch(setErrorInfo('Please assign the shares'));
}
// -----