2017-04-10 4 views
0

Ich baue eine E-Commerce-Website von ExpressJs + Mongodb und ich bin mit dieser Sorge fest: Wann müssen wir den Warenkorb ablaufen lassen (entfernen Sie den Warenkorb und das Produkt in den Lagerbestand zurück) technisch? Wann immer Benutzer den Warenkorb besuchen? oder sollte ich einen Cron Job brauchen?Mongodb: Wann müssen Einkaufswagen verlängert werden?

habe ich diesen Artikel folgt: https://www.infoq.com/articles/data-model-mongodb

Implementierung des hier mein Warenkorb Modell:

'use strict'; 

const mongoose = require('mongoose'); 
const Schema = mongoose.Schema; 
const CartItem = new Schema({ 
    product: { type: Schema.Types.ObjectId, ref: 'Product' }, 
    quantity: Number 
}); 

const Cart = new Schema({ 
    userSessionId: String, 
    status: { 
     type: String, 
     enum: [ 'active', 'completed', 'expiring', 'expired' ], 
     default: 'active' 
    }, 
    items: [ CartItem ], 
    modifiedOn: { type: Date } 
}); 

Cart.static({ 
    summary: function(params, cb) { 
     this.aggregate([ 
      { 
       $match: { userSessionId: params.userSessionId } 
      }, 
      { 
       $unwind: { 
        path: '$items' 
       } 
      }, 
      { 
       $lookup: { 
        from: 'products', 
        localField: 'items.product', 
        foreignField: '_id', 
        as: 'product' 
       } 
      }, 
      { 
       $unwind: { 
        path: '$product', 
        preserveNullAndEmptyArrays: true 
       } 
      }, 
      { 
       $group: { 
        _id: { userSessionId: '$userSessionId' }, 
        count: { $sum: '$items.quantity' }, 
        total: { $sum: { $multiply: [ '$product.price', '$items.quantity' ] } } 
       } 
      } 
     ], (err, results) => cb(err, results[0])); 
    }, 
    addProduct: function(params, cb, test) { 
     var d = new Date(); 

     if (test) { 
      d.setMinutes(d.getMinutes() - 10); 
     } 

     this.findOneAndUpdate(
      { userSessionId: params.userSessionId }, 
      { $set: { modifiedOn: d } }, 
      { upsert: true, new: true }, (err, cart) => { 
       if (err) { 
        return cb(err); 
       } 

       const index = cart.items.findIndex((item) => { 
        return item.product.equals(params.productId); 
       }); 

       if (index === -1) { 
        cart.items.push({ 
         product: params.productId, 
         quantity: params.quantity 
        }); 
       } else { 
        cart.items[index].quantity += parseFloat(params.quantity); 
       } 
       cart.save(cb); 
      }); 
    }, 
    updateQuantity: function(params, cb) { 
     this.findOneAndUpdate(
      { userSessionId: params.userSessionId }, 
      {}, 
      { upsert: true, new: true }, (err, cart) => { 
       if (err) { 
        return cb(err); 
       } 

       const index = cart.items.findIndex((item) => { 
        return item.product.equals(params.productId); 
       }); 

       if (index === -1) { 
        return cb(new Error('Can not find product in cart')); 
       } 
       cart.items[index].quantity = params.quantity; 

       cart.save(cb); 
      }); 
    }, 
    findItem: function(params, cb) { 
     this.findOne({ userSessionId: params.userSessionId }).exec((err, cart) => { 
      if (err) { 
       return cb(err); 
      } 

      const index = cart.items.findIndex((item) => { 
       return item.product.equals(params.productId); 
      }); 

      if (index === -1) { 
       return cb(new Error('Can not find product in cart')); 
      } 

      cb(null, cart.items[index]); 
     }); 
    }, 
    removeProduct: function(params, cb) { 
     this.update(
      { userSessionId: params.userSessionId }, 
      { 
       $pull: { items: { product: params.productId } }, 
       $set: { modifiedOn: new Date() } 
      }, 
      cb 
     ); 
    }, 
    getExpiredCarts: function(params, cb) { 
     var now = new Date(); 

     if (typeof params.timeout !== 'number') { 
      return cb(new Error('timeout should be a number!')); 
     } 

     now.setMinutes(now.getMinutes() - params.timeout); 

     this.find(
      { modifiedOn: { $lte: now }, status: 'active' } 
     ).exec(cb); 
    } 
}); 

mongoose.model('Cart', Cart); 

Antwort

0

Sie irgendeine Art von verteilten Sitzung den Warenkorb speichern verwenden sollten!

Ich glaube, Sie suchen nach etwas, wie: https://www.youtube.com/watch?v=g32awc4HrLA

Es verwendet expressjs-session und mongodb dann haben Sie einen verteilten Cache und es wird mit mehreren Instanzen Ihrer Anwendung zu arbeiten.

+0

hallo @ marco, ich benutze expressjs-session tatsächlich. Meine Sorge ist über '' 'Wann müssen wir den Wagen ablaufen lassen (den Wagen entfernen und das Produkt ins Inventar zurückbringen) technisch? Wann immer Benutzer den Warenkorb besuchen? oder sollte ich einen Cron Job brauchen? '' ' –

+0

Wenn Sie' mongodb' verwenden, können Sie eine 'ttl' https://docs.mongodb.com/manual/tutorial/expire-data/ festlegen, die das Dokument nach einer bestimmten Zeit entfernt. Wenn Sie das tun, müssen Sie keinen Cron-Job verwenden. Sie können nach X Tagen ablaufen. –

+1

Vielen Dank, ich werde mit Ihnen einen Vorschlag versuchen –

Verwandte Themen