Risk Assessment

This example illustrates how to leverage the Eventing service in the Banking and Financial domain by performing a sample risk assessment for a credit card transaction.

Need for Risk Assessment

To mitigate risks, transaction assessment cannot be performed as a batch job, but needs to be performed in near real-time. After every transaction, the transaction amount needs to be validated against the total available credit limit. An alert, as an early warning, should be generated to mitigate risks during subsequent transactions.

Developing your Handler Code

Create a JavaScript function that contains an OnUpdate handler. This handler listens for data-changes within a source bucket. The OnUpdate handler executes a user-defined routine for every credit card transaction.

In this example, for every transaction, the handler code checks if the transaction amount is less than the user’s available credit limit. If this condition is breached, then the transaction is flagged as a high-risk transaction. The handler then moves this transaction to a different bucket, the flagged_transactions bucket. After moving this transaction to a new bucket, the handler enriches the document with predefined comments and appends a reason code. In the last part, the handler performs a currency validation step. On a mismatch, a currency mismatch alert is generated.

function OnUpdate(doc, meta) {
    try
    {
        //log('txn id:', meta.id, '; user_id:', doc.user_id , ', doc.amount:', doc.amount);
        var this_user = getUser(doc.user_id);
        if (this_user)
        {
            if(this_user['creditlimit'] < doc.amount)
            {
                log('Txn['+String(meta.id)+']*****High Risk Transaction as Txn Amount:'+ String(doc.amount)+' exceeds Credit Limit:',this_user['creditlimit']);
                doc["comments"] = "High Risk Transaction as Txn Amount exceeds Credit Limit " +String(this_user['creditlimit']);
                doc["reason_code"] = "X-CREDIT";
                high_risk[meta.id] = doc;
                return;
            }
            else
            {
                if(doc.txn_currency != this_user['currency'])
                {
                    log('Txn['+ String(meta.id) +']*****High Risk Transaction - Currency Mismatch:'+ this_user['currency']);
                    doc["comments"] = "High Risk Transaction - Currency Mismatch:" + this_user['currency'];
                    doc["reason_code"] = "XE-MISMATCH";
                    high_risk[meta.id] = doc;
                    return;
                }
            }
            //log('Acceptable Transaction:',doc.amount, ' for Credit Limit:', this_user['creditlimit']);
        }
        else
        {
            log('Txn['+ String(meta.id) + "] User Does not Exist:" + String(doc.user_id) );
        }
     }
    catch (e)
    {
        log('Error OnUpdate :', String(meta.id), e);
    }
}

function OnDelete(meta) {
    log('Document OnDelete:', meta.id);
}

function getUser(userId)
{
    try
    {
        if(userId != null)
        {
            return user[userId];
        }
    }
    catch (e)
    {
        log('Error getUser :', userId,'; Exception:', e);
    }
    return null;
}

Prerequisites

Before you begin, you’ll need to ensure that three buckets, metadata, flagged_transactions and transactions buckets, are created. See Creating Buckets.

Procedure

Proceed as follows:

  1. From the Couchbase Web Console > Eventing page, click ADD FUNCTION,to add a new Function. The ADD FUNCTION dialog appears.

  2. In the ADD FUNCTION dialog, for individual Function elements provide the below information:

    1. For the Source Bucket drop-down, select the transactions bucket option that was created for this purpose.

    2. For the Metadata Bucket drop-down, select metadata bucket option that was created for this purpose.

    3. Enter high_risks_transactions as the name of the Function you are creating in the Function Name text-box.

    4. Enter Functions that computes risky transaction and flags them, in the Description text-box.

    5. For the Settings option, use the default values.

    6. For the Bindings option, add two bindings. For the first binding specify users as the name of the bucket, and specify user as its associated value. For the second binding, specify flagged_transactions as the name of the bucket, and specify high_risk as its associated value.

  3. In the ADD FUNCTION dialog, click Next: Add Code. The high_risks_transactions dialog appears. The high_risks_transactions dialog initially contains a placeholder code block. You will substitute your actual high_risks_transactions code in this block.

    add functions code exp2
  4. Copy the sample Function handler code, and paste it in the placeholder code block of the high_risks_transactions dialog.

  5. After pasting, the screen appears as displayed below:

    high risks transactions handler code
  6. Click Save.

  7. To return to the Eventing screen, click Eventing.

    high risks transactions handler deploy
  8. The Function high_risks_transactionsis listed as a defined Function.

  9. Click Deploy.

  10. From the Confirm Deploy Function dialog, from the Feed boundary drop-down, select Everything and click Deploy Function.
    From this point, the defined Function is executed on all existing documents and on subsequent mutations.

  11. To check results of the deployed Function, after a sufficient time elapse, from the Couchbase Web Console> Eventing page, click Buckets.

  12. Click the flagged_transactions bucket. All documents available in this bucket are transactions that are flagged as high-risk transactions.

    buckets

    This indicates that transactions which were flagged as high risk gets moved to the flagged_transactionsbucket.

  13. From the Couchbase Web Console > Query page, execute the below N1QL query. The query result displays currency mismatch and high risk transaction amount that was flagged.

    SELECT reason_code, COUNT(1), num_txns, SUM(amount) amount
    FROM `flagged_transactions`
    GROUP BY reason_code;
    N1QL Query

To summarize, this example illustrated how easy it was to track a high-risk transaction. The handler code was configured in accordance with your business logic to identify and flag risky transactions.

In case your business logic (workflow to assess a high-risk transaction) evolves with time, then you need to modify only the Function handler code. With changes only to the Function handler code, your efforts in application configuration and infrastructure are significantly reduced.