Useful Functions
addError(errorType, errorText)
function addError(errorType, errorText) {
errors.push({
'Type': errorType,
'Text': errorText
});
}
This is used to note any errors that occur when rates are run, the errors are stored in the output element "Errors".
addUIQuestion(questionsArray, questionCode, questionAnswer)
function addUIQuestion(questionsArray, questionCode, questionAnswer) {
var found = questionsArray.find(q => q['QuestionCode'] === questionCode);
if (found) {
found['QuestionAnswer'] = questionAnswer;
} else {
var question = {
'QuestionCode': questionCode,
'QuestionAnswer': questionAnswer
}
questionsArray.push(question);
}
}
This method is used to update a question answer (or add a new question) . You must provide:
questionArray: Usually risk.Questions
questionCode: As configured in Umbraco
questionAnswer: The answer you want to set it to.
After rates has finished running, the question will be updated with the data you've provided.
getQuestionAnswer(questionCode, defaultIfNull)
function getQuestionAnswer(questionCode, defaultIfNull) {
var otherReturn = defaultIfNull ? defaultIfNull : '';
var value = questions[questionCode] ? questions[questionCode] : otherReturn;
try {
value = JSON.parse(value);
} catch (err) {
// Failed, but no need to throw error
}
var printNewLine = false;
if (typeof value === 'object' || typeof value === 'array') {
printNewLine = true;
}
addDebug(value, debugVariables, questionCode, printNewLine);
return value;
}
This utility function gives you an easy way to get any question answer from the array returned by getQuestion(questions) - It will also automatically try to parse any question answer to a JSON object if necessary. As well as doing this, it will automatically add the question answer to the debug array for you. Note that you do not have to provide defaultIfNull.
getQuestion(questions)
function getQuestion(questions) {
for (var i = 0; i < questions.length; i++) {
var questionAnswer = "";
try {
questionAnswer = JSON.parse(questions[i].QuestionAnswer);
} catch (error) {
questionAnswer = questions[i].QuestionAnswer;
}
questionsToCheck[questions[i].QuestionCode] = (!questionAnswer || questionAnswer["_csharpnull"]) ? null : questionAnswer;
if (questions[i].SubQuestions && questions[i].SubQuestions.length > 0) {
getQuestion(questions[i].SubQuestions);
}
}
}
This is an essential function that is run during the initialise phase. You will never need to call it other than at this point. This function loops through the array of questions provided and saves the answers to an array called "questionsToCheck".
getLookup(table, filter, resultColumn, valueIfNotFound, title)
function getLookup(table, filter, resultColumn, valueIfNotFound, title) {
var value = null;
if (valueIfNotFound) {
value = singleRateLookup(table, filter, resultColumn, true, valueIfNotFound);
} else {
value = singleRateLookup(table, filter, resultColumn, false, null);
}
addDebug(value, debugLookups, title);
return value;
}
This is another utility function designed to get the lookup you need and automatically add the value to the debug array. See singleRateLookup(collection, filters, returnColumn, allowNotFound, valueIfNotFound) for more info.
singleRateLookup(collection, filters, returnColumn, allowNotFound, valueIfNotFound)
function singleRateLookup(collection, filters, returnColumn, allowNotFound, valueIfNotFound) {
//Fallback value for rate that can't be found
var returnValue = -1;
//If a different fallback value is provided
if (valueIfNotFound !== undefined) {
returnValue = valueIfNotFound;
}
//Setting up query
var queryAnd = [];
//Filters
if (filters && Array.isArray(filters) && filters.length > 0) {
for (var x = 0; x < filters.length; x++) {
var filter = filters[x];
queryAnd.push(filter);
}
}
var query = { };
if (queryAnd.length > 0) {
query['$and'] = queryAnd
}
var queryResults = db[collection].find(query).sort({
'Modified': -1
}).limit(1);
var results = queryResults.toArray();
if (results.length === 1) {
if (results[0].hasOwnProperty(returnColumn)) {
returnValue = results[0][returnColumn];
} else {
addError('Lookup', 'Failed to find "' + returnColumn + '" in "' + collection + '"');
}
} else if (results.length > 1) {
addError('Lookup', 'Lookup returned multiple rows for "' + returnColumn + '" from "' + collection + '"');
} else if (!allowNotFound) {
addError('Lookup', 'Failed to return "' + returnColumn + '" from "' + collection + '"');
}
return returnValue;
}
This function will search a database table with filters you provide. It will return one row from the table.
Arguments:
collection: The name of the database
filters: A JSON object representing the MongoDB query
resultColumn: A string matching the name of the value you'd like to get
allowNotFound: Boolean. If true and a value cannot be found in the database, then the returned value will be valueIfNotFound
valueIfNotFound: The value to be returned if allowNotFound = true and the lookup fails
addDebug(value, target, name, printNewLine)
function addDebug(value, target, name, printNewLine) {
if (debugOverrideFlag && value) {
var title = name ? name : value;
if (printNewLine) {
target.push(title + "...");
target.push(value);
} else {
target.push(title + ": " + value);
}
}
}
This utility function simplifies the process for adding debug lines.
value: The value that is being printed to debug
target: The debug array you'd like to print to
name: The "title" of the value, displayed before the value so you can identify it
printNewLine: Boolean. Do you need to print the value on a new line (useful if you're trying to print an object
addNormalDebug(name, value, printNewLine)
function addNormalDebug(name, value, printNewLine) {
addDebug(value, debugLog, name, printNewLine);
}
Used in the same way as addDebug(value, target, name, printNewLine), except the target is set to the debugLog array automatically, making it slightly easier to read, write and debug.
addRiskCode(riskCodesArray, riskCode, riskCodeShare)
function addRiskCode(riskCodesArray, riskCode, riskCodeShare) {
riskCodesArray.push({ 'RiskCode': riskCode, 'Share': riskCodeShare ? parseFloat(riskCodeShare) : 0 });
}
This function is used to add share percentages to the risk code array.
currencyRound(num)
function currencyRound(num) {
var num = num.toString().replace(/,/g, '');
num = parseFloat(num);
return Math.round(num * 100) / 100;
}
This utility function is used to round a number to 2 decimal places.
round(num, decimalPlaces)
function round(num, decimalPlaces) {
var num = num.toString().replace(/,/g, '');
num = parseFloat(num);
const factorOfTen = Math.pow(10, decimalPlaces);
return Math.round(num * factorOfTen) / factorOfTen;
}
This utility function is used to round a number to the amount of defined decimal places.
getTransaction()
function getTransaction() {
//Cancellation?
var isCancellation = false;
if (risk['Cancellation'] && risk['Cancellation'].hasOwnProperty('IsCancellation')) {
isCancellation = risk['Cancellation']['IsCancellation'];
}
//Renewal?
var isRenewal = false;
if (risk['Renewal'] && risk['Renewal'].hasOwnProperty('IsRenewal')) {
isRenewal = risk['Renewal']['IsRenewal'];
}
//MTA?
var isMTA = false;
if (risk['IsMta']) {
isMTA = risk['IsMta'];
}
//Set Transaction
var transaction = '';
if (isCancellation === true) {
transaction = 'CANX';
} else if (isMTA === true) {
transaction = 'MTA';
} else if (isRenewal === true) {
transaction = 'REN';
} else {
transaction = 'NB';
}
return transaction;
}
Even though this is not "officially" a part of the rates functions, it is very useful and is used across our system, so I have included it here as a reference.
Last updated