UNPKG

@tomei/sso

Version:
293 lines 15 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.GroupReportingUser = void 0; const sequelize_1 = require("sequelize"); const general_1 = require("@tomei/general"); const group_reporting_user_repository_1 = require("./group-reporting-user.repository"); const user_1 = require("../login-user/user"); const user_entity_1 = require("../../models/user.entity"); const group_1 = require("../group/group"); const config_1 = require("@tomei/config"); const activity_history_1 = require("@tomei/activity-history"); class GroupReportingUser extends general_1.ObjectBase { get CreatedById() { return this._CreatedById; } get CreatedAt() { return this._CreatedAt; } get UpdatedById() { return this._UpdatedById; } get UpdatedAt() { return this._UpdatedAt; } constructor(groupReportingUserAttr) { super(); this.TableName = 'sso_GroupReportingUser'; this.ObjectType = 'GroupReportingUser'; if (groupReportingUserAttr) { this.GroupReportingUserId = groupReportingUserAttr.GroupReportingUserId; this.GroupCode = groupReportingUserAttr.GroupCode; this.UserId = groupReportingUserAttr === null || groupReportingUserAttr === void 0 ? void 0 : groupReportingUserAttr.UserId; this.Rank = groupReportingUserAttr === null || groupReportingUserAttr === void 0 ? void 0 : groupReportingUserAttr.Rank; this.Status = groupReportingUserAttr === null || groupReportingUserAttr === void 0 ? void 0 : groupReportingUserAttr.Status; this._CreatedById = groupReportingUserAttr.CreatedById; this._CreatedAt = groupReportingUserAttr.CreatedAt; this._UpdatedById = groupReportingUserAttr.UpdatedById; this._UpdatedAt = groupReportingUserAttr.UpdatedAt; } } static init(dbTransaction, GroupReportingUserId) { return __awaiter(this, void 0, void 0, function* () { try { if (GroupReportingUserId) { const groupReportingUser = yield GroupReportingUser._Repo.findByPk(GroupReportingUserId, { transaction: dbTransaction, }); if (groupReportingUser) { return new GroupReportingUser(groupReportingUser); } else { throw new general_1.ClassError('GroupReportingUser', 'GroupReportingUserErrMsg00', 'GroupReportingUser Not Found', 'init', 404); } } return new GroupReportingUser(); } catch (error) { throw error; } }); } createGroupReportingUser(loginUser, dbTransaction, groupCode, userId, rank, status) { return __awaiter(this, void 0, void 0, function* () { try { const group = yield group_1.Group.init(dbTransaction, groupCode); const user = yield user_1.User.init(dbTransaction, userId); const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'GROUP_REPORTING_USER_CREATE'); if (!isPrivileged) { throw new general_1.ClassError('GroupReportingUser', 'GroupReportingUserErrMsg02', 'Insufficient privileges to add a user to the group'); } const groupReportingUser = yield GroupReportingUser._Repo.findOne({ where: { GroupCode: groupCode, UserId: userId, }, transaction: dbTransaction, }); if (groupReportingUser) { throw new general_1.ClassError('GroupReportingUser', 'GroupReportingUserErrMsg03', 'User already exists in the group', 'createGroupReportingUser'); } const groupReportingUserRank = yield GroupReportingUser._Repo.findOne({ where: { GroupCode: groupCode, Rank: rank, }, transaction: dbTransaction, }); if (groupReportingUserRank) { throw new general_1.ClassError('GroupReportingUser', 'GroupReportingUserErrMsg04', 'Rank already exists in the group', 'createGroupReportingUser'); } this.GroupCode = groupCode; 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 = { GroupCode: groupCode, UserId: userId, Rank: rank, Status: status, CreatedById: loginUser.UserId, CreatedAt: this._CreatedAt, UpdatedById: loginUser.UserId, UpdatedAt: this._UpdatedAt, }; const newGroupReportingUser = yield GroupReportingUser._Repo.create(entityValueAfter, { transaction: dbTransaction, }); entityValueAfter.GroupReportingUserId = newGroupReportingUser.GroupReportingUserId; const activity = new activity_history_1.Activity(); activity.ActivityId = activity.createId(); activity.Action = activity_history_1.ActionEnum.CREATE; activity.Description = 'Create Group Reporting User'; activity.EntityType = 'GroupReportingUser'; activity.EntityId = newGroupReportingUser.GroupReportingUserId.toString(); activity.EntityValueBefore = JSON.stringify({}); activity.EntityValueAfter = JSON.stringify(entityValueAfter); yield activity.create(loginUser.ObjectId, dbTransaction); return this; } catch (error) { throw error; } }); } updateGroupReportingUser(loginUser, dbTransaction, groupCode, userId, rank, status) { return __awaiter(this, void 0, void 0, function* () { try { yield group_1.Group.init(dbTransaction, groupCode); yield user_1.User.init(dbTransaction, userId); const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'GROUP_REPORTING_USER_UPDATE'); if (!isPrivileged) { throw new general_1.ClassError('GroupReportingUser', 'GroupReportingUserErrMsg02', 'Insufficient privileges to update a user to the group'); } const currentGroupReportingUser = yield GroupReportingUser._Repo.findOne({ where: { GroupCode: groupCode, GroupReportingUserId: this.GroupReportingUserId, }, transaction: dbTransaction, }); const groupReportingUser = yield GroupReportingUser._Repo.findOne({ where: { GroupCode: groupCode, UserId: userId, GroupReportingUserId: { [sequelize_1.Op.ne]: this.GroupReportingUserId, }, }, transaction: dbTransaction, }); if (groupReportingUser) { throw new general_1.ClassError('GroupReportingUser', 'GroupReportingUserErrMsg03', 'User already exists in the group', 'updateGroupReportingUser'); } const groupReportingUserRank = yield GroupReportingUser._Repo.findOne({ where: { GroupCode: groupCode, Rank: rank, GroupReportingUserId: { [sequelize_1.Op.ne]: this.GroupReportingUserId, }, }, transaction: dbTransaction, }); if (groupReportingUserRank) { throw new general_1.ClassError('GroupReportingUser', 'GroupReportingUserErrMsg04', 'Rank already exists in the group', 'updateGroupReportingUser'); } this.GroupCode = groupCode; this.UserId = userId; this.Rank = rank; this.Status = status; this._CreatedById = currentGroupReportingUser.CreatedById; this._CreatedAt = currentGroupReportingUser.CreatedAt; this._UpdatedAt = new Date(); this._UpdatedById = loginUser.UserId; const entityValueAfter = { GroupCode: groupCode, UserId: userId, Rank: rank, Status: status, CreatedById: currentGroupReportingUser.CreatedById, CreatedAt: this._CreatedAt, UpdatedById: loginUser.UserId, UpdatedAt: this._UpdatedAt, }; yield GroupReportingUser._Repo.update(entityValueAfter, { where: { GroupReportingUserId: this.GroupReportingUserId, }, transaction: dbTransaction, }); const activity = new activity_history_1.Activity(); activity.ActivityId = activity.createId(); activity.Action = activity_history_1.ActionEnum.UPDATE; activity.Description = 'Update Group Reporting User'; activity.EntityType = 'GroupReportingUser'; activity.EntityId = this.GroupReportingUserId.toString(); activity.EntityValueBefore = JSON.stringify({}); activity.EntityValueAfter = JSON.stringify(entityValueAfter); yield activity.create(loginUser.ObjectId, dbTransaction); return this; } catch (error) { throw error; } }); } static findAllGroupReportingUsers(loginUser, dbTransaction, groupCode) { return __awaiter(this, void 0, void 0, function* () { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'GROUP_REPORTING_USER_VIEW'); if (!isPrivileged) { throw new general_1.ClassError('Group', 'GroupReportingUserErrMsg05', 'You do not have the privilege to view group reporting user'); } yield group_1.Group.init(dbTransaction, groupCode); const result = yield GroupReportingUser._Repo.findAll({ where: { GroupCode: groupCode, }, include: [ { model: user_entity_1.default, as: 'User', }, ], order: [ ['Rank', 'ASC'], ], transaction: dbTransaction, }); return result; } catch (error) { throw error; } }); } static removeGroupReportingUser(loginUser, dbTransaction, groupReportingUserId) { return __awaiter(this, void 0, void 0, function* () { try { const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code'); const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'GROUP_REPORTING_USER_REMOVE'); if (!isPrivileged) { throw new general_1.ClassError('GroupReportingUser', 'GroupReportingUserErrMsg06', 'Insufficient privileges to remove a user from the group', 'removeGroupReportingUser', 403); } const groupReportingUser = yield GroupReportingUser.init(dbTransaction, groupReportingUserId.toString()); yield GroupReportingUser._Repo.destroy(groupReportingUserId, dbTransaction); const entityValueBefore = { GroupReportingUserId: groupReportingUser.GroupReportingUserId, GroupCode: groupReportingUser.GroupCode, UserId: groupReportingUser.UserId, Rank: groupReportingUser.Rank, Status: groupReportingUser.Status, CreatedById: groupReportingUser.CreatedById, CreatedAt: groupReportingUser.CreatedAt, UpdatedById: groupReportingUser.UpdatedById, UpdatedAt: groupReportingUser.UpdatedAt, }; const activity = new activity_history_1.Activity(); activity.ActivityId = activity.createId(); activity.Action = activity_history_1.ActionEnum.DELETE; activity.Description = 'Remove Group Reporting User'; activity.EntityType = 'GroupReportingUser'; activity.EntityId = groupReportingUserId.toString(); activity.EntityValueBefore = JSON.stringify(entityValueBefore); activity.EntityValueAfter = JSON.stringify({}); yield activity.create(loginUser.ObjectId, dbTransaction); } catch (error) { throw error; } }); } } exports.GroupReportingUser = GroupReportingUser; GroupReportingUser._Repo = new group_reporting_user_repository_1.GroupReportingUserRepository(); //# sourceMappingURL=group-reporting-user.js.map