UNPKG

trailpack-proxy-cart

Version:

eCommerce - Trailpack for Proxy Engine

224 lines (203 loc) 5.11 kB
/* eslint no-underscore-dangle: [0]*/ /* eslint no-console: [0] */ 'use strict' /** * Module dependencies. */ const req = module.exports = {} /** * Initiate a login session for `cart`. * * Options: * - `session` Save login state in session, defaults to _true_ * * Examples: * * req.logInCart(cart, { session: false }) * * req.logInCart(cart, function(err) { * if (err) { throw err } * // session saved * }) * * @param {Cart} cart * @param {Object} options * @param {Function} done * @api public */ req.loginCart = req.logInCart = function(cart, options, done) { if (typeof options == 'function') { done = options options = {} } options = options || {} let property = 'cart' if (this._proxyCart && this._proxyCart.instance) { property = this._proxyCart.instance._cartProperty || 'cart' } const session = (options.session === undefined) ? true : options.session this[property] = cart if (session) { if (!this._proxyCart) { throw new Error('proxyCart.initialize() middleware not in use') } if (typeof done !== 'function') { throw new Error('req#loginCart requires a callback function') } this._proxyCart.instance.serializeCart(cart, (err, obj) => { if (err) { this[property] = null return done(err) } if (!this._proxyCart.session) { this._proxyCart.session = {} } this._proxyCart.session.cart = obj if (!this.session) { this.session = {} } this.session[this._proxyCart.instance._key] = this._proxyCart.session done() }) } else { done && done() } } /** * Initiate a login session for `customer`. * * Options: * - `session` Save login state in session, defaults to _true_ * * Examples: * * req.logInCustomer(customer, { session: false }) * * req.logInCustomer(customer, function(err) { * if (err) { throw err } * // session saved * }) * * @param {Customer} customer * @param {Object} options * @param {Function} done * @api public */ req.loginCustomer = req.logInCustomer = function(customer, options, done) { if (typeof options == 'function') { done = options options = {} } options = options || {} let property = 'customer' if (this._proxyCart && this._proxyCart.instance) { property = this._proxyCart.instance._customerProperty || 'customer' } const session = (options.session === undefined) ? true : options.session this[property] = customer if (session) { if (!this._proxyCart) { throw new Error('proxyCart.initialize() middleware not in use') } if (typeof done !== 'function') { throw new Error('req#loginCustomer requires a callback function') } this._proxyCart.instance.serializeCustomer(customer, (err, obj) => { if (err) { this[property] = null return done(err) } if (!this._proxyCart.session) { this._proxyCart.session = {} } this._proxyCart.session.customer = obj if (!this.session) { this.session = {} } this.session[this._proxyCart.instance._key] = this._proxyCart.session done() }) } else { done && done() } } /** * Terminate an existing login session. * * @api public */ req.logoutCart = req.logOutCart = function() { let property = 'cart' if (this._proxyCart && this._proxyCart.instance) { property = this._proxyCart.instance._cartProperty || 'cart' } this[property] = null if (this._proxyCart && this._proxyCart.session) { delete this._proxyCart.session.cart } } /** * Terminate an existing login session. * * @api public */ req.logoutCustomer = req.logOutCustomer = function() { let property = 'customer' if (this._proxyCart && this._proxyCart.instance) { property = this._proxyCart.instance._customerProperty || 'customer' } this[property] = null if (this._proxyCart && this._proxyCart.session) { delete this._proxyCart.session.customer } } /** * Test if request is has a cart. * * @return {Boolean} * @api public */ req.hasCart = function() { let property = 'cart' if (this._proxyCart && this._proxyCart.instance) { property = this._proxyCart.instance._cartProperty || 'cart' } return !!(this[property]) } /** * Test if request has no cart. * * @return {Boolean} * @api public */ req.hasNoCart = function() { return !this.hasCart() } /** * Test if request is has a customer. * * @return {Boolean} * @api public */ req.hasCustomer = function() { let property = 'customer' if (this._proxyCart && this._proxyCart.instance) { property = this._proxyCart.instance._customerProperty || 'customer' } return !!(this[property]) } /** * Test if request has no customer. * * @return {Boolean} * @api public */ req.hasNoCustomer = function() { return !this.hasCustomer() }