exports.handler = function()

in source/ws_realtime_transcribe_OnDDBInsert/index.js [25:108]


exports.handler = function (event, context, callback) {

  console.log(JSON.stringify(event));

  var scanParams = {
    TableName: process.env.TABLE_NAME,
    ProjectionExpression: "connectionId,contactID"
  };

  DDB.scan(scanParams, function (err, data) {
    if (err) {
      callback(null, {
        statusCode: 500,
        body: JSON.stringify(err)
      });
    } else {
      var apigwManagementApi = new AWS.ApiGatewayManagementApi({
        apiVersion: "2018-11-29",
        endpoint: process.env.DOMAIN_NAME + "/" + process.env.STAGE_NAME
      });

      var st = "";
      var bool ;
      var segId = "";
      //Assuming we will always get ONLY 1 record based on the trigger setup
      var contactID = event.Records[0].dynamodb.Keys.ContactId.S.toString();
      event.Records.forEach(function (element){
        /*try{
          st = st + element.dynamodb.OldImage.Transcript.S;
        }catch(e){
          
        }*/
        try{
          st = element.dynamodb.NewImage.Transcript.S;
          bool = element.dynamodb.NewImage.IsPartial.BOOL;
          segId = element.dynamodb.NewImage.SegmentId.S;
        }catch(e){
          
        }
      });
      var postParams = {
        Data: st + '@' + bool + '@' + segId
      };
      var count = 0;

      data.Items.forEach(function (element) {
        console.log(JSON.stringify(element));
        postParams.ConnectionId = element.connectionId.S;
        console.log('Sending data to connection id : ' + element.connectionId.S);
        try{
            console.log("contactID -> " + contactID + " element.contactID.S -> " + element.contactID.S);
            if(contactID == element.contactID.S){
              console.log("Match found, sending the data");
              apigwManagementApi.postToConnection(postParams, function (err) {
                if (err) {
                  // API Gateway returns a status of 410 GONE when the connection is no
                  // longer available. If this happens, we simply delete the identifier
                  // from our DynamoDB table.
                  if (err.statusCode === 410) {
                    console.log("Found stale connection, deleting " + postParams.connectionId);
                    DDB.deleteItem({ TableName: process.env.TABLE_NAME,
                                     Key: { connectionId: { S: postParams.connectionId } } });
                  } else {
                    console.log("Failed to post. Error: " + JSON.stringify(err));
                  }
                } else {
                  count++;
                }
              }); 
            }
        }catch(e){
          
        }
        

      });

      callback(null, {
        statusCode: 200,
        body: "Data send to " + count + " connection" + (count === 1 ? "" : "s")
      });
    }
  });
};