UNPKG

kritsana135-hooray-server

Version:

for server

266 lines (220 loc) 11.1 kB
"use strict"; var _validator = _interopRequireDefault(require("validator")); var _configUser = require("../constants/configUser"); var _application = require("../constants/errors/application"); var _resourceOwner = require("../constants/errors/resourceOwner"); var _utils = require("../constants/errors/utils"); var _featureKeys = require("../constants/featureKeys"); var _language = require("../constants/language"); var _loginType = require("../constants/loginType"); var _message = require("../constants/logsSystem/message"); var _resourceOwnerPermissions = require("../constants/resourceOwnerPermissions"); var _AppModel = _interopRequireDefault(require("../models/AppModel")); var _ResourceOwnerModel = _interopRequireDefault(require("../models/ResourceOwnerModel")); var _ServiceModel = _interopRequireDefault(require("../models/ServiceModel")); var _ThemeModel = _interopRequireDefault(require("../models/ThemeModel")); var _auth = require("./auth"); var _error = _interopRequireDefault(require("./error")); var _sendEmail = require("./sendEmail"); var _validate = require("./validate"); 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 addAttribute = /*#__PURE__*/function () { var _ref = _asyncToGenerator(function* (user, application) { var _application$services; var locale = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _language.EN; var connection = arguments.length > 3 ? arguments[3] : undefined; var coreServiceKey = process.env.SERVICE_KEY || ""; if (!application) throw (0, _error.default)(locale, _application.APP_NOT_FOUND); user.userType = _configUser.USER_TYPE.USER; user.application = application; user.userPermission = { service: null, permissions: [] }; _resourceOwnerPermissions.DEFAULT_PERMISSION.forEach(element => { user.userPermission.permissions.push({ permissionName: element.permissionName, code: element.defaultCode }); }); // console.log("\n==app==\n", application); if (!application.services || application.services.length == 0) { var service = yield (0, _ServiceModel.default)(connection).findOne({ serviceKey: coreServiceKey }); if (!service) throw (0, _error.default)(locale, _application.SERVICE_NOT_FOUND); user.userPermission.service = service; return user; } var findCoreService = application === null || application === void 0 ? void 0 : (_application$services = application.services) === null || _application$services === void 0 ? void 0 : _application$services.find(element => { var _element$service; return ((_element$service = element.service) === null || _element$service === void 0 ? void 0 : _element$service.serviceKey) === coreServiceKey; }); if (!findCoreService) throw (0, _error.default)(locale, _application.CORE_SERVICE_NOT_FOUND); var permission = findCoreService.userPermissions.find(element => element.permissionType === "DEFAULT"); if (permission) user.userPermission.permissions = permission.permissions; user.userPermission.service = findCoreService.service; return user; }); return function addAttribute(_x, _x2) { return _ref.apply(this, arguments); }; }(); var register = /*#__PURE__*/function () { var _ref2 = _asyncToGenerator(function* () { 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 type = arguments.length > 3 ? arguments[3] : undefined; var data = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {}; var connection = arguments.length > 5 ? arguments[5] : undefined; if (!type) throw (0, _error.default)(locale, _resourceOwner.TYPE_LOGIN_NOT_FOUND); console.log(connection ? "have" : "not"); yield (0, _ResourceOwnerModel.default)(connection); yield (0, _ServiceModel.default)(connection); yield (0, _ThemeModel.default)(connection); var app = yield (0, _AppModel.default)(connection).findOne({ credentials: { $elemMatch: { credentialKey } } }).populate({ path: "resourceOwners" }).populate({ path: "services.service", match: { isActive: { $eq: true } } }); if (!app) throw (0, _error.default)(locale, _application.APP_NOT_FOUND); type = type.toUpperCase(); switch (type) { case _loginType.REGISTER.EMAIL: { yield (0, _auth.checkFeatureKey)(locale, credentialKey, coreServiceKey, [_featureKeys.FEATURE_KEYS_PROPERTY.REGISTER], true, connection); if (!data.email) throw (0, _error.default)(locale, _resourceOwner.EMAIL_ADDRESS_NOT_FOUND); if (!data.password) throw (0, _error.default)(locale, _utils.PASSWORD_NOT_FOUND); if (!_validator.default.isEmail(data.email) || _validator.default.isEmpty(data.email)) throw (0, _error.default)(locale, _resourceOwner.INVALID_EMAIL_ADDRESS); return yield createWithEmail(locale, data, app, connection); } case _loginType.REGISTER.USERNAME: { yield (0, _auth.checkFeatureKey)(locale, credentialKey, coreServiceKey, [_featureKeys.FEATURE_KEYS_PROPERTY.REGISTER], true, connection); if (!data.username) throw (0, _error.default)(locale, _resourceOwner.USER_NAME_NOT_FOUND); if (!data.password) throw (0, _error.default)(locale, _utils.PASSWORD_NOT_FOUND); return yield createWithUsername(locale, data, app, connection); } default: yield (0, _error.default)(locale, _resourceOwner.INVALID_REGISTER_TYPE); } return {}; }); return function register() { return _ref2.apply(this, arguments); }; }(); var createWithEmail = /*#__PURE__*/function () { var _ref3 = _asyncToGenerator(function* () { var _app$services, _service$featureKeys; var locale = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _language.EN; var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var app = arguments.length > 2 ? arguments[2] : undefined; var connection = arguments.length > 3 ? arguments[3] : undefined; var { email, password, confirmPassword } = data; data.email = {}; yield (0, _validate.validatePassword)(locale, password, confirmPassword); var user = yield (0, _ResourceOwnerModel.default)(connection).findOne({ "email.value": email, application: app._id }); if (user) throw (0, _error.default)(locale, _utils.EMAIL_ALREADY_EXIST); var coreServiceKey = process.env.SERVICE_KEY || ""; 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$service2; return (element === null || element === void 0 ? void 0 : (_element$service2 = element.service) === null || _element$service2 === void 0 ? void 0 : _element$service2.serviceKey) === coreServiceKey; }); if (!service) throw (0, _error.default)(locale, _application.CORE_SERVICE_NOT_FOUND); var checkFeature = (_service$featureKeys = service.featureKeys) === null || _service$featureKeys === void 0 ? void 0 : _service$featureKeys.find(element => (element === null || element === void 0 ? void 0 : element.key) === _featureKeys.FEATURE_KEYS_PROPERTY.CONFIRM_EMAIL); if (checkFeature && checkFeature.enable) { // Send verify var verifyToken = yield (0, _sendEmail.sendToVerifyEmail)(locale, email, app); data.verifyTokens = []; data.verifyTokens.push(verifyToken); data.email.verifyStatus = "NO"; } data.email.value = email; data = yield addAttribute(data, app, locale, connection); delete data.credential; var resourceOwner = new _ResourceOwnerModel.default(connection)(data); yield resourceOwner.save().then(result => result.populate({ path: "application", select: "_id name" }).populate({ path: "services.service", select: "_id name name logo graphqlEndpointAdmin" }).populate({ path: "userPermission.service", select: "_id name logo graphqlEndpointAdmin" })); // let prevData = { ...app }; app.resourceOwners.push(resourceOwner); yield app.save(); resourceOwner.logMessage = _message.CREATE_RESOURCE_SUCCESS; return resourceOwner; }); return function createWithEmail() { return _ref3.apply(this, arguments); }; }(); var createWithUsername = /*#__PURE__*/function () { var _ref4 = _asyncToGenerator(function* () { var locale = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _language.EN; var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var app = arguments.length > 2 ? arguments[2] : undefined; var connection = arguments.length > 3 ? arguments[3] : undefined; var { username, password, confirmPassword } = data; data.username = {}; yield (0, _validate.validatePassword)(locale, password, confirmPassword); var user = yield (0, _ResourceOwnerModel.default)(connection).findOne({ "username.value": username, application: app._id }); if (user) throw (0, _error.default)(locale, _resourceOwner.USERNAME_ALREADY_EXIST); data.username.value = username; data = yield addAttribute(data, app, locale, connection); delete data.credential; var resourceOwner = new _ResourceOwnerModel.default(connection)(data); yield resourceOwner.save().then(result => result.populate({ path: "application", select: "_id name" }).populate({ path: "services.service", select: "_id name name logo graphqlEndpointAdmin" }).populate({ path: "userPermission.service", select: "_id name logo graphqlEndpointAdmin" })); // let prevData = { ...app }; app.resourceOwners.push(resourceOwner); yield app.save(); resourceOwner.logMessage = _message.CREATE_RESOURCE_SUCCESS; return resourceOwner; }); return function createWithUsername() { return _ref4.apply(this, arguments); }; }(); module.exports = { // addAttribute, register };