UNPKG

@tomei/sso

Version:
88 lines 5.1 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LoginHistory = void 0; const general_1 = require("@tomei/general"); const login_history_repository_1 = require("./login-history.repository"); const config_1 = require("@tomei/config"); const sequelize_1 = require("sequelize"); const user_entity_1 = require("../../models/user.entity"); class LoginHistory extends general_1.ObjectBase { get CreatedAt() { return this._CreatedAt; } constructor(loginHistoryAttr) { var _a; super(); this.TableName = 'sso_LoginHistory'; this.ObjectType = 'Login History'; if (loginHistoryAttr) { this.HistoryId = loginHistoryAttr === null || loginHistoryAttr === void 0 ? void 0 : loginHistoryAttr.HistoryId; this.UserId = loginHistoryAttr === null || loginHistoryAttr === void 0 ? void 0 : loginHistoryAttr.UserId; this.UserName = (_a = loginHistoryAttr === null || loginHistoryAttr === void 0 ? void 0 : loginHistoryAttr.User) === null || _a === void 0 ? void 0 : _a.UserName; this.OriginIP = loginHistoryAttr === null || loginHistoryAttr === void 0 ? void 0 : loginHistoryAttr.OriginIp; this.SystemCode = loginHistoryAttr === null || loginHistoryAttr === void 0 ? void 0 : loginHistoryAttr.SystemCode; this.LoginStatus = loginHistoryAttr === null || loginHistoryAttr === void 0 ? void 0 : loginHistoryAttr.LoginStatus; this._CreatedAt = loginHistoryAttr === null || loginHistoryAttr === void 0 ? void 0 : loginHistoryAttr.CreatedAt; } } static findAll(dbTransaction, loginUser, page, rows, search) { return __awaiter(this, void 0, void 0, function* () { try { const sc = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = yield loginUser.checkPrivileges(sc, 'LOGIN_HISTORY_REPORT'); if (!isPrivileged) { throw new general_1.ClassError('System', 'SystemErrMsg06', 'You do not have permission to list login histories.'); } const queryObj = {}; const whereObj = {}; if (search) { Object.entries(search).forEach(([key, value]) => { if (value !== undefined && value !== null) { if (key === 'DateFrom' || key === 'DateTo') { return; } if (key === 'UserId') { queryObj[key] = value; return; } queryObj[key] = { [sequelize_1.Op.substring]: value, }; } }); } if (page && rows) { whereObj.offset = (page - 1) * rows; whereObj.limit = rows; } if ((search === null || search === void 0 ? void 0 : search.DateFrom) || (search === null || search === void 0 ? void 0 : search.DateTo)) { queryObj.createdAt = Object.assign(Object.assign({}, ((search === null || search === void 0 ? void 0 : search.DateFrom) && { [sequelize_1.Op.gte]: search.DateFrom })), ((search === null || search === void 0 ? void 0 : search.DateTo) && { [sequelize_1.Op.lte]: search.DateTo })); } const result = yield LoginHistory._Repo.findAllWithPagination(Object.assign(Object.assign({ distinct: true, where: queryObj }, whereObj), { order: [['CreatedAt', 'DESC']], include: [ { model: user_entity_1.default, required: false, attributes: ['UserName'], }, ], transaction: dbTransaction })); const loginHistory = result.rows.map((data) => new LoginHistory(data)); return { count: result.count, loginHistory }; } catch (error) { throw error; } }); } } exports.LoginHistory = LoginHistory; LoginHistory._Repo = new login_history_repository_1.LoginHistoryRepository(); //# sourceMappingURL=login-history.js.map