Overview

The summary tag skill is written to allow Andi to display a message PrecisionLender. This skill template will help you write summary tag skills in PrecisionLender. You can modify the code in the template or use it as a reference when writing your skill.  This article will walk through the summary tag skill.

 

In this Article

 

Run

This is main execution of the skill which is called after the Andi skills platforms determines if it should run.

export async function run(skillContext: ISkillContext) : Promise<ISkillActivity> {

Uses the getSkillRunState power to get the loanAmount set in the shouldIRun.

const loanAmount: number = await skillContext.getSkillRunState("loanAmount");

Builds and sends a summary tag with information for the user.

const summaryTag: SummaryTag = {
        text: skillContext.skillConfig.summaryTagMessage,
        tagType: FieldTagTypes.Info,
        key: "uniqueTemplateKey",

Says that Andi should only load this summary tag if the amount hasn't changed.

 loadWhen: [{
            query: { name: "getRawAmount", args: [] },
            comparator: "==",
            value: loanAmount,
            type: "number"
        }]
    };
    const url = skillContext.skillConfig.url;
    if (url) {
        summaryTag.onClick = [{
            name: "openNewTab",
            args: [url]
        }];
    }
    return TagPowers.sendTags([summaryTag]);
}

 

In total, the run code is as follows:

import { TagPowers, SummaryTag } from "@andi/powers";
import { FieldTagTypes, ISkillContext, ISkillActivity } from "andiskills";

// this is main execution of the skill which is called after the andi
// skills platforms determines if it should run
export async function run(skillContext: ISkillContext) : Promise<ISkillActivity> {
    const loanAmount: number = await skillContext.getSkillRunState("loanAmount");

    const summaryTag: SummaryTag = {
        text: skillContext.skillConfig.summaryTagMessage,
        tagType: FieldTagTypes.Info,
        key: "uniqueTemplateKey",
        //Andi should only load this summary tag if the amount hasn't changed 
        loadWhen: [{
            query: { name: "getRawAmount", args: [] },
            comparator: "==",
            value: loanAmount,
            type: "number"
        }]
    };

    const url = skillContext.skillConfig.url;
    if (url) {
        summaryTag.onClick = [{
            name: "openNewTab",
            args: [url]
        }];
    }

    return TagPowers.sendTags([summaryTag]);
}

 

Should I Run

The shouldIRun is used by the Andi skills platform to determine if the given skill's current context needs to be executed.

export async function shouldIRun(skillContext: ISkillContext): Promise<ShouldIRunResponseType> {

Uses the isLoanAccountChangeEvent to determine if the event type coming into the skill is a Loan Account Change Event.

 const isLoanAccountChangeEvent: boolean = skillContext.powers.precisionLender.opportunity.isLoanAccountChangeEvent();
    if (!isLoanAccountChangeEvent) {
        return skillContext.powers.andi.shouldIRun.shouldIRunFalse();
    }
    validateConfiguration(skillContext);

Uses the getApplicationEvent power to get event data.

const event = skillContext.powers.andi.event.getApplicationEvent(skillContext);
    const data = event.applicationEventData;

Iterates through the commercial loan accounts attached to the event data to find the one relevant to this skill.

const cla: CommercialLoanAccount = data.engineModel.commercialLoanAccounts.find((cla) => {
        return cla.id === data.contextId;
    });

Tells Andi not send the field tag if the loan amount is less than the threshold.

if (cla.amount < skillContext.skillConfig.loanAmountThreshold) {
        return skillContext.powers.andi.shouldIRun.shouldIRunFalse();
    }

Uses the setSkillRunState power to add a new key/value pair to the skill run state that can be accessed later.

 await skillContext.setSkillRunState("loanAmount", cla.amount);

Tells Andi to run the skill by returning the value from shouldIRunTrue power.

return skillContext.powers.andi.shouldIRun.shouldIRunTrue();
}
function validateConfiguration(skillContext: ISkillContext) {
    if (!skillContext.skillConfig) {
        throw new Error("Skill has not been configured.");
    }
    if (!skillContext.skillConfig.loanAmountThreshold) {
        throw new Error("Loan Amount Threshold is a required configuration value.");
    }
    if (!skillContext.skillConfig.fieldTagMessage) {
        throw new Error("Field Tag Message is a required configuration value.");
    }
}

 

In total, the shouldIRun code is as follows:

import { CommercialLoanAccount } from "andiexternal";
import { ISkillContext, ShouldIRunResponseType } from "andiskills";

// shouldIRun is used by the andi skills platform to determine if the given
// skill's current context needs to be executed. 
export async function shouldIRun(skillContext: ISkillContext): Promise<ShouldIRunResponseType> {
    const isLoanAccountChangeEvent: boolean = skillContext.powers.precisionLender.opportunity.isLoanAccountChangeEvent();

    if (!isLoanAccountChangeEvent) {
        return skillContext.powers.andi.shouldIRun.shouldIRunFalse();
    }

    validateConfiguration(skillContext);

    const event = skillContext.powers.andi.event.getApplicationEvent(skillContext);
    const data = event.applicationEventData;

    const cla: CommercialLoanAccount = data.engineModel.commercialLoanAccounts.find((cla) => {
        return cla.id === data.contextId;
    });
    
    // Do not send the summary tag if the loan amount is less than the threshold
    if (cla.amount < skillContext.skillConfig.loanAmountThreshold) {
        return skillContext.powers.andi.shouldIRun.shouldIRunFalse();
    }

    await skillContext.setSkillRunState("loanAmount", cla.amount);
    
    return skillContext.powers.andi.shouldIRun.shouldIRunTrue();
}

function validateConfiguration(skillContext: ISkillContext) {
    if (!skillContext.skillConfig) {
        throw new Error("Skill has not been configured.");
    }

    if (!skillContext.skillConfig.loanAmountThreshold) {
        throw new Error("Loan Amount Threshold is a required configuration value.");
    }

    if (!skillContext.skillConfig.summaryTagMessage) {
        throw new Error("Summary Tag Message is a required configuration value.");
    }
}
Was this article helpful?
0 out of 0 found this helpful