def place_order()

in dialogflow-prebuilt-agents/cloud-functions/retail_assistant/main.py [0:0]


def place_order():
  """Place the order for the shopping cart items"""
  app.logger.warning("REACHED /PLACE_ORDER")
  request_json = request.get_json(silent=True)
  app.logger.warning("REQUEST: %s", request_json)

  products = request_json.get("products", [])
  app.logger.warning("SHOPPING CART PRODUCTS: %s", products)

  if not products:
    app.logger.warning("EMPTY CART: %s", products)
    return flask.jsonify(
        {"order_status": "not_placed", "reason": "Shopping cart is empty!"}
    )

  order_id = uuid.uuid4().hex[:8]
  order_created_on = datetime.utcnow().isoformat() + "Z"

  # Eventually this response should be returned from an API after successful order placement
  order_data = {
      "order_id": order_id,
      "order_status": "confirmed",
      "order_created_on": order_created_on,
      "products": products,
  }

  try:
    # store the order data in firestore
    db.collection("orders").document(order_id).set(order_data)
    app.logger.warning("ORDER PLACED: %s", order_data)
    return flask.jsonify(order_data)
  except Exception as e:
    app.logger.warning("PLACE ORDER EXCEPTION: %s", e)
    return flask.jsonify(
        {"order_status": "not_placed", "reason": "Internal Server Error!"}
    )