This repository has been archived by the owner on Dec 10, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Drop Visit:Exit events, and only create SNS obj if we need to
- Loading branch information
Showing
1 changed file
with
26 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |