hey velo-ninja;
Thanks for wading through that and giving me the pointers (told you I was a noob)
I’ve gone away and looking up some basics around promises and I’ve cracked it.
I do take the point about tiding it all up and commenting on it better.
Anyway here is the working code.
import wixData from 'wix-data';
$w.onReady(function () {
$w("#myRepeater").onItemReady( ($item, itemData, index) => {
$item("#nameData").text = itemData.title;
$item("#balanceData").text = itemData.borrowingAmount.toString();
$item("#remoDateData").text = itemData.remortgageDate.toLocaleDateString();
$item("#ercData").text = itemData.currentErc.toString();
$item('#outcomeData').text = itemData.newData.toString();
} )
} )
function getCustomer(){
return new Promise((resolve, reject) => {
wixData.query("collection1")
.ascending("remortgageDate")
.eq("type","rate checker")
.find()
.then((results) => {
//create array with result from search of collection for rate checks
const finalData = results.items;
resolve(finalData)
});
})
}
function getRates(ltv) {
return new Promise((resolve, reject) => {
let items;
wixData.query("collection2")
.ge("ltv", ltv)
.ascending("ltv", "fees")
.find()
.then((results) => {
if(results.items.length > 0) {
items = results.items; //all items in the query
} else {
console.log("NOTHING MATCHES THIS SEARCH")
}
resolve(items);
});
});
}
let array = [];
getCustomer()
.then(customers => {
let count = customers.length
let pass = 0;
customers.forEach(customer => {
getRates(customer.ltvPc)
.then(rates => {
let date1 = new Date();
let currentTimeLeft = customer.remortgageDate.getTime() - date1.getTime();
let currentMonthsLeft = Math.round(currentTimeLeft / 2629746000);
const lowestPrice = Math.round(totalCostLoop(rates, currentMonthsLeft, customer.borrowingAmount, customer.currentRepaymentMethod, customer.term));
passResult(customer.currentPayment, currentMonthsLeft, lowestPrice, customer.currentErc)
.then(outcome => {
customer.newData = outcome;
array.push(customer)
pass += 1;
if (pass === count) {
$w("#remoCheckRepeater").data = array;
}
})
})
});
})
//function to work out which out the all the rates in the collection is the lowest cost overall
function totalCostLoop(item, mths, bal, mthd, term) {
let lowestTotalCost=0;
//check if the calculation should be based in C&I or IO
if (mthd !== "repayment") {
//loop over all the products and work out the overall cost of each using IO repayment method
for (let i = 0; i < item.length; i++) {
let monthlyCost = 0;
monthlyCost = item[i].interestRate / 100 / 12 * bal;
let totalCost = monthlyCost * mths + item[i].fees - item[i].cashBack;
//capture which is the lowest cost deal
if (i === 0) {
lowestTotalCost = totalCost;
} else if (i > 0 && totalCost < lowestTotalCost){
lowestTotalCost = totalCost;
}
}
} else {
for (let i = 0; i < item.length; i++) {
let monthlyCost = 0;
let monthlyRate = item[i].interestRate / 100 / 12;
//calculate the monthly cost of the mortgage using C&I repayment method
monthlyCost = (bal * monthlyRate * (Math.pow(1 + monthlyRate, (term*12))) / (Math.pow(1 + monthlyRate, (term*12)) - 1));
let totalCost = monthlyCost * mths + item[i].fees - item[i].cashBack;
//console.log('the total monthly cost is £'+monthlyCost+' and the total cost is £'+totalCost)
if (i === 0) {
lowestTotalCost = totalCost;
} else if (i > 0 && totalCost < lowestTotalCost){
lowestTotalCost = totalCost;
}
}
}
return lowestTotalCost;
}
function passResult(currentPayment, monthsLeft, remoCost, erc) {
//work out the cost of the current mortgage over the months left on the deal
let currentTotalCost = currentPayment * monthsLeft;
//compare the difference in the current total cost vs the remo total cost over the period
let costDifference = currentTotalCost - remoCost;
if (costDifference > erc) {
//we can save you money over your current deal
return new Promise ((resolve, reject) =>
resolve("yes we can save you money"));
} else if (costDifference < erc && monthsLeft <= 6) {
// the new deal will cost more but you are withing the next product window
return new Promise ((resolve, reject) =>
resolve("no we cant save you money but you're within 6 months"));
} else {
return new Promise ((resolve, reject) =>
resolve("no we cant save you money"));
}
}