<commit_before>function roundToCents(value) {
  return Math.round(value * 100) /100; 
}

Meteor.methods({
  // Add to shopping cart collection
  addCartSampleItems: function() {

    var order = {
      userId: Meteor.userId(),
      vendorName: "<NAME>",
      vendorEmail: '<EMAIL>',
      vendorUserId: Random.id(),
      vendorBusinessId: null,
      vendorTotal: 75.10,
      products: [{
        productId: Random.id(),
        quantity: 3,
        name: "<NAME>",
        description: "Grandma went to great lengths to weave her heir on this loom",
        unitType: "pound",
        unitPrice: 4.20,
        photo: {src: "/images/user-images/AuntRubyTomato.png"},
        currency: 'USD',
        itemTotalPrice: 63.60,
      }, {
        productId: Random.id(),
        quantity: 0.5,
        name: "<NAME>",
        description: "Ask Dad why he is shivering",
        unitType: "pound",
        unitPrice: 3.00,
        currency: 'USD',
        photo: {src: "/images/user-images/ScarletNantesCarrot.png"},
        itemTotalPrice: 1.50,
      }, {
        productId: Random.id(),
        quantity: 2,
        name: "<NAME>",
        description: "Mmmmm so sweet and fruity",
        unitType: "bottle",
        unitPrice: 5.00,
        currency: 'USD',
        photo: {src: "/images/user-images/LittleGemLettuce.png"},
        itemTotalPrice: 10.00,
      }]
    }
    // console.log('adding cart sample order:', order);
    ShoppingCart.insert(order);
  },

  // Adds an item to the shopping cart collection
  addCartItem: function(item) {

      // TODO vendorEmail won't be exposed to the client (browser)
      // TODO when not logged in, add session id

      // If vendor doesn't exist in collection
      if (!Meteor.call('checkForVendor', item)) {
        // Create the product structure
        var product = {
        productId: item.productId,
        name: item.name,
        description: item.description,
        photo: item.photo,
        currency: item.currency,
        unitType: item.unitType,
        quantity: item.quantity,
        unitPrice: item.unitPrice,
        itemTotalPrice: roundToCents(item.quantity * item.unitPrice),
        }

        var vendorTotal = product.itemTotalPrice;

        // Create the order structure
        var order = {
        userId: Meteor.userId(),
        vendorUserId: item.vendorUserId,
        vendorBusinessId: item.vendorBusinessId,
        vendorName: item.vendorName,
        vendorLink: item.vendorLink,
        vendorEmail: item.vendorEmail,
        vendorTotal: vendorTotal,
        products: [ product, ],
        }

        // Add to collection
        ShoppingCart.insert(order);
      }
      else { // existing vendor<commit_msg>bug note<commit_after>function roundToCents(value) {
  return Math.round(value * 100) /100; 
}

Meteor.methods({
  // Add to shopping cart collection
  addCartSampleItems: function() {

    var order = {
      userId: Meteor.userId(),
      vendorName: "<NAME>",
      vendorEmail: '<EMAIL>',
      vendorUserId: Random.id(),
      vendorBusinessId: null,
      vendorTotal: 75.10,
      products: [{
        productId: Random.id(),
        quantity: 3,
        name: "<NAME>",
        description: "Grandma went to great lengths to weave her heir on this loom",
        unitType: "pound",
        unitPrice: 4.20,
        photo: {src: "/images/user-images/AuntRubyTomato.png"},
        currency: 'USD',
        itemTotalPrice: 63.60,
      }, {
        productId: Random.id(),
        quantity: 0.5,
        name: "<NAME>",
        description: "Ask Dad why he is shivering",
        unitType: "pound",
        unitPrice: 3.00,
        currency: 'USD',
        photo: {src: "/images/user-images/ScarletNantesCarrot.png"},
        itemTotalPrice: 1.50,
      }, {
        productId: Random.id(),
        quantity: 2,
        name: "<NAME>",
        description: "Mmmmm so sweet and fruity",
        unitType: "bottle",
        unitPrice: 5.00,
        currency: 'USD',
        photo: {src: "/images/user-images/LittleGemLettuce.png"},
        itemTotalPrice: 10.00,
      }]
    }
    // console.log('adding cart sample order:', order);
    ShoppingCart.insert(order);
  },

  // Adds an item to the shopping cart collection
  addCartItem: function(item) {

      // TODO vendorEmail won't be exposed to the client (browser)
      // TODO when not logged in, add session id

      // If vendor doesn't exist in collection
      if (!Meteor.call('checkForVendor', item)) {
        // Create the product structure
        var product = {
        productId: item.productId,
        name: item.name,
        description: item.description,
        photo: item.photo,
        currency: item.currency,
        unitType: item.unitType,
        quantity: item.quantity,
        unitPrice: item.unitPrice,
        itemTotalPrice: roundToCents(item.quantity * item.unitPrice),
        }

        var vendorTotal = product.itemTotalPrice;

        // Create the order structure
        var order = {
        userId: Meteor.userId(),
        vendorUserId: item.vendorUserId,
        vendorBusinessId: item.vendorBusinessId,
        vendorName: item.vendorName,
        vendorLink: item.vendorLink,
        vendorEmail: item.vendorEmail,
        vendorTotal: vendorTotal,
        products: [ product, ],
        }

        // Add to collection
        ShoppingCart.insert(order);
      }
      else { // existing vendor
        // TODO BUG not updating vendor total when update existing product with different quantity
{'fraction_non_alphanumeric': 0.11534276387377584, 'fraction_numerical': 0.012332245194051506, 'mean_word_length': 2.119343891402715, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6098519', 'n_tokens_mistral': 1665, 'n_tokens_neox': 1568, 'n_words': 423}