Skip to content

Instantly share code, notes, and snippets.

@chrisprice
Created April 19, 2016 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisprice/202b8336da639052e508c4472c1f2dba to your computer and use it in GitHub Desktop.
Save chrisprice/202b8336da639052e508c4472c1f2dba to your computer and use it in GitHub Desktop.
Mondo transaction webhook to Pebble Timeline integration
var validate = require('@smallwins/validate')
var lambda = require('@smallwins/lambda')
var Timeline = require('pebble-api').Timeline
function valid (event, callback) {
var schema = {
'query': { required: true, type: Object },
'query.token': { required: true, type: String },
'body': { required: true, type: Object },
'body.data': { required: true, type: Object },
'body.data.amount': { required: true, type: Number },
'body.data.created': { required: true, type: String },
'body.data.currency': { required: true, type: String },
'body.data.description': { required: true, type: String },
'body.data.id': { required: true, type: String },
'body.data.notes': { required: true, type: String }
}
validate(event, schema, callback)
}
var backgroundColorMap = {
'transport': 'islamicgreen',
'groceries': 'yellow',
'eating_out': 'red',
'cash': 'mintgreen',
'bills': 'pictonblue',
'entertainment': 'chromeyellow',
'shopping': 'melon',
'holiday': 'vividviolet',
'general': 'lightgray',
'expenses': 'brass'
};
function fn (event, callback) {
var timeline = new Timeline()
var pin = new Timeline.Pin({
id: event.body.data.id,
time: new Date(event.body.data.created),
layout: {
type: Timeline.Pin.LayoutType.GENERIC_PIN,
tinyIcon: Timeline.Pin.Icon.NOTIFICATION_FLAG,
backgroundColor: backgroundColorMap[event.body.data.category || 'general'],
title: event.body.data.description,
subtitle: event.body.data.currency + ' ' + (-event.body.data.amount / 100).toFixed(2),
body: event.body.data.notes || '<no notes>'
}
})
timeline.sendUserPin(event.query.token, pin, function (err) {
callback(err, event)
})
}
exports.handler = lambda(valid, fn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment