concept address in category node

appears as: ddress, n address, address, ddresses
Serverless Applications with Node.js

This is an excerpt from Manning's book Serverless Applications with Node.js.

Listing 2.8 Creating a pizza order handler
function createOrder(order) {    #1  
  if (!order || !order.pizzaId || !order.address)    #2  
    throw new Error('To order pizza please provide pizza type and address where pizza should be delivered')

  return {}    #3  
}

module.exports = createOrder    #4  

  #1   The createOrder handler function accepts the order object.
  #2   If the order object doesn’t contain a pizzaId or a customer address, throw an error.
  #3   Otherwise, return an empty object.
  #4   Export the handler function.

Currently, an order can only reach the in-progress status. Until you know the customer’s address, you can’t send the order to the delivery company. You’ll need to obtain the address and save it—but at the moment, if the message does not contain a pizza’s shortCode, your SMS chatbot will always reply with the greeting and pizza menu. You’ll need to override that behavior and somehow manage to handle the address input properly.

Luckily, there is a solution. You are already storing the sender’s phone number with the in-progress order, so you can first check if there is an in-progress order in your database with a matching phone number. If that’s the case and there is no saved address, you can then save the sent message as the address. Figure 10.1 shows the message parsing process.

Listing 10.7 The updated sms-bot.js file
'use strict'
const botBuilder = require('claudia-bot-builder')
const pizzas = require('./data/pizzas.json')
const pizzaMenu = require('./handlers/pizza-menu'),
  orderPizza = require('./handlers/order-pizza'),
  checkOrderProgress = require('./handlers/check-order-progress'),    #1  
  saveAddress = require('./handlers/save-address')    #2  

const api = botBuilder((message, originalApiRequest) => {

  let chosenPizza
  pizzas.forEach(pizza => {
    if (message.indexOf(pizza.shortCode) != -1) {
      chosenPizza = pizza
    }
  })

  if (chosenPizza) {
    return orderPizza(chosenPizza, message.sender)
  }

  return checkOrderProgress(message.sender)    #3  
    .then(orderInProgress => {
      if (orderInProgress) {
        return saveAddress(orderInProgress, message)    #4  
      } else {
        return pizzaMenu()    #5  
      }
    })
}, { platforms: ['twilio'] })

module.exports = api

  #1   Import the check-order-progress.js handler.
  #2   Import the save-address.js handler.
  #3   Check if there is an in-progress order for the current sender.
  #4   If there is an in-progress order, save the current order and save the current customer message as the address.
  #5   If there isn’t an in-progress order, return the pizza menu.

You’re now missing only the save-address.js handler. Create the save-address.js file in the handlers folder, open it, and write the code to update an order in your DynamoDB table using the provided order ID as its key. You should also update the address and change the status from in-progress to pending. The handler is shown in the following listing.