Hi all!
I created a plugin for my app that will convert a price, say 15.65 to 16.99, based on predefined user settings. The user can choose 4 values to change the dollar position such as 2, 4, 6, 9 and append the cents value to a predefined value, .99 in this example. It’s works perfectly on the front end, but having issues with the server side script. Bubble says that it can’t be interpreted as javascript. I need it to run on the server for cases where a large list of product pricing needs to be updated.
function run_server(properties, context) {
// Extract input properties
var price = properties.price || “0”;
var option1 = properties.customer_option_1 || 0;
var option2 = properties.customer_option_2 || 0;
var option3 = properties.customer_option_3 || 0;
var option4 = properties.customer_option_4 || 0;
var cents = “99”;
var round = properties.round;
// Check if the input price is a float
var isFloat = price.includes(".");
// Function to modify the price if it’s a float
function modifyPriceIfFloat(price) {
var pattern = /[0-9]{1}[.]/;
var result = price.match(pattern);
var numberToBeReplaced = parseInt(result[0]);
var replacement = findSuitableReplacement(numberToBeReplaced, option1, option2, option3, option4, round);
var newPrice = price.replace(pattern, replacement + “.”);
newPrice = append99(newPrice);
return newPrice;
}
// Function to modify the price if it’s an integer
function modifyPriceIfInt(price) {
var numberToBeReplaced = parseInt(price[price.length - 1]);
var numberNotToBeChanged = price.slice(0, price.length - 1);
var replacement = findSuitableReplacement(numberToBeReplaced, option1, option2, option3, option4, round);
var newPrice = numberNotToBeChanged + replacement;
newPrice = append99(newPrice);
return newPrice;
}
// Find a suitable replacement from the available options
function findSuitableReplacement(numberToBeReplaced, option1, option2, option3, option4, round) {
// Sort the options in ascending order
var options = [option1, option2, option3, option4];
options.sort(function(a, b) {
return a - b;
});
if (round === "Up") {
// If rounding up, find the next highest option
var nextHighestOption = options.find(function(option) {
return option > numberToBeReplaced;
});
// If there is no higher option, use the highest option available
return nextHighestOption || options[options.length - 1];
} else if (round === "Down") {
// If rounding down, find the highest option before the number to be replaced
var prevHighestOption = options.filter(function(option) {
return option < numberToBeReplaced;
});
// If there are no options below the number, use the lowest option available
return prevHighestOption.length > 0 ? prevHighestOption[prevHighestOption.length - 1] : options[0];
} else {
// If no rounding preference is specified, find the option with the minimum difference
var diffs = options.map(function(option) {
return Math.abs(numberToBeReplaced - option);
});
var minDiff = Math.min.apply(Math, diffs);
var minDiffIndex = diffs.indexOf(minDiff);
return options[minDiffIndex];
}
}
// Append “.99” to the price
function append99(price) {
var price99Pattern = /[.][0-9]*/;
var isFloat = price99Pattern.test(price);
return isFloat ? price.replace(price99Pattern, “.” + cents) : price + “.” + cents;
}
// Return the modified price based on whether it’s a float or an integer
return isFloat ? modifyPriceIfFloat(price) : modifyPriceIfInt(price);
}