UNPKG

node-rigorous

Version:
93 lines (72 loc) 3.2 kB
"use strict"; 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); }); }; } var mongoose = require('mongoose'); var _require = require('../../../factory/RigorousError/index'), RigorousError = _require.RigorousError, errorTypes = _require.errorTypes; var mongoRead = require('./read'); var helper_format_checker = require('../../../helpers/h_format_checker'); module.exports = /*#__PURE__*/ function () { var _ref = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee(modelName, attributeObjectUpdated, beforeCreateTransformation, params) { var result, ids, resultJson; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: if (!helper_format_checker.isNil(params)) { _context.next = 2; break; } throw new RigorousError(errorTypes.RESPONSE_ERROR_OPERATION_OP_PARAMS_MISSING); case 2: if (!(helper_format_checker.isNil(params.selectAttributesReturned) && helper_format_checker.isObjectEmpty(params.selectAttributesReturned))) { _context.next = 4; break; } throw new RigorousError(errorTypes.RESPONSE_ERROR_OPERATION_OP_PARAMS_MISSING); case 4: _context.prev = 4; if (beforeCreateTransformation) { beforeCreateTransformation(attributeObjectUpdated); } // Create _context.next = 8; return mongoose.model(modelName).create(attributeObjectUpdated); case 8: result = _context.sent; ids = []; if (Array.isArray(result)) { result.forEach(function (resultUnit) { ids.push(resultUnit.id); }); } else { ids.push(result.id); } _context.next = 13; return mongoRead(modelName, { _id: { $in: ids } }, params); case 13: resultJson = _context.sent; return _context.abrupt("return", resultJson); case 17: _context.prev = 17; _context.t0 = _context["catch"](4); throw new RigorousError(errorTypes.RESPONSE_ERROR_OPERATION, _context.t0); case 20: case "end": return _context.stop(); } } }, _callee, null, [[4, 17]]); })); return function (_x, _x2, _x3, _x4) { return _ref.apply(this, arguments); }; }();