Risk Assessment

Goal: This example illustrates how to leverage Eventing Service in the Banking and Financial domain. When a credit card transaction exceeds the user’s available credit limit, to indicate a high-risk transaction, an alert can be generated.

This example requires you to create four buckets: *flagged_transactions, users, metadata*and *transactions*buckets. For steps on how to create buckets, see Create a Bucket.

Implementation: Create a JavaScript Function that contains an OnUpdate handler. The handler listens to data-changes within a specified, transactions source bucket. When a document within the source bucket is created or mutated, the handler executes a user-defined routine. In this example, if the created or mutated document contains a high-risk transaction, a new document gets created in a specified, flagged_transactions bucket.

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:

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

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

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

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

    • For the Settings option, use the default values.

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

  3. After providing all the required information 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 following Function, and paste it in the placeholder code block of the high_risks_transactions dialog:

    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;
    }

    After pasting, the screen appears as displayed below:

    high risks transactions handler code

    The OnUpdate handler is triggered for every transaction. The handler checks if the transaction amount is less than the user’s available credit limit. When this condition is breached, then this transaction is flagged as a high-risk transaction. The Function high_risks_transactions then moves this transaction to a different bucket, flagged_transactions bucket. When the transaction is moved to a new bucket, the handler enriches the document with predefined comments and also provides a reason code*.* In the last part, the handler performs a currency validation step. If the transaction currency is other than the preconfigured home currency of the user, then the handler flags the transactions and moves it to a different bucket.

  5. Click Save.

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

    high risks transactions handler deploy

    The Function high_risks_transactionsis listed as a defined Function. Currently, it is listed as Undeployed and Paused.

  7. Click Deploy.

  8. From the Confirm Deploy Function dialog, click Deploy Function. This deploys the Function and displays the main Eventing screen. From this point, the defined Function is executed on all existing documents and on subsequent mutations.

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

  10. Click 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_transactions bucket.

  11. From the Couchbase Web Console > Query page, execute the below N1QL query:

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