UNPKG

node-rigorous

Version:
279 lines (228 loc) 11 kB
"use strict"; function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(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 asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } /* eslint class-methods-use-this: 0 */ var _ = require('lodash'); var _require = require('../../factory/RigorousError/index'), RigorousError = _require.RigorousError, errorTypes = _require.errorTypes; var RigorousRoute = require('../RigorousRoute'); var h_secure_user_input = require('../../helpers/h_secure_user_input'); var h_format_checker = require('../../helpers/h_format_checker'); var Route = /*#__PURE__*/ function (_RigorousRoute) { _inherits(Route, _RigorousRoute); function Route(middlewares, modelService, authorizationUserId) { var _this; _classCallCheck(this, Route); _this = _possibleConstructorReturn(this, _getPrototypeOf(Route).call(this, middlewares, {})); _this.modelService = modelService; _this.authorizationUserId = authorizationUserId; return _this; } _createClass(Route, [{ key: "secureInput", value: function () { var _secureInput = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee(req) { var body, inputs; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: body = _.get(req, 'body'); this.userIdAsking = req.authuser.user_id; inputs = { ids: h_secure_user_input.escapeHtml(_.get(body, 'ids')) }; return _context.abrupt("return", inputs); case 4: case "end": return _context.stop(); } } }, _callee, this); })); function secureInput(_x) { return _secureInput.apply(this, arguments); } return secureInput; }() }, { key: "checkConformityInput", value: function () { var _checkConformityInput = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee3(inputs) { var _this2 = this; var checker, checkConformityIdInput; return regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: checker = {}; checkConformityIdInput = /*#__PURE__*/ function () { var _ref = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee2(ids) { var promises, i, id, modelCheckers; return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: promises = []; for (i = 0; i < ids.length; i += 1) { id = ids[i]; promises.push(_this2.modelService.readIt({ _id: id }, { selectAttributesReturned: _defineProperty({}, _this2.authorizationUserId, 1) })); } _context2.next = 4; return Promise.all(promises); case 4: modelCheckers = _context2.sent; modelCheckers.forEach(function (modelChecker) { if (h_format_checker.isNil(modelChecker)) { throw new RigorousError(errorTypes.RESPONSE_ERROR_DATA_NOT_CONFORM); } }); return _context2.abrupt("return", modelCheckers); case 7: case "end": return _context2.stop(); } } }, _callee2); })); return function checkConformityIdInput(_x3) { return _ref.apply(this, arguments); }; }(); _context3.next = 4; return checkConformityIdInput(inputs.ids); case 4: checker.modelCheckers = _context3.sent; checker.modelCheckers.forEach(function (modelChecker) { if (h_format_checker.isNil(modelChecker)) { throw new RigorousError(errorTypes.RESPONSE_ERROR_DATA_NOT_CONFORM); } }); return _context3.abrupt("return", checker); case 7: case "end": return _context3.stop(); } } }, _callee3); })); function checkConformityInput(_x2) { return _checkConformityInput.apply(this, arguments); } return checkConformityInput; }() }, { key: "checkAuthorization", value: function () { var _checkAuthorization = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee4(checker) { var _this3 = this; return regeneratorRuntime.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: checker.modelCheckers.forEach(function (modelChecker) { if (!modelChecker[_this3.authorizationUserId].equals(_this3.userIdAsking)) { throw new RigorousError(errorTypes.RESPONSE_ERROR_DATA_NOT_CONFORM); } }); case 1: case "end": return _context4.stop(); } } }, _callee4); })); function checkAuthorization(_x4) { return _checkAuthorization.apply(this, arguments); } return checkAuthorization; }() }, { key: "processData", value: function () { var _processData = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee5(inputs, checker) { var promises, i, id; return regeneratorRuntime.wrap(function _callee5$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: promises = []; for (i = 0; i < inputs.ids.length; i += 1) { id = inputs.ids[i]; promises.push(this.modelService.deleteIt({ _id: id })); } _context5.next = 4; return Promise.all(promises); case 4: return _context5.abrupt("return", { result: null, notificationParam: inputs }); case 5: case "end": return _context5.stop(); } } }, _callee5, this); })); function processData(_x5, _x6) { return _processData.apply(this, arguments); } return processData; }() }, { key: "sendNotification", value: function () { var _sendNotification = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee6(inputs, notificationParam) { return regeneratorRuntime.wrap(function _callee6$(_context6) { while (1) { switch (_context6.prev = _context6.next) { case 0: case "end": return _context6.stop(); } } }, _callee6); })); function sendNotification(_x7, _x8) { return _sendNotification.apply(this, arguments); } return sendNotification; }() }]); return Route; }(RigorousRoute); module.exports = Route;