Skip to content
This repository has been archived by the owner on Dec 10, 2022. It is now read-only.

Commit

Permalink
Drop Visit:Exit events, and only create SNS obj if we need to
Browse files Browse the repository at this point in the history
  • Loading branch information
tdmalone committed Jan 22, 2018
1 parent bcce459 commit 370441b
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions index.js
@@ -1,50 +1,59 @@
/**
* Queues custom geolocation events sent from devices such as iPhones.
* Designed to be invoked by the AWS API Gateway as a Lambda Proxy function.
*
* @author Tim Malone <tim@timmalone.id.au>
* @see ::runGeoEvent
* @see http://proximityevents.com/faq/index.html
*/

const AWS = require( 'aws-sdk' );
AWS.config.region = 'ap-southeast-2';

const sns = new AWS.SNS();

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

// Prepare the API response.
const response = {
isBase64Encoded: false,
statusCode: 200,
headers: {}
};

// Put together the event data we want.
const geoEventData = JSON.parse( event.body );
geoEventData.person = event.queryStringParameters.person;


// Drop Visit:Exit events, as they'll often supply an old address way too late.
if ( 'Visit:Exit' === geoEventData.event_type ) {
response.body = JSON.stringify({ message: 'Dropped.' });
callback( null, response );
return;
}

// Remove some data we don't need.
delete geoEventData.event_id;
delete geoEventData.trigger_id;

// Store an SNS message to process everything else we need, so we can return quickly.

const snsMessage = {
Message: JSON.stringify( geoEventData ),
TopicArn: process.env.SNS_QUEUE
};

const AWS = require( 'aws-sdk' );
AWS.config.region = 'ap-southeast-2';

const sns = new AWS.SNS();

sns.publish( snsMessage, ( error, data ) => {

const response = {
isBase64Encoded: false,
headers: {}
};


if ( error ) {
console.log( error );
response.statusCode = 500;
response.body = JSON.stringify({ error: error });
} else {
console.log( data );
response.statusCode = 200;
response.body = JSON.stringify({ message: 'Queued.' });
}

callback( null, response );

});

}); // Sns.publish.
}; // Exports.handler.

0 comments on commit 370441b

Please sign in to comment.