@tomei/sso
Version:
Tomei SSO Package
301 lines • 13.6 kB
JavaScript
"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.System = void 0;
const general_1 = require("@tomei/general");
const system_repository_1 = require("./system.repository");
const config_1 = require("@tomei/config");
const activity_history_1 = require("@tomei/activity-history");
const sequelize_1 = require("sequelize");
const uuid_1 = require("uuid");
class System 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(systemAttr) {
super();
this.TableName = 'sso_System';
this.ObjectType = 'System';
if (systemAttr) {
this.SystemCode = systemAttr.SystemCode;
this.Name = systemAttr.Name;
this.Description = systemAttr === null || systemAttr === void 0 ? void 0 : systemAttr.Description;
this.AccessURL = systemAttr === null || systemAttr === void 0 ? void 0 : systemAttr.AccessURL;
this.GooglePlayURL = systemAttr === null || systemAttr === void 0 ? void 0 : systemAttr.GooglePlayURL;
this.AppleStoreURL = systemAttr === null || systemAttr === void 0 ? void 0 : systemAttr.AppleStoreURL;
this.APIKey = systemAttr === null || systemAttr === void 0 ? void 0 : systemAttr.APIKey;
this.APISecret = systemAttr === null || systemAttr === void 0 ? void 0 : systemAttr.APISecret;
this.Status = systemAttr === null || systemAttr === void 0 ? void 0 : systemAttr.Status;
this._CreatedById = systemAttr.CreatedById;
this._CreatedAt = systemAttr.CreatedAt;
this._UpdatedById = systemAttr.UpdatedById;
this._UpdatedAt = systemAttr.UpdatedAt;
}
}
static init(dbTransaction, SystemCode) {
return __awaiter(this, void 0, void 0, function* () {
try {
if (SystemCode) {
const system = yield System._Repo.findByPk(SystemCode, {
transaction: dbTransaction,
});
if (system) {
return new System(system);
}
else {
throw new general_1.ClassError('System', 'SystemErrMsg00', 'System Not Found');
}
}
return new System();
}
catch (error) {
throw new general_1.ClassError('System', 'SystemErrMsg01', 'Failed To Initialize System');
}
});
}
createSystem(loginUser, dbTransaction) {
return __awaiter(this, void 0, void 0, function* () {
try {
const systemCode = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = yield loginUser.checkPrivileges(systemCode, 'System - Create');
if (!isPrivileged) {
throw new Error('You do not have permission to list UserGroup.');
}
if (!this.SystemCode) {
throw new general_1.ClassError('System', 'SystemErrMsg02', 'SystemCode must have value.');
}
if (!this.Name) {
throw new general_1.ClassError('System', 'SystemErrMsg03', 'Name must have value.');
}
if (!this.Description) {
throw new general_1.ClassError('System', 'SystemErrMsg04', 'Description must have value.');
}
this._CreatedById = loginUser.UserId;
this._CreatedAt = new Date();
this._UpdatedById = loginUser.UserId;
this._UpdatedAt = new Date();
yield System._Repo.create({
SystemCode: this.SystemCode,
Name: this.Name,
Description: this.Description,
AccessURL: this.AccessURL,
GooglePlayURL: this.GooglePlayURL,
AppleStoreURL: this.AppleStoreURL,
APIKey: this.APIKey,
APISecret: this.APISecret,
Status: this.Status,
CreatedById: this._CreatedById,
CreatedAt: this._CreatedAt,
UpdatedById: this._UpdatedById,
UpdatedAt: this._UpdatedAt,
}, {
transaction: dbTransaction,
});
const entityValueAfter = {
SystemCode: this.SystemCode,
Name: this.Name,
Description: this.Description,
AccessURL: this.AccessURL,
GooglePlayURL: this.GooglePlayURL,
AppleStoreURL: this.AppleStoreURL,
APIKey: this.APIKey,
APISecret: this.APISecret,
Status: this.Status,
};
const activity = new activity_history_1.Activity();
activity.ActivityId = activity.createId();
activity.Action = activity_history_1.ActionEnum.CREATE;
activity.Description = 'Add System';
activity.EntityType = 'System';
activity.EntityId = this.SystemCode;
activity.EntityValueBefore = JSON.stringify({});
activity.EntityValueAfter = JSON.stringify(entityValueAfter);
}
catch (error) {
throw error;
}
});
}
static checkDuplicateSystemCode(dbTransaction, systemCode) {
return __awaiter(this, void 0, void 0, function* () {
try {
const system = yield System._Repo.findOne({
where: {
SystemCode: systemCode,
},
transaction: dbTransaction,
});
if (system) {
throw new general_1.ClassError('System', 'SystemErrMsg05', 'System Code already exists.');
}
}
catch (error) {
throw error;
}
});
}
setSystemCode(dbTransaction, systemCode) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield System.checkDuplicateSystemCode(dbTransaction, systemCode);
this.SystemCode = systemCode;
}
catch (error) {
throw error;
}
});
}
static findAll(dbTransaction, loginUser, page, rows, search) {
return __awaiter(this, void 0, void 0, function* () {
try {
const queryObj = {};
const whereObj = {};
if (search) {
Object.entries(search).forEach(([key, value]) => {
if (value) {
queryObj[key] = {
[sequelize_1.Op.substring]: value,
};
}
});
}
if (page && rows) {
whereObj.offset = (page - 1) * rows;
whereObj.limit = rows;
}
const result = yield System._Repo.findAllWithPagination(Object.assign(Object.assign({ distinct: true, where: queryObj }, whereObj), { order: [['CreatedAt', 'DESC']], transaction: dbTransaction }));
const systems = result.rows.map((system) => new System(system));
return { count: result.count, systems };
}
catch (error) {
throw error;
}
});
}
static renewApiKeyAndSecret(loginUser, dbTransaction, systemCode) {
return __awaiter(this, void 0, void 0, function* () {
try {
const sc = config_1.ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = yield loginUser.checkPrivileges(sc, 'SYSTEM_UPDATE');
if (!isPrivileged) {
throw new general_1.ClassError('System', 'SystemErrMsg06', 'You do not have permission to renew API Key and Secret.');
}
const system = yield System.init(dbTransaction, systemCode);
if (!system.AccessURL) {
throw new general_1.ClassError('System', 'SystemErrMsg07', 'AccessURL is required for callback');
}
if (system.Status !== 'Active') {
throw new general_1.ClassError('System', 'SystemErrMsg08', 'Cannot do this operation on inactive system.');
}
const entityValueBefore = {
SystemCode: system.SystemCode,
Name: system.Name,
Description: system.Description,
AccessURL: system.AccessURL,
GooglePlayURL: system.GooglePlayURL,
AppleStoreURL: system.AppleStoreURL,
APIKey: system.APIKey,
APISecret: system.APISecret,
Status: system.Status,
};
const apiKey = (0, uuid_1.v4)();
const apiSecret = (0, uuid_1.v4)();
system.APIKey = apiKey;
system.APISecret = apiSecret;
system._UpdatedById = loginUser.UserId;
system._UpdatedAt = new Date();
yield System._Repo.update({
APIKey: apiKey,
APISecret: apiSecret,
UpdatedById: system._UpdatedById,
UpdatedAt: system._UpdatedAt,
}, {
where: {
SystemCode: systemCode,
},
transaction: dbTransaction,
});
const entityValueAfter = {
SystemCode: system.SystemCode,
Name: system.Name,
Description: system.Description,
AccessURL: system.AccessURL,
GooglePlayURL: system.GooglePlayURL,
AppleStoreURL: system.AppleStoreURL,
APIKey: system.APIKey,
APISecret: system.APISecret,
Status: system.Status,
};
const activity = new activity_history_1.Activity();
activity.ActivityId = activity.createId();
activity.Action = activity_history_1.ActionEnum.UPDATE;
activity.Description = 'Renew API key and secret for a system';
activity.EntityType = 'System';
activity.EntityId = system.SystemCode;
activity.EntityValueBefore = JSON.stringify(entityValueBefore);
activity.EntityValueAfter = JSON.stringify(entityValueAfter);
yield activity.create(loginUser.ObjectId, dbTransaction);
return system;
}
catch (error) {
throw error;
}
});
}
static loadSystem(dbTransaction) {
return __awaiter(this, void 0, void 0, function* () {
try {
config_1.ComponentConfig.loadComponentConfig('./component-config/sso-config.json');
const config = config_1.ComponentConfig.getComponentConfigValue('@tomei/sso', 'system');
if (!config.name ||
!config.code ||
!config.description ||
!config.userId) {
throw new Error('Missing required fields in the config file.');
}
const system = yield System._Repo.findByPk(config.code, {
transaction: dbTransaction,
});
if (system) {
return 'System loaded.';
}
yield System._Repo.create({
SystemCode: config.code,
Name: config.name,
Description: config.description,
Status: 'Active',
CreatedById: config.userId,
CreatedAt: new Date(),
UpdatedById: config.userId,
UpdatedAt: new Date(),
}, {
transaction: dbTransaction,
});
return 'System loaded.';
}
catch (error) {
throw error;
}
});
}
}
exports.System = System;
System._Repo = new system_repository_1.SystemRepository();
//# sourceMappingURL=system.js.map