UNPKG

@fabrix/spool-cart

Version:

Spool - eCommerce Spool for Fabrix

592 lines (591 loc) 20 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const common_1 = require("@fabrix/fabrix/dist/common"); const errors_1 = require("@fabrix/spool-sequelize/dist/errors"); const Validator = require("../../validator"); const enums_1 = require("../../enums"); class CartController extends common_1.FabrixController { generalStats(req, res) { res.json({}); } init(req, res) { if (!req.cart) { if (!req.body) { req.body = {}; } if (req.customer && !req.body.customer_id) { req.body.customer_id = req.customer.id; } if (req.user && !req.body.customer_id) { req.body.customer_id = req.user.current_customer_id; } this.app.services.CartService.create(req.body) .then(cart => { if (!cart) { throw new Error('Unexpected Error while creating cart'); } return new Promise((resolve, reject) => { req.loginCart(cart, function (err) { if (err) { return reject(err); } return resolve(cart); }); }); }) .then(cart => { return this.app.services.PermissionsService.sanitizeResult(req, cart); }) .then(result => { return res.json(result); }) .catch(err => { this.app.log.error('CartController.init', err); return res.serverError(err); }); } else { res.json(req.cart); } } count(req, res) { const EventsService = this.app.services.EventsService; EventsService.count('Cart') .then(count => { const counts = { carts: count }; return res.json(counts); }) .catch(err => { return res.serverError(err); }); } session(req, res) { if (!req.cart) { return res.sendStatus(401); } return this.app.models['Cart'].findByIdDefault(req.cart.id) .then(cart => { return this.app.services.PermissionsService.sanitizeResult(req, cart); }) .then(result => { return res.json(result); }); } findById(req, res) { const orm = this.app.models; const Cart = orm['Cart']; Cart.findByIdDefault(req.params.id, {}) .then(cart => { if (!cart) { throw new errors_1.ModelError('E_NOT_FOUND', `Cart id ${req.params.id} not found`); } return this.app.services.PermissionsService.sanitizeResult(req, cart); }) .then(result => { return res.json(result); }) .catch(err => { return res.serverError(err); }); } resolve(req, res) { const Cart = this.app.models['Cart']; if (!req.params.id) { const err = new Error('id is required'); return res.serverError(err); } Cart.resolve(req.params.id, {}) .then(cart => { if (!cart) { throw new errors_1.ModelError('E_NOT_FOUND', `Cart ${req.params.id} not found`); } return this.app.services.PermissionsService.sanitizeResult(req, cart); }) .then(result => { return res.json(result); }) .catch(err => { return res.serverError(err); }); } findAll(req, res) { const Cart = this.app.models['Cart']; const limit = Math.max(0, req.query.limit || 10); const offset = Math.max(0, req.query.offset || 0); const sort = req.query.sort || [['created_at', 'DESC']]; const where = req.query.where; const include = req.query.include || []; Cart.findAndCountAll({ order: sort, offset: offset, limit: limit, where: where, include: include }) .then(carts => { res.paginate(carts.count, limit, offset, sort); return this.app.services.PermissionsService.sanitizeResult(req, carts.rows); }) .then(result => { return res.json(result); }) .catch(err => { return res.serverError(err); }); } customer(req, res) { const orm = this.app.models; const Cart = orm['Cart']; const Customer = orm['Customer']; Cart.findById(req.params.id, { attributes: ['id', 'customer_id'] }) .then(cart => { if (!cart) { throw new errors_1.ModelError('E_NOT_FOUND', `Cart id ${req.params.id} not found`); } if (!cart.customer_id) { throw new errors_1.ModelError('E_NOT_FOUND', `Cart id ${req.params.id} customer not found`); } return Customer.findById(cart.customer_id); }) .then(customer => { if (!customer) { throw new errors_1.ModelError('E_NOT_FOUND', `Cart id ${req.params.id} customer not found`); } return this.app.services.PermissionsService.sanitizeResult(req, customer); }) .then(result => { return res.json(result); }) .catch(err => { return res.serverError(err); }); } create(req, res) { const CartService = this.app.services.CartService; if (req.customer && !req.body.customer_id) { req.body.customer_id = req.customer.id; } if (req.user && !req.body.customer_id) { req.body.customer_id = req.user.current_customer_id; } if (req.user && !req.body.owners) { req.body.owners = [req.user]; } Validator.validateCart.create(req.body) .then(values => { return CartService.create(req.body); }) .then(cart => { if (!cart) { throw new Error('Unexpected Error while creating cart'); } return new Promise((resolve, reject) => { req.loginCart(cart, function (err) { if (err) { return reject(err); } return resolve(cart); }); }); }) .then(cart => { return this.app.services.PermissionsService.sanitizeResult(req, cart); }) .then(result => { return res.json(result); }) .catch(err => { this.app.log.error('CartController.create', err); return res.serverError(err); }); } draft(req, res) { const CartService = this.app.services.CartService; req.body.status = enums_1.CART_STATUS.DRAFT; Validator.validateCart.create(req.body) .then(values => { return CartService.create(req.body); }) .then(cart => { if (!cart) { throw new Error('Unexpected Error while creating cart'); } return this.app.services.PermissionsService.sanitizeResult(req, cart); }) .then(result => { return res.json(result); }) .catch(err => { this.app.log.error('CartController.draft', err); return res.serverError(err); }); } update(req, res) { const CartService = this.app.services.CartService; let id = req.params.id; if (!id && req.body.id) { id = req.body.id; } else if (!id && req.body.token) { id = req.body.token; } else if (!id && req.cart) { id = req.cart.id; } if (req.customer && !req.body.customer_id) { req.body.customer_id = req.customer.id; } if (req.user && !req.body.customer_id) { req.body.customer_id = req.user.current_customer_id; } if (req.user && !req.body.owners) { req.body.owners = [req.user]; } Validator.validateCart.update(req.body) .then(values => { return CartService.update(id, req.body); }) .then(cart => { if (!cart) { throw new Error('Unexpected Error while creating cart'); } return new Promise((resolve, reject) => { req.loginCart(cart, function (err) { if (err) { return reject(err); } return resolve(cart); }); }); }) .then(cart => { return this.app.services.PermissionsService.sanitizeResult(req, cart); }) .then(result => { return res.json(result); }) .catch(err => { this.app.log.error('CartController.update', err); return res.serverError(err); }); } checkout(req, res) { if (!req.body.cart) { req.body.cart = {}; } if (!req.body.customer) { req.body.customer = {}; } const CartService = this.app.services.CartService; Validator.validateCart.checkout(req.body) .then(values => { const cartId = req.params.id || req.body.cart.id; const customerId = req.params.customer || req.body.customer.id || req.body.cart.customer_id; if (!cartId && req.cart) { req.body.cart = req.cart; } else if (cartId) { req.body.cart.id = cartId; } if (!customerId && req.customer) { req.body.customer = req.customer; } else if (!customerId && !req.customer && req.user) { req.body.customer.id = req.user.current_customer_id; } else if (customerId) { req.body.customer.id = customerId; } if (!req.body.cart.id) { throw new Error('Checkout requires a cart session or cart id, or token'); } return CartService.checkout(req); }) .then(data => { if (!data || !data.cart || !data.order) { throw new Error('Unexpected Error while checking out'); } return res.json({ cart: data.cart, order: data.order }); }) .catch(err => { this.app.log.error('CartController.checkout', err); return res.serverError(err); }); } addItems(req, res) { const CartService = this.app.services.CartService; let id = req.params.id; if (!id && req.cart) { id = req.cart.id; } Validator.validateCart.addItems(req.body) .then(values => { return CartService.addItemsToCart(req.body, id); }) .then(cart => { if (!cart) { throw new Error('Unexpected Error while adding items'); } return this.app.services.PermissionsService.sanitizeResult(req, cart); }) .then(result => { return res.json(result); }) .catch(err => { this.app.log.error('CartController.addItems', err); return res.serverError(err); }); } removeItems(req, res) { const CartService = this.app.services.CartService; let id = req.params.id; if (!id && req.cart) { id = req.cart.id; } Validator.validateCart.removeItems(req.body) .then(values => { return CartService.removeItemsFromCart(req.body, id); }) .then(cart => { if (!cart) { throw new Error('Unexpected Error while removing items'); } return this.app.services.PermissionsService.sanitizeResult(req, cart); }) .then(result => { return res.json(result); }) .catch(err => { this.app.log.error('CartController.removeItems', err); return res.serverError(err); }); } addShipping(req, res) { const CartService = this.app.services.CartService; Validator.validateCart.addShipping(req.body) .then(values => { return CartService.addShipping(req.params.id, req.body); }) .then(cart => { return this.app.services.PermissionsService.sanitizeResult(req, cart); }) .then(result => { return res.json(result); }) .catch(err => { this.app.log.error('CartController.addShipping', err); return res.serverError(err); }); } removeShipping(req, res) { const CartService = this.app.services.CartService; Validator.validateCart.removeShipping(req.body) .then(values => { return CartService.removeShipping(req.params.id, req.body); }) .then(cart => { return this.app.services.PermissionsService.sanitizeResult(req, cart); }) .then(result => { return res.json(result); }) .catch(err => { this.app.log.error('CartController.removeShipping', err); return res.serverError(err); }); } addTaxes(req, res) { const CartService = this.app.services.CartService; Validator.validateCart.addTaxes(req.body) .then(values => { return CartService.addTaxes(req.params.id, req.body); }) .then(cart => { return this.app.services.PermissionsService.sanitizeResult(req, cart); }) .then(result => { return res.json(result); }) .catch(err => { this.app.log.error('CartController.addTaxes', err); return res.serverError(err); }); } removeTaxes(req, res) { const CartService = this.app.services.CartService; Validator.validateCart.removeTaxes(req.body) .then(values => { return CartService.removeTaxes(req.params.id, req.body); }) .then(cart => { return this.app.services.PermissionsService.sanitizeResult(req, cart); }) .then(result => { return res.json(result); }) .catch(err => { this.app.log.error('CartController.removeTaxes', err); return res.serverError(err); }); } pricingOverrides(req, res) { const CartService = this.app.services.CartService; let id = req.params.id; if (!id && req.body.id) { id = req.body.id; } if (!id && req.cart) { id = req.cart.id; } Validator.validateCart.pricingOverrides(req.body) .then(values => { return CartService.pricingOverrides(req.body, id, req.user); }) .then(cart => { if (!cart) { throw new Error('Unexpected Error while overriding prices'); } return this.app.services.PermissionsService.sanitizeResult(req, cart); }) .then(result => { return res.json(result); }) .catch(err => { this.app.log.error('CartController.pricingOverrides', err); return res.serverError(err); }); } clear(req, res) { const CartService = this.app.services.CartService; let id = req.params.id; if (!id && req.cart) { id = req.cart.id; } Validator.validateCart.clear(req.body) .then(values => { return CartService.clearCart(id); }) .then(cart => { if (!cart) { throw new Error('Unexpected error while clearing cart'); } return this.app.services.PermissionsService.sanitizeResult(req, cart); }) .then(result => { return res.json(result); }) .catch(err => { this.app.log.error('CartController.clear', err); return res.serverError(err); }); } login(req, res) { const Cart = this.app.models['Cart']; let cartId = req.params.id; let customerId; if (!cartId && req.user) { cartId = req.user.current_cart_id; } if (req.user) { customerId = req.user.current_customer_id; } Cart.resolve(cartId) .then(cart => { if (!cart) { throw new Error('Unexpected Error while authenticating cart'); } if (customerId) { return cart.setCustomer(customerId); } return cart; }) .then(cart => { return new Promise((resolve, reject) => { req.loginCart(cart, function (err) { if (err) { return reject(err); } return resolve(cart); }); }); }) .then(cart => { if (!cart) { throw new Error('Unexpected Error while login cart in'); } return this.app.services.PermissionsService.sanitizeResult(req, cart); }) .then(result => { return res.json(result); }) .catch(err => { this.app.log.error('CartController.login', err); return res.serverError(err); }); } switchCart(req, res) { const cartId = req.params.id; const Cart = this.app.models['Cart']; const User = this.app.models['User']; if (!cartId || !req.user) { const err = new Error('A cart id and a user in session are required'); return res.serverError(err); } let resCart; Cart.resolve(cartId) .then(foundCart => { if (!foundCart) { throw new Error('Unable to resolve cart'); } resCart = foundCart; return User.findById(req.user.id); }) .then(user => { user.current_cart_id = resCart.id; return user.save(); }) .then(user => { req.user.current_cart_id = resCart.id; return resCart.setCustomer(req.user.current_customer_id); }) .then(() => { return new Promise((resolve, reject) => { req.loginCart(resCart, (err) => { if (err) { return reject(err); } return resolve(resCart); }); }); }) .then(cart => { if (!cart) { throw new Error('Unexpected Error while switching cart'); } return this.app.services.PermissionsService.sanitizeResult(req, cart); }) .then(result => { return res.json(result); }) .catch(err => { this.app.log.error('CartController.switchCart', err); return res.serverError(err); }); } logout(req, res) { req.logoutCart(); res.ok(); } addCoupon(req, res) { } removeCoupon(req, res) { } } exports.CartController = CartController;