2017-04-10 64 views
0

我正在通過ExpressJs + Mongodb構建一個電子商務網站,並且我一直在關注這個問題: 技術上何時需要將購物車過期(將購物車移出並將產品退回庫存)?每當用戶訪問購物車?或者我應該需要一個cron工作?Mongodb:我們什麼時候需要購物車過期?

我已經按照這篇文章:https://www.infoq.com/articles/data-model-mongodb

這是我的車模型的實現:

'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); 

回答

0

您應該使用某種分佈式會話存儲的購物車!

我認爲你正在尋找的東西,如:https://www.youtube.com/watch?v=g32awc4HrLA

它採用expressjs-sessionmongodb那麼你有一個分佈式緩存,它會與你的應用程序的多個實例。

+0

hello @marco,我正在使用expressjs-session。我關心的是'''我們什麼時候需要在技術上過期購物車(取出購物車並將產品退回庫存)?每當用戶訪問購物車?或者我應該需要一個cron工作? ''' –

+0

如果您正在使用'mongodb',您可以設置'ttl' https://docs.mongodb.com/manual/tutorial/expire-data/,它將在一段時間後刪除文檔。如果你這樣做,你不需要使用'cron作業'。您可以在X天后過期。 –

+1

謝謝,我會嘗試與你建議 –

相關問題