UNPKG

@di-zed/yandex-smart-home

Version:

The Yandex Smart Home skills for the different device types.

128 lines (127 loc) 5.51 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /** * @author DiZed Team * @copyright Copyright (c) DiZed Team (https://github.com/di-zed/) */ const i18n_1 = __importDefault(require("i18n")); const fs_1 = __importDefault(require("fs")); const configProvider_1 = __importDefault(require("../providers/configProvider")); /** * User Repository. */ class UserRepository { /** * Get User by ID. * * @param userId * @returns Promise<UserInterface> */ getUserById(userId) { return __awaiter(this, void 0, void 0, function* () { const functionGetUserById = configProvider_1.default.getConfigOption('functionGetUserById'); if (typeof functionGetUserById === 'function') { return (yield functionGetUserById(userId)); } const configUsers = yield this.getConfigUsers(); return new Promise((resolve, reject) => { const user = configUsers.find((element) => String(element.id) === String(userId)); if (user) { const result = JSON.parse(JSON.stringify(user)); delete result.password; return resolve(result); } return reject(i18n_1.default.__('A user with ID #%s does not exist.', String(userId))); }); }); } /** * Get User by Email and Password. * * @param email * @param password * @returns Promise<UserInterface> */ getUserByEmailAndPassword(email, password) { return __awaiter(this, void 0, void 0, function* () { const functionGetUserByEmailAndPassword = configProvider_1.default.getConfigOption('functionGetUserByEmailAndPassword'); if (typeof functionGetUserByEmailAndPassword === 'function') { return (yield functionGetUserByEmailAndPassword(email, password)); } const configUsers = yield this.getConfigUsers(); return new Promise((resolve, reject) => { const user = configUsers.find((element) => { return element.email.toLowerCase() === email.toLowerCase() && element.password === password; }); if (user) { const result = JSON.parse(JSON.stringify(user)); delete result.password; return resolve(result); } return reject(i18n_1.default.__('A user with email "%s" does not exist.', email)); }); }); } /** * Get User by Name or Email. * * @param nameOrEmail * @returns Promise<UserInterface> */ getUserByNameOrEmail(nameOrEmail) { return __awaiter(this, void 0, void 0, function* () { const functionGetUserByNameOrEmail = configProvider_1.default.getConfigOption('functionGetUserByNameOrEmail'); if (typeof functionGetUserByNameOrEmail === 'function') { return (yield functionGetUserByNameOrEmail(nameOrEmail)); } const configUsers = yield this.getConfigUsers(); return new Promise((resolve, reject) => { const user = configUsers.find((element) => { if (nameOrEmail.search('@') !== -1) { return element.email.toLowerCase() === nameOrEmail.toLowerCase(); } return element.fullName === nameOrEmail; }); if (user) { const result = JSON.parse(JSON.stringify(user)); delete result.password; return resolve(result); } return reject(i18n_1.default.__('A user with name/email "%s" does not exist.', nameOrEmail)); }); }); } /** * Get Users from the Configuration file. * * @returns Promise<UserInterface[]> */ getConfigUsers() { return __awaiter(this, void 0, void 0, function* () { try { if (this.configUsers === undefined) { const configFileUsers = configProvider_1.default.getConfigOption('configFileUsers'); const filePath = configFileUsers ? configFileUsers : `${__dirname}/../../config/users.json`; this.configUsers = yield JSON.parse(fs_1.default.readFileSync(filePath, { encoding: 'utf8', flag: 'r' })); } return this.configUsers; } catch (err) { return []; } }); } } exports.default = new UserRepository();