UNPKG

@briswell/bw-domain

Version:

Domain Library for Node.js

144 lines (143 loc) 5.47 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { 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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const crypto = require("crypto"); const Sequelize = require("sequelize"); const __1 = require(".."); const user_1 = require("../model/user"); const common_1 = require("../utils/common"); class UserRepository { constructor(db, secretKey = 'hash_secret') { this.userModel = user_1.default(db); this.secret = secretKey; } /** * ログイン * @param params ログイン情報 */ login(params) { return __awaiter(this, void 0, void 0, function* () { const userInfo = yield this.userModel.findOne({ where: { email: params.email, pass: crypto.createHmac('sha256', this.secret).update(params.pass).digest('hex'), }, attributes: { exclude: ['pass'] // パスワード非表示 } }); if (userInfo !== null) { userInfo.set('lastLoginDate', new Date()); yield userInfo.save(); } return userInfo; }); } /** * 登録 * @param params 登録情報 */ register(params) { return __awaiter(this, void 0, void 0, function* () { const existUser = yield this.userModel.count({ where: { email: params.email } }); if (existUser === 0) { const newUser = yield this.userModel.create({ email: params.email, pass: crypto.createHmac('sha256', this.secret).update(params.pass).digest('hex'), userFlag: params.userFlag, createdBy: params.userId, updatedBy: params.userId }); return this.userModel.findById(newUser.id); } else { throw new __1.factory.errors.AlreadyInUse('user', ['email']); } }); } /** * 編集 * @param params 編集情報 */ update(params) { return __awaiter(this, void 0, void 0, function* () { const existUser = yield this.userModel.count({ where: { email: params.email } }); if (existUser === 0) { if (params.pass !== undefined) { yield this.userModel.update({ email: params.email, userFlag: params.userFlag, pass: crypto.createHmac('sha256', this.secret).update(params.pass).digest('hex') }, { where: { id: params.id } }); } else { yield this.userModel.update({ email: params.email, userFlag: params.userFlag }, { where: { id: params.id } }); } return this.userModel.findById(params.id); } else { throw new __1.factory.errors.AlreadyInUse('user', ['email']); } }); } search(params) { return __awaiter(this, void 0, void 0, function* () { const findOptions = common_1.stripUndefinedField(params); if (params.sort !== undefined) { findOptions.order = [['id', params.sort]]; } if (params.limit !== undefined) { findOptions.limit = params.limit; } findOptions.attributes = { exclude: ['pass'] // パスワード非表示 }; findOptions.where = Object.assign({}, findOptions.where, { userFlag: { [Sequelize.Op.not]: 1 } }); if (findOptions.where.email !== undefined) { findOptions.where.email = { [Sequelize.Op.like]: `%${findOptions.where.email}%` }; } return this.userModel.findAndCount(findOptions); }); } registerFaceId(params) { return __awaiter(this, void 0, void 0, function* () { const u = yield this.userModel.findById(params.userId); if (u === null) { throw new __1.factory.errors.NotFound('user', `user with id = ${params.userId} is not found`); } else { if (u.recognitionId !== null) { yield params.deleteExistingFaceId(u.recognitionId); } u.recognitionId = params.faceId; u.expirationDate = params.expirationDate; yield u.save(); } }); } } exports.default = UserRepository;