UNPKG

kritsana135-hooray-server

Version:

for server

80 lines (60 loc) 3.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.checkFeatureKey = void 0; var _apolloServer = require("apollo-server"); var _AppModel = _interopRequireDefault(require("../models/AppModel")); var _language = require("../constants/language"); var _application = require("../constants/errors/application"); var _error = _interopRequireDefault(require("./error")); var _mongoose = require("mongoose"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: 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); }); }; } var checkFeatureKey = /*#__PURE__*/function () { var _ref = _asyncToGenerator(function* () { var _app$services; var locale = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _language.EN; var credentialKey = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ""; var coreServiceKey = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ""; var featureKeys = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : []; var errorMessage = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : true; var connection = arguments.length > 5 ? arguments[5] : undefined; var app = yield (0, _AppModel.default)(connection).findOne({ credentials: { $elemMatch: { credentialKey } } }).populate({ path: "services.service" }).populate({ path: "theme", select: "-__v" }); if (!app) throw (0, _error.default)(locale, _application.APP_NOT_FOUND); var service = app === null || app === void 0 ? void 0 : (_app$services = app.services) === null || _app$services === void 0 ? void 0 : _app$services.find(element => { var _element$service; return ((_element$service = element.service) === null || _element$service === void 0 ? void 0 : _element$service.serviceKey) === coreServiceKey; }); if (!service) throw (0, _error.default)(locale, _application.CORE_SERVICE_NOT_FOUND); if (!Array.isArray(featureKeys)) throw new _apolloServer.ApolloError("Invalid featureKey", "INVALID_FEATURE_KEY"); // check Feature var result = {}; featureKeys.forEach(element => { var _service$featureKeys; var checkFeature = (_service$featureKeys = service.featureKeys) === null || _service$featureKeys === void 0 ? void 0 : _service$featureKeys.find(element2 => element2.key === element); if (errorMessage) { if (!checkFeature) throw (0, _error.default)(locale, _application.FEATURE_NOT_FOUND); if (!checkFeature.enable) { throw new _apolloServer.ApolloError(checkFeature.key + _application.FEATURE_NOT_ENABLE.message[locale], _application.FEATURE_NOT_ENABLE.code); } // return true } else if (!checkFeature) result[element] = false;else result[element] = checkFeature.enable; }); return result; }); return function checkFeatureKey() { return _ref.apply(this, arguments); }; }(); exports.checkFeatureKey = checkFeatureKey;