Last active
October 11, 2019 17:44
-
-
Save dabit3/b0eb1d55a859518d16f971a5e71b8abe to your computer and use it in GitHub Desktop.
Basic example of Lambda function that accepts both GraphQL & API gateway requests
This file contains hidden or 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
exports.handler = function (event, context) { //eslint-disable-line | |
const taxTable = { | |
NY: 8.49, CA: 8.56, OK: 8.92, FL: 7.05, | |
IL: 8.74, PA: 6.34, WA: 9.17, TX: 8.19 | |
} | |
let price | |
let taxRate | |
if (event.arguments) { | |
// graphql request | |
price = event.arguments.price | |
taxRate = taxTable[event.arguments.location] | |
} else { | |
price = event.price | |
taxRate = taxTable[event.location] | |
} | |
let calculatedTax = (taxRate / 100) * price | |
calculatedTax = Math.round( ( calculatedTax + Number.EPSILON ) * 100 ) / 100 | |
const finalPrice = (calculatedTax + price).toFixed(2) | |
const response = { | |
calculatedTax, | |
finalPrice | |
} | |
context.done(null, response); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment