@fabrix/spool-cart
Version:
Spool - eCommerce Spool for Fabrix
187 lines (186 loc) • 6.36 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.req = {};
exports.req.loginCart =
exports.req.logInCart = function (cart, options, done) {
if (typeof options === 'function') {
done = options;
options = {};
}
options = options || {};
let property = 'cart';
if (this._cart && this._cart.instance) {
property = this._cart.instance._cartProperty || 'cart';
}
const session = (options.session === undefined) ? true : options.session;
this[property] = cart;
if (session) {
if (!this._cart) {
throw new Error('cart.initialize() middleware not in use');
}
if (typeof done !== 'function') {
throw new Error('req#loginCart requires a callback function');
}
this._cart.instance.serializeCart(cart, (err, obj) => {
if (err) {
this[property] = null;
return done(err);
}
if (!this._cart.session) {
this._cart.session = {};
}
this._cart.session.cart = obj;
if (!this.session) {
this.session = {};
}
this.session[this._cart.instance._key] = this._cart.session;
done();
});
}
else {
done && done();
}
};
exports.req.loginCustomer =
exports.req.logInCustomer = function (customer, options, done) {
if (typeof options === 'function') {
done = options;
options = {};
}
options = options || {};
let property = 'customer';
if (this._cart && this._cart.instance) {
property = this._cart.instance._customerProperty || 'customer';
}
const session = (options.session === undefined) ? true : options.session;
this[property] = customer;
if (session) {
if (!this._cart) {
throw new Error('cart.initialize() middleware not in use');
}
if (typeof done !== 'function') {
throw new Error('req#loginCustomer requires a callback function');
}
this._cart.instance.serializeCustomer(customer, (err, obj) => {
if (err) {
this[property] = null;
return done(err);
}
if (!this._cart.session) {
this._cart.session = {};
}
this._cart.session.customer = obj;
if (!this.session) {
this.session = {};
}
this.session[this._cart.instance._key] = this._cart.session;
done();
});
}
else {
done && done();
}
};
exports.req.loginShop =
exports.req.logInShop = function (shop, options, done) {
if (typeof options === 'function') {
done = options;
options = {};
}
options = options || {};
let property = 'shop';
if (this._cart && this._cart.instance) {
property = this._cart.instance._shopProperty || 'shop';
}
const session = (options.session === undefined) ? true : options.session;
this[property] = shop;
if (session) {
if (!this._cart) {
throw new Error('cart.initialize() middleware not in use');
}
if (typeof done !== 'function') {
throw new Error('req#loginShop requires a callback function');
}
this._cart.instance.serializeShop(shop, (err, obj) => {
if (err) {
this[property] = null;
return done(err);
}
if (!this._cart.session) {
this._cart.session = {};
}
this._cart.session.shop = obj;
if (!this.session) {
this.session = {};
}
this.session[this._cart.instance._key] = this._cart.session;
done();
});
}
else {
done && done();
}
};
exports.req.logoutCart =
exports.req.logOutCart = function () {
let property = 'cart';
if (this._cart && this._cart.instance) {
property = this._cart.instance._cartProperty || 'cart';
}
this[property] = null;
if (this._cart && this._cart.session) {
delete this._cart.session.cart;
}
};
exports.req.logoutCustomer =
exports.req.logOutCustomer = function () {
let property = 'customer';
if (this._cart && this._cart.instance) {
property = this._cart.instance._customerProperty || 'customer';
}
this[property] = null;
if (this._cart && this._cart.session) {
delete this._cart.session.customer;
}
};
exports.req.logoutShop =
exports.req.logOutShop = function () {
let property = 'shop';
if (this._cart && this._cart.instance) {
property = this._cart.instance._shopProperty || 'shop';
}
this[property] = null;
if (this._cart && this._cart.session) {
delete this._cart.session.shop;
}
};
exports.req.hasCart = function () {
let property = 'cart';
if (this._cart && this._cart.instance) {
property = this._cart.instance._cartProperty || 'cart';
}
return !!(this[property]);
};
exports.req.hasNoCart = function () {
return !this.hasCart();
};
exports.req.hasCustomer = function () {
let property = 'customer';
if (this._cart && this._cart.instance) {
property = this._cart.instance._customerProperty || 'customer';
}
return !!(this[property]);
};
exports.req.hasNoCustomer = function () {
return !this.hasCustomer();
};
exports.req.hasShop = function () {
let property = 'shop';
if (this._cart && this._cart.instance) {
property = this._cart.instance._shopProperty || 'shop';
}
return !!(this[property]);
};
exports.req.hasNoShop = function () {
return !this.hasShop();
};