text: const { getUser } = require('../shared/user-utils');
  const addOrder = require('../shared/add-order');
  const data = require('../shared/recommendations');
  const config = require('../shared/config');
  const { QueueClient } = require("@azure/storage-queue");
  
  module.exports = async function (context, req) {
  
    // Get the user details from the request
    var user = getUser(req);
    if(user === null || user === undefined){
      context.res.status(401);
      return;
      // Switch during local tests
      //user = { userDetails: "John Doe" };
    }
  
    const eventId = req.body.eventId;
    if(eventId !== undefined && eventId.length > 0) {
      await data.rewardItem(eventId, 1);
    }
  
    const userDetails = user.userDetails;
    const iceCreamId = req.body.id;
    const fullAddress = '1 Microsoft Way, Redmond, WA 98052, USA';
  
    try{
      const order = await addOrder.addOrder(userDetails, iceCreamId, fullAddress);
      
      // add the pre-order JSON document in a queue
      const orderJson = JSON.stringify(order);
      const orderJsonBase64 = Buffer.from(orderJson).toString('base64');
  
      const queueClient = new QueueClient(config.config.azure_storage_connectionstring, config.config.azure_storage_queue_preorder);
      await queueClient.createIfNotExists();
      await queueClient.sendMessage(orderJsonBase64);
  
      context.res.status(201).send(order.Id);
    } catch (error) {
      if (error === 'not found') {      
        context.res.status(404).send(error);
      } else {
        context.res.status(500).send(error);
      }
    }
  
  };
  