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".

circle-info

If you set the errorType to "UI", it will be picked up by the UI and presented to the user

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)

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)

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)

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)

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)

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)

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)

This function is used to add share percentages to the risk code array.

currencyRound(num)

This utility function is used to round a number to 2 decimal places.

round(num, decimalPlaces)

This utility function is used to round a number to the amount of defined decimal places.

getTransaction()

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