Created
July 2, 2020 13:27
-
-
Save tyrion70/8cf086dc9e966d640b86a355492ba15a to your computer and use it in GitHub Desktop.
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
| const { Requester, Validator } = require('@chainlink/external-adapter') | |
| const { box, randomBytes } = require('tweetnacl') | |
| const { | |
| decodeUTF8, | |
| encodeUTF8, | |
| encodeBase64, | |
| decodeBase64 | |
| } = require('tweetnacl-util') | |
| const customError = (data) => { | |
| return data.status === 'ERROR' | |
| } | |
| const customParams = { | |
| base: false, | |
| quote:false, | |
| endpoint: false, | |
| amount: false, | |
| precision: false, | |
| deliveryDetails: ['deliveryDetails'] | |
| } | |
| const createRequest = async (input, callback) => { | |
| const validator = new Validator(callback, input, customParams) | |
| const jobRunID = validator.validated.id | |
| const address = validator.validated.data.address | |
| const apikey = process.env.API_KEY | |
| // node priv | |
| const secretOrSharedKey = decodeBase64('cnFtm/QWY3l+Uzz7Nx0ljtVwLpDH7iNOkX1KAAmTaL4=') | |
| // app public | |
| const key = decodeBase64('INdW9zSl/MgfbPcUiUkPP/Baab7hO7mWS5+XeP2I2D8=') | |
| const messageWithNonce = input.data.deliveryDetails | |
| const messageWithNonceAsUint8Array = decodeBase64(messageWithNonce) | |
| const nonce = messageWithNonceAsUint8Array.slice(0, box.nonceLength) | |
| const message = messageWithNonceAsUint8Array.slice( | |
| box.nonceLength, | |
| messageWithNonce.length | |
| ) | |
| const decrypted = key | |
| ? box.open(message, nonce, key, secretOrSharedKey) | |
| : box.open.after(message, nonce, secretOrSharedKey); | |
| if (!decrypted) { | |
| callback(422, Requester.success(jobRunID, { "data": { "result": 0, "error": "failed to unbox" }})) | |
| } | |
| const payload = JSON.parse(encodeUTF8(decrypted)) | |
| console.log(payload) | |
| console.log(payload.address) | |
| var pizzapi = require('pizzapi'); | |
| const theCustomer = new pizzapi.Customer( | |
| { | |
| firstName: payload.firstname, | |
| lastName: payload.lastname, | |
| address: payload.address, | |
| email: payload.email | |
| } | |
| ); | |
| await pizzapi.Util.findNearbyStores( | |
| payload.address, | |
| 'Delivery', | |
| async function(storeData){ | |
| if (storeData.success == false) { | |
| callback(422, Requester.success(jobRunID, { "data": { "result": 0, "error": "can't find store" }})) | |
| } | |
| store = storeData.result.Stores[0] | |
| mystoreID = storeData.result.Stores[0].StoreID | |
| mystoreDetails = storeData.result.Stores[0].AddressDescription | |
| const theCustomer = await new pizzapi.Customer( | |
| { | |
| firstName: payload.firstname, | |
| lastName: payload.lastname, | |
| address: payload.address, | |
| email: payload.email | |
| } | |
| ); | |
| console.log(0) | |
| var myStore = await new pizzapi.Store({ID: mystoreID}); | |
| console.log(myStore) | |
| console.log(1) | |
| var order = await new pizzapi.Order( | |
| { | |
| customer: theCustomer, | |
| storeID: myStore.ID, | |
| deliveryMethod: 'Delivery' | |
| } | |
| ); | |
| console.log(myStore) | |
| console.log(2) | |
| await order.addItem( | |
| new pizzapi.Item( | |
| { | |
| code: '14SCREEN', | |
| options: {}, | |
| quantity: 1 | |
| } | |
| ) | |
| ); | |
| console.log(3) | |
| order.validate( | |
| await function(result) { | |
| console.log("We did it!"); | |
| } | |
| ); | |
| console.log(4) | |
| order.place( | |
| await function(result) { | |
| console.log("Order placed!"); | |
| } | |
| ); | |
| callback(200, Requester.success(jobRunID, { "data": { "result": 1, "storeDetails": mystoreDetails }})) | |
| console.log(5) | |
| } | |
| ); | |
| } | |
| module.exports.createRequest = createRequest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment