<commit_before>Meteor.publish('mart/cart', function() {
  var sub = this, lineItemHandles = [], cartHandle = null;

  // send over all line items for a specific cart
  function publishCartLineItems(cartId) {
    var lineItemsCursor = Mart.LineItems.find({cartId: cartId});
    lineItemHandles[cartId] =
      Mongo.Collection._publishCursor(lineItemsCursor, sub, 'MartLineItems');
  }

  cartHandle = Mart.Carts.find({ userId: userId, state: "current"}).observeChanges({
    added: function(id, cart) {
      publishCartLineItems(id);
      sub.added('MartCarts', id, cart);
    },
    changed: function(id, fields) {
      sub.changed('MartCarts', id, fields);
    },
    removed: function(id) {
      // stop observing changes on the post's comments
      lineItemHandles[id] && lineItemHandles[id].stop();
      // delete the post
      sub.removed('MartCarts', id);
    }
  });

  sub.ready();

  // make sure we clean everything up (note `_publishCursor`
  //   does this for us with the comment observers)
  sub.onStop(function() { cartHandle.stop(); });
});

Mart.Carts.permit(['insert', 'update', 'remove']).never().apply();

Meteor.methods({
  'mart/cart/findCurrentOrCreate': function() {
    userId = Meteor.userId()
    return Mart.Carts.upsert({state: "current", userId: userId}, {$set: {state: "current", userId: userId}})<commit_msg>Add products to cart subscription<commit_after>Meteor.publish('mart/cart', function() {
  var sub = this, productHandles = [], lineItemHandles = [],
      cartHandle = null, lineItemHandle = null;
  var userId = this.userId

  // send over all line items for a specific cart
  function publishCartLineItems(cartId) {
    var lineItemsCursor = Mart.LineItems.find({cartId: cartId})

    lineItemHandles[cartId] =
      Mongo.Collection._publishCursor(lineItemsCursor, sub, 'MartLineItems');

    lineItemHandle = Mart.LineItems.find({cartId: cartId}).observeChanges({
        added: function(id, lineItem) {
          publishLineItemProducts(id);
          sub.added('MartLineItems', id, lineItem)
        },
        changed: function(id, fields) {
          sub.changed('MartLineItems', id, fields)
        },
        removed: function(id) {
          productHandles[id] && productHandles[id].stop();
          // delete the post
          sub.removed('MartLineItems', id);
        }
      });
  }

  function publishLineItemProducts(lineItemId) {
    var lineItem = Mart.LineItems.findOne(lineItemId)
    var productsCursor = Mart.Products.find({_id: lineItem.productId})
    productHandles[lineItemId] =
      Mongo.Collection._publishCursor(productsCursor, sub, "MartProducts")
  }

  cartHandle = Mart.Carts.find({userId: userId, state: "current"}).observeChanges({
    added: function(id, cart) {
      publishCartLineItems(id);
      sub.added('MartCarts', id, cart);
    },
    changed: function(id, fields) {
      sub.changed('MartCarts', id, fields);
    },
    removed: function(id) {
      // stop observing changes on the post's comments
      lineItemHandles[id] && lineItemHandles[id].stop();
      // delete the post
      sub.removed('MartCarts', id);
    }
  });

  sub.ready();

  // make sure we clean everything up (note `_publishCursor`
  //   does this for us with the comment observers)
  sub.onStop(function() {
    cartHandle.stop();
    lineItemHandle.stop();
  });
});

Mart.Carts.permit(['insert', 'update', 'remove']).never().apply();

Meteor.methods({
  'mart/cart/findCurrentOrCreate': function() {
    console.log('mart/cart/findCurrentOrCreate');
    userId = Meteor.userId()
    return Mart.Carts.upsert({state: "current", userId: userId}, {$set: {state: "current", userId: userId}})
{'fraction_non_alphanumeric': 0.14702673213311512, 'fraction_numerical': 0.0, 'mean_word_length': 3.4993865030674844, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1276497', 'n_tokens_mistral': 1127, 'n_tokens_neox': 1121, 'n_words': 246}