Type Script


Type Script 1

handler.ts

import { Context, Handler, Callback } from "aws-lambda";
import { DocumentClient } from "aws-sdk/clients/dynamodb";
import { EventRecord, Response, ConnectEvent } from "./types";
const ddb = new DocumentClient({ region: process.env.REGION });
const tableName: string = process.env["LE_TABLE"] || "";

export const handler: Handler = async (
    event: ConnectEvent,
    ctx: Context,
    cb: Callback
) => {
    if (!tableName) {
        console.error("MISSING TABLE ENVIRONMENT VARIABLE");
        return { message: "MISSING TABLE ENVIRONMENT VARIABLE" };
    }

    if (!event.Details.Parameters.policyNumber) {
        event.Details.Parameters.policyNumber = "not found";
    }

    try {

        return await putItem(event.Details.Parameters);
        
    } catch (err) {
            console.log(err);
            return { message: "PUT RECORD ERROR" };
    }
};
    
    
const putItem = async (event: EventRecord): Promise => {
    if (!event.agentId) {
        return { message: "No Agent ID" }
    }
    
    const thirtyDays = 60 * 60 * 24 * 30;
    event.TTL = Math.round(Date.now() / 1000) + thirtyDays

    const putParams: DocumentClient.PutItemInput = {
        TableName: tableName,
        Item: event
    }
    try {
        await ddb.put(putParams).promise();
        return { message: "Success" }
    } catch (err) {
        console.error(err);
        return { message: "Put failure" }
    }
}

types.ts

export interface ConnectEvent {
    Details: {
        ContactData: {
            Attributes: any,
            ContactId: string,
            CustomerEndpoint: {
                Address: string,
                Type: string
            },
            InitialContactId: string,
            InitiationMethod: string,
            InstanceARN: string
        },
        Parameters: EventRecord
    }
}

export interface EventRecord {
    phoneNumber: string,
    policyNumber?: string,
    agentId: string,
    TTL?: number
}

export interface Response {
    message: string
}


Type Script 2

handler.ts

import { Context, Handler, Callback } from "aws-lambda";
import { DocumentClient } from "aws-sdk/clients/dynamodb";
import {
    EventRecord,
    CheckEvent,
    Response,
    getParams,
    ConnectEvent
} from "./types";
const ddb = new DocumentClient({ region: process.env.REGION });
const tableName: string = process.env["LE_TABLE"] || "";

export const handler: Handler = async (
    event: ConnectEvent,
    ctx: Context,
    cb: Callback
) => {
    let response: Response;
    if (!tableName) {
        console.error("MISSING TABLE ENVIRONMENT VARIABLE");
        return {
            message: "MISSING TABLE ENVIRONMENT VARIABLE",
            assignedAgent: "not found"
        };
    }
    try {
        if (event.Details.Parameters.phoneNumber) {
            response = await getRecord({
                key: "phoneNumber",
                value: event.Details.Parameters.phoneNumber
            });
            if (
                response.assignedAgent &&
                response.assignedAgent === "not found" &&
                event.Details.Parameters.policyNumber
            ) {
                response = await queryIndex({
                    key: "policyNumber",
                    value: event.Details.Parameters.policyNumber,
                    index: "policyNumber"
                });
            }
            } else if (event.Details.Parameters.policyNumber) {
                response = await queryIndex({
                    key: "policyNumber",
                    value: event.Details.Parameters.policyNumber,
                    index: "policyNumber"
                });
            } else {
                return {
                    message: "Invalid input",
                    assignedAgent: "not found"
                };
            }
        } catch (err) {
            console.log(err);
            return {
                message: "Get record error",
                assignedAgent: "not found"
            };
        }
        return response;
    };
    
const queryIndex = async (event: getParams): Promise => {
    const getParams: DocumentClient.QueryInput = {
        TableName: tableName,
        IndexName: event.index,
        KeyConditionExpression: `${event.key} = :ct`,
        ExpressionAttributeValues: {
            ":ct": event.value
        }
    };
    
    try {
        let results = await ddb.query(getParams).promise();
        if (results.Items && results.Items.length) {
            return {
                assignedAgent: results.Items[0].agentId
            };
        } else {
            return {
                assignedAgent: "not found"
            };
        }
    } catch (err) {
        console.error(err);
        return {
            assignedAgent: "not found"
        };
    }
};
    
const getRecord = async (event: getParams): Promise => {
    const getParams: DocumentClient.GetItemInput = {
        TableName: tableName,
        Key: {}
    };
    getParams.Key[event.key] = event.value;
    
    try {
        let results = await ddb.get(getParams).promise();
        if (results.Item) {
            return {
                assignedAgent: results.Item.agentId
            };
        } else {
            return {
                assignedAgent: "not found"
            };
        }
    } catch (err) {
        console.error(err);
        return {
            assignedAgent: "not found"
        };
    }
};

types.ts

export interface ConnectEvent {
    Details: {
        ContactData: {
            Attributes: any,
            ContactId: string,
            CustomerEndpoint: {
                Address: string,
                Type: string
            },
            InitialContactId: string,
            InitiationMethod: string,
            InstanceARN: string
        },
        Parameters: CheckEvent
    }
}

export interface EventRecord {
    phoneNumber: string,
    policyNumber?: string,
    agentId: string,
    TTL: number
}

export interface CheckEvent {
    phoneNumber?: string,
    policyNumber?: string
}

export interface Response {
    assignedAgent?: string
}

export interface getParams {
    key: string,
    value: string,
    index?: string
}