@fabrix/spool-cart
Version:
Spool - eCommerce Spool for Fabrix
104 lines (103 loc) • 4.58 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const common_1 = require("@fabrix/fabrix/dist/common");
const cart_1 = require("./cart");
const Validator = require("./validator");
const utils_1 = require("./utils");
const config = require("./config/index");
const pkg = require("../package.json");
const api = require("./api/index");
class CartSpool extends common_1.Spool {
constructor(app) {
super(app, {
config: config,
pkg: pkg,
api: api
});
}
validate() {
return __awaiter(this, void 0, void 0, function* () {
const requiredSpools = [
'router',
'express',
'sequelize',
'events',
'crons',
'permissions',
'passport',
'notifications',
'generics',
'cart-countries'
];
const spools = Object.keys(this.app.spools);
if (!spools.some(v => requiredSpools.indexOf(v) >= 0)) {
return Promise.reject(new Error(`spool-cart requires spools: ${requiredSpools.join(', ')}!`));
}
if (!this.app.config.get('crons')) {
return Promise.reject(new Error('No configuration found at config.crons!'));
}
if (!this.app.config.get('events')) {
return Promise.reject(new Error('No configuration found at config.events!'));
}
if (!this.app.config.get('cart')) {
return Promise.reject(new Error('No configuration found at config.cart!'));
}
if (!this.app.config.get('notifications')) {
return Promise.reject(new Error('No configuration found at config.notifications!'));
}
if (!this.app.config.get('generics')) {
return Promise.reject(new Error('No configuration found at config.generics!'));
}
if (!this.app.config.get('permissions')) {
return Promise.reject(new Error('No configuration found at config.permissions!'));
}
if (!this.app.config.get('passport')) {
return Promise.reject(new Error('No configuration found at config.passport!'));
}
if (!this.app.config.get('web.middlewares.order').some(a => a === 'cartInit')) {
return Promise.reject(new Error('cartInit middleware missing in web.middlewares.order!'));
}
if (!this.app.config.get('web.middlewares.order').some(a => a === 'cartSession')) {
return Promise.reject(new Error('cartSession middleware missing in web.middlewares.order!'));
}
return Promise.all([
Validator.validateCartSpool.config(this.app.config.get('cart')),
Validator.validateMiddleware.config(this.app.config.get('web.middlewares'))
]);
});
}
configure() {
return __awaiter(this, void 0, void 0, function* () {
return Promise.all([
cart_1.Cart.configure(this.app),
cart_1.Cart.resolveGenerics(this.app),
cart_1.Cart.copyDefaults(this.app)
]);
});
}
initialize() {
return __awaiter(this, void 0, void 0, function* () {
return Promise.all([
utils_1.Utils.buildShopFixtures(this.app)
.then(fixtures => {
this.shopFixtures = fixtures;
return utils_1.Utils.loadShopFixtures(this.app);
}),
utils_1.Utils.buildCountryFixtures(this.app)
.then(fixtures => {
this.countryFixtures = fixtures;
return utils_1.Utils.loadCountryFixtures(this.app);
})
]);
});
}
}
exports.CartSpool = CartSpool;