Testing out event stream

node v8.17.0
version: 0.0.2
endpointsharetweet
const AWS = require("aws-sdk") const axios = require("axios") const Promise = require("bluebird") const { unmarshall } = AWS.DynamoDB.Converter const STATES = { CHECKING_CUSTOMER_CARD_AND_CART: 'checkingCustomerCardAndCart', AWAITING_FULFILLMENT: 'awaitingFulfillment', AWAITING_PICKUP: 'awaitingPickup', AWAITING_TIP: 'awaitingTip', COMPLETED: 'completed', REFUNDED: 'refunded', DECLINED: 'declined', CANCELLED: 'cancelled', BAD_OR_MISSING_CHARGE_CARD: 'badOrMissingChargeCard', NONEXISTENT_ITEM_IN_CART: 'nonexistentItemInCart', } const usingVersions = (previousVersion, nextVersion) => { const previousState = previousVersion.state const nextState = nextVersion.state return function(targetState) { return nextState === targetState && previousState !== targetState } } const stateChangedTo = (previousVersion, nextVersion) => { const previousState = previousVersion.state const nextState = nextVersion.state if (nextState !== previousState) { return nextState } } const actionsByEventName = { 'INSERT': record => { const newVersion = unmarshall(record.dynamodb.NewImage) return Event.create('Order', newVersion.id, 'ORDER_CREATED', { order: newVersion }) }, 'MODIFY': record => { const newVersion = unmarshall(record.dynamodb.NewImage) const oldVersion = unmarshall(record.dynamodb.OldImage) // Customer tipped AWAITING_TIP -> COMPLETED const newState = stateChangedTo(oldVersion, newVersion) if (newState) { console.log(`changing order ${newVersion.id} to ${newState}`) } return Promise.resolve() }, 'REMOVE': Promise.resolve } const callback = (err, resp) => { if (err) { return console.error(err) } console.info(resp) }
This checks if order records change, if they change, what's the new state.
const response = await axios.get('https://gist.githubusercontent.com/joemocha/4bf44622a4a0a363611ee4e7d120c6b7/raw/830041fe50c8f0889ec3d32db3486273ec56345a/event.orderStreamAwaitingTip.json') const event = response.data return Promise.map(event.Records, record => actionsByEventName[record.eventName](record)). then(() => { callback(null, `Successfully processed ${event.Records.length} records.`); }).catch(e => { if (/Execution Already Exists/.test(e.message)) { return callback(null, 'Execution Already Exists') } console.error(e) callback(e.message); })
Loading…

no comments

    sign in to comment