UNPKG

@tomei/sso

Version:
294 lines 15.4 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.UserReportingHierarchy = void 0; const general_1 = require("@tomei/general"); const user_reporting_hierarchy_repository_1 = require("./user-reporting-hierarchy.repository"); const user_1 = require("../login-user/user"); const config_1 = require("@tomei/config"); const activity_history_1 = require("@tomei/activity-history"); const sequelize_1 = require("sequelize"); class UserReportingHierarchy extends general_1.ObjectBase { get UserReportingHierarchyId() { return parseInt(this.ObjectId); } set UserReportingHierarchyId(value) { this.ObjectId = value.toString(); } get CreatedById() { return this._CreatedById; } get CreatedAt() { return this._CreatedAt; } get UpdatedById() { return this._UpdatedById; } get UpdatedAt() { return this._UpdatedAt; } constructor(params) { super(); this.ObjectType = 'UserReportingHierarchy'; this.TableName = 'sso_UserReportingHierarchy'; if (params) { this.ObjectId = params.UserReportingHierarchyId.toString(); this.ReportingUserId = params.ReportingUserId; this.UserId = params.UserId; this.Rank = params.Rank; this.Status = params.Status; this._CreatedById = params.CreatedById; this._CreatedAt = params.CreatedAt; this._UpdatedById = params.UpdatedById; this._UpdatedAt = params.UpdatedAt; } } static init(userReportingHierarchyId, dbTransaction) { return __awaiter(this, void 0, void 0, function* () { try { if (userReportingHierarchyId) { const data = yield UserReportingHierarchy._Repo.findByPk(userReportingHierarchyId.toString(), dbTransaction); if (!data) { throw new general_1.ClassError('UserReportingHierarchy', 'UserReportingHierarchyErrMsg01', 'UserReportingHierarchy not found', 'init', 400); } return new UserReportingHierarchy(data.get({ plain: true })); } return new UserReportingHierarchy(); } catch (error) { throw error; } }); } createUserReportingHierarchy(loginUser, dbTransaction, reportingUserId, userId, rank, status) { return __awaiter(this, void 0, void 0, function* () { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'USER_REPORTING_HIERARCHY_CREATE'); if (!isPrivileged) { throw new general_1.ClassError('UserReportingHierarchy', 'UserReportingHierarchyErrMsg02', 'User does not have the required privileges', 'createUserReportingHierarchy', 403); } yield user_1.User.init(dbTransaction, userId); yield user_1.User.init(dbTransaction, reportingUserId); let userReportingHierarchy = yield UserReportingHierarchy._Repo.findOne({ where: { ReportingUserId: reportingUserId, UserId: userId, }, transaction: dbTransaction, }); if (userReportingHierarchy) { throw new general_1.ClassError('UserReportingHierarchy', 'UserReportingHierarchyErrMsg03', 'Relationship already exists', 'createUserReportingHierarchy', 400); } userReportingHierarchy = yield UserReportingHierarchy._Repo.findOne({ where: { ReportingUserId: reportingUserId, Rank: rank, }, transaction: dbTransaction, }); if (userReportingHierarchy) { throw new general_1.ClassError('UserReportingHierarchy', 'UserReportingHierarchyErrMsg04', 'Rank already exists', 'createUserReportingHierarchy', 400); } if (rank > 1) { const userReportingHierarchyBefore = yield UserReportingHierarchy._Repo.findOne({ where: { ReportingUserId: reportingUserId, Rank: rank - 1, }, transaction: dbTransaction, }); if (!userReportingHierarchyBefore) { throw new general_1.ClassError('UserReportingHierarchy', 'UserReportingHierarchyErrMsg05', 'Rank before the new rank is not yet assigned to the user', 'createUserReportingHierarchy', 400); } } this.ReportingUserId = reportingUserId; this.UserId = userId; this.Rank = rank; this.Status = status; this._CreatedById = loginUser.UserId; this._CreatedAt = new Date(); this._UpdatedAt = new Date(); this._UpdatedById = loginUser.UserId; const entityValueAfter = { ReportingUserId: reportingUserId, UserId: userId, Rank: rank, Status: status, CreatedById: loginUser.UserId, CreatedAt: this._CreatedAt, UpdatedById: loginUser.UserId, UpdatedAt: this._UpdatedAt, }; userReportingHierarchy = yield UserReportingHierarchy._Repo.create(entityValueAfter, { transaction: dbTransaction, }); entityValueAfter.UserReportingHierarchyId = userReportingHierarchy.UserReportingHierarchyId; const activity = new activity_history_1.Activity(); activity.ActivityId = activity.createId(); activity.Action = activity_history_1.ActionEnum.CREATE; activity.Description = 'Create User Reporting Hierarchy'; activity.EntityType = 'UserReportingHierarchy'; activity.EntityId = userReportingHierarchy.UserReportingHierarchyId.toString(); activity.EntityValueBefore = JSON.stringify({}); activity.EntityValueAfter = JSON.stringify(entityValueAfter); yield activity.create(loginUser.ObjectId, dbTransaction); return this; } catch (error) { throw error; } }); } updateUserReportingHierarchy(loginUser, dbTransaction, reportingUserId, userId, rank, status) { return __awaiter(this, void 0, void 0, function* () { try { yield user_1.User.init(dbTransaction, userId); yield user_1.User.init(dbTransaction, reportingUserId); const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'USER_REPORTING_HIERARCHY_UPDATE'); if (!isPrivileged) { throw new general_1.ClassError('UserReportingHierarchy', 'UserReportingHierarchyErrMsg02', 'User does not have the required privileges', 'updateUserReportingHierarchy', 403); } const userReportingHierarchy = yield UserReportingHierarchy._Repo.findOne({ where: { ReportingUserId: reportingUserId, UserId: userId, UserReportingHierarchyId: { [sequelize_1.Op.ne]: this.UserReportingHierarchyId, }, }, transaction: dbTransaction, }); if (userReportingHierarchy) { throw new general_1.ClassError('UserReportingHierarchy', 'UserReportingHierarchyErrMsg03', 'Relationship already exists', 'updateUserReportingHierarchy'); } const userReportingRank = yield UserReportingHierarchy._Repo.findOne({ where: { ReportingUserId: reportingUserId, Rank: rank, UserReportingHierarchyId: { [sequelize_1.Op.ne]: this.UserReportingHierarchyId, }, }, transaction: dbTransaction, }); if (userReportingRank) { throw new general_1.ClassError('UserReportingHierarchy', 'UserReportingHierarchyErrMsg04', 'Rank already exists', 'updateUserReportingHierarchy'); } if (rank > 1) { const userReportingRankBefore = yield UserReportingHierarchy._Repo.findOne({ where: { ReportingUserId: reportingUserId, Rank: rank - 1, UserReportingHierarchyId: { [sequelize_1.Op.ne]: this.UserReportingHierarchyId, }, }, transaction: dbTransaction, }); if (!userReportingRankBefore) { throw new general_1.ClassError('UserReportingHierarchy', 'UserReportingHierarchyErrMsg05', 'Rank before the new rank is not yet assigned to the user', 'updateUserReportingHierarchy'); } } const entityValueBefore = { UserReportingHierarchyId: this.UserReportingHierarchyId, ReportingUserId: this.ReportingUserId, UserId: this.UserId, Rank: this.Rank, Status: this.Status, CreatedById: this._CreatedById, CreatedAt: this._CreatedAt, UpdatedById: this._UpdatedById, UpdatedAt: this._UpdatedAt, }; this.ReportingUserId = reportingUserId; this.UserId = userId; this.Rank = rank; this.Status = status; this._CreatedById = this.CreatedById; this._CreatedAt = this.CreatedAt; this._UpdatedAt = new Date(); this._UpdatedById = loginUser.UserId; const entityValueAfter = { ReportingUserId: reportingUserId, UserId: userId, Rank: rank, Status: status, CreatedById: this.CreatedById, CreatedAt: this._CreatedAt, UpdatedById: loginUser.UserId, UpdatedAt: this._UpdatedAt, }; yield UserReportingHierarchy._Repo.update(entityValueAfter, { where: { UserReportingHierarchyId: this.UserReportingHierarchyId, }, transaction: dbTransaction, }); const activity = new activity_history_1.Activity(); activity.ActivityId = activity.createId(); activity.Action = activity_history_1.ActionEnum.UPDATE; activity.Description = 'Update User Reporting Hierarchy'; activity.EntityType = 'UserReportingHierarchy'; activity.EntityId = this.UserReportingHierarchyId.toString(); activity.EntityValueBefore = JSON.stringify(entityValueBefore); activity.EntityValueAfter = JSON.stringify(entityValueAfter); yield activity.create(loginUser.ObjectId, dbTransaction); return this; } catch (error) { throw error; } }); } static removeUserReportingHierarchy(loginUser, dbTransaction, userReportingHierarchyId) { return __awaiter(this, void 0, void 0, function* () { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'USER_REPORTING_HIERARCHY_REMOVE'); if (!isPrivileged) { throw new general_1.ClassError('UserReportingHierarchy', 'UserReportingHierarchyErrMsg06', 'Insufficient privileges to remove reporting hierarchy', 'removeUserReportingHierarchy', 403); } const userReportingHierarchy = yield UserReportingHierarchy.init(userReportingHierarchyId, dbTransaction); yield UserReportingHierarchy._Repo.destroy(userReportingHierarchyId, dbTransaction); const entityValueBefore = { UserReportingHierarchyId: userReportingHierarchy.UserReportingHierarchyId, ReportingUserId: userReportingHierarchy.ReportingUserId, UserId: userReportingHierarchy.UserId, Rank: userReportingHierarchy.Rank, Status: userReportingHierarchy.Status, CreatedById: userReportingHierarchy.CreatedById, CreatedAt: userReportingHierarchy.CreatedAt, UpdatedById: userReportingHierarchy.UpdatedById, UpdatedAt: userReportingHierarchy.UpdatedAt, }; const activity = new activity_history_1.Activity(); activity.ActivityId = activity.createId(); activity.Action = activity_history_1.ActionEnum.DELETE; activity.Description = 'Remove User Reporting Hierarchy'; activity.EntityType = 'UserReportingHierarchy'; activity.EntityId = userReportingHierarchy.toString(); activity.EntityValueBefore = JSON.stringify(entityValueBefore); activity.EntityValueAfter = JSON.stringify({}); yield activity.create(loginUser.ObjectId, dbTransaction); } catch (error) { throw error; } }); } } exports.UserReportingHierarchy = UserReportingHierarchy; UserReportingHierarchy._Repo = new user_reporting_hierarchy_repository_1.UserReportingHierarchyRepository(); //# sourceMappingURL=user-reporting-hierarchy.js.map