{"size": 1837, "ext": "js", "max_stars_repo_path": "after/routes/product.js", "max_stars_repo_name": "DynamicYield/api-tutorial-petshop", "max_stars_repo_stars_event_min_datetime": "2022-01-21T10:30:37.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-21T10:30:37.000Z", "max_issues_repo_path": "after/routes/product.js", "max_issues_repo_name": "DynamicYield/api-tutorial-petshop", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "after/routes/product.js", "max_forks_repo_name": "DynamicYield/api-tutorial-petshop", "max_forks_repo_forks_event_min_datetime": "2020-01-22T16:47:48.000Z", "max_forks_repo_forks_event_max_datetime": "2021-04-26T19:36:32.000Z"}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": 4, "avg_line_length": 26.6231884058, "max_line_length": 112, "alphanum_fraction": 0.6423516603}
const router = require('express').Router();
const createError = require('http-errors');
const products = require('../models/product');
const DYAPI = require('../DYAPI');

router.get('/:sku', async (req, res, next) => {
  const { sku } = req.params;

  const product = products.getProduct(sku);
  if (product.length === 0) {
    next(createError(404));
    return;
  }

  const currentProduct = product[0];

  const recommendations = products.getCategory(currentProduct.categories, 5, 1)
    .products.filter(_product => _product.sku !== sku)
    .slice(0, 4);

  res.render('product', {
    product: currentProduct,
    recommendations,
  });
});

router.post('/add', (req, res) => {
  const { sku, qty, price } = req.body;
  const addedProduct = products.getProduct(sku);

  if (addedProduct.length === 0) {
    res.status(404).json({ success: false });
    return;
  }

  /** add the item to the cart. cart is stored in the req.session cookie */
  const { cart } = req.session;
  const cartProducts = cart.products;
  const search = cartProducts.filter(item => item.sku === sku);
  if (search.length > 0) {
    cartProducts[cartProducts.indexOf(search[0])].qty = cartProducts[cartProducts.indexOf(search[0])].qty + qty;
  } else {
    cartProducts.push({ sku, qty, price });
  }

  req.session.cart.total = cartProducts.reduce((acc, product) => acc + (product.price * product.qty), 0);

  const addToCartEvent = {
    name: 'Add to Cart',
    properties: {
      dyType: 'add-to-cart-v1',
      value: price,
      productId: sku,
      quantity: qty,
    }
  };

  DYAPI.reportEvent(req.userId, req.sessionId, addToCartEvent);

  res.json({
    success: true,
    itemsInCart: req.session.cart.products.reduce((acc, product) => acc + product.qty, 0),
    total: req.session.cart.total.toFixed(2),
  });
});


module.exports = router;
