UNPKG

dbl-coins-invest

Version:

Sharade DBL package over other coins invest services

187 lines (153 loc) 8.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _models = require("./models"); var _rxjs = require("rxjs"); var _rxjs2 = _interopRequireDefault(_rxjs); var _dbl = require("./dbl"); var _dbl2 = _interopRequireDefault(_dbl); var _account2 = require("./account"); var _account3 = _interopRequireDefault(_account2); var _investments = require("./investments"); var _investments2 = _interopRequireDefault(_investments); var _sequelize = require("sequelize"); var Sequelize = _interopRequireWildcard(_sequelize); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var Op = Sequelize.Op; var PortfolioService = function (_DBL) { _inherits(PortfolioService, _DBL); function PortfolioService(userSub) { _classCallCheck(this, PortfolioService); if (!userSub) throw new Error('userSub have to be provided!!!!'); var _this = _possibleConstructorReturn(this, (PortfolioService.__proto__ || Object.getPrototypeOf(PortfolioService)).call(this)); _this.userSub = userSub; _this.model = _models.Portfolio; _this.investmentService = new _investments2.default(userSub); _this.accountServise = new _account3.default(userSub); return _this; } _createClass(PortfolioService, [{ key: "addInvestment", value: function addInvestment(portfolioId, investment) { return _rxjs2.default.Observable.forkJoin(this.investmentService.create(investment), this.getById(portfolioId)).mergeMap(function (_ref) { var _ref2 = _slicedToArray(_ref, 2), investModel = _ref2[0], portfolioModel = _ref2[1]; return _rxjs2.default.Observable.fromPromise(portfolioModel.addInvestments(investModel)); }); } }, { key: "getAll", value: function getAll() { return _rxjs2.default.Observable.fromPromise(_models.Account.findOne({ where: { userSub: _defineProperty({}, Op.eq, this.userSub) }, include: [{ model: _models.Portfolio, as: 'Portfolios' }] })).map(function (account) { if (!account) { return _rxjs2.default.Observable.throw('noAccountFound'); } return account.get('Portfolios'); }); } }, { key: "createPortfolio", value: function createPortfolio(portfolio) { return _rxjs2.default.Observable.forkJoin(this.accountServise.getUserAccount(), this.model.create(portfolio)).mergeMap(function (response) { var _response = _slicedToArray(response, 2), _account = _response[0], _portfolio = _response[1]; return _rxjs2.default.Observable.fromPromise(_account.addPortfolios(_portfolio)).map(function (account) { return _portfolio; }); }); } }, { key: "getById", value: function getById(portfolioId) { return _rxjs2.default.Observable.fromPromise(_models.Account.findOne({ where: { userSub: _defineProperty({}, Op.eq, this.userSub) }, include: [{ model: _models.Portfolio, as: 'Portfolios', where: { id: _defineProperty({}, Op.eq, portfolioId) } }] })).map(function (account) { return account.get('Portfolios')[0]; }); } }, { key: "delete", value: function _delete(portfolioId) { return this.getById(portfolioId).mergeMap(function (portfolioModel) { return _rxjs2.default.Observable.fromPromise(portfolioModel.destroy()); }); } //TODO should return investments!!! }, { key: "getInvestments", value: function getInvestments(portfolioId) { return _rxjs2.default.Observable.fromPromise(_models.Account.findOne({ where: { userSub: _defineProperty({}, Op.eq, this.userSub) }, include: [{ model: _models.Portfolio, as: 'Portfolios', where: { id: _defineProperty({}, Op.eq, portfolioId) }, include: [{ model: _models.Investment, as: 'Investments' }] }] })).map(function (account) { return account.get('Portfolios')[0].get('Investments'); }); } }, { key: "removeInvestment", value: function removeInvestment(portfolioId, investId) { var _this2 = this; return _rxjs2.default.Observable.fromPromise(_models.Account.findOne({ where: { userSub: _defineProperty({}, Op.eq, this.userSub) }, include: [{ model: _models.Portfolio, as: 'Portfolios', where: { id: _defineProperty({}, Op.eq, portfolioId) }, include: [{ model: _models.Investment, as: 'Investments', where: { id: _defineProperty({}, Op.eq, investId) } }] }] })).mergeMap(function (account) { return _rxjs2.default.Observable.fromPromise(account.get('Portfolios')[0].get('Investments')[0].destroy()); }).mergeMap(function (resp) { return _this2.getInvestments(portfolioId); }); } }]); return PortfolioService; }(_dbl2.default); exports.default = PortfolioService;