Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | 1x 1x 1x 1x 1x 1x 19x 19x 1x 19x 19x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 4x 4x 2x 2x 1x 1x 2x 1x 6x 6x 6x 6x 1x 5x 1x 4x 1x 3x 1x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 3x 3x 2x 1x 2x 3x 3x 1x 2x 2x 2x 2x 2x 1x 1x 1x 2x 1x 1x 2x 2x 2x | import { ClassError, HashTable, ObjectBase } from '@tomei/general';
import { SystemRepository } from './system.repository';
import { ISystemAttr } from '../../interfaces/system.interface';
import { LoginUser } from '../login-user/login-user';
import { ApplicationConfig } from '@tomei/config';
import { ActionEnum, Activity } from '@tomei/activity-history';
import { ISystemSearchAttr } from '../../interfaces/system-search-attr.interface';
import { Op } from 'sequelize';
export class System extends ObjectBase {
ObjectId: string;
ObjectName: string;
TableName = 'sso_System';
ObjectType = 'System';
private static _htSystem: HashTable;
SystemCode: string;
Name: string;
Description: string;
AccessURL: string;
GooglePlayURL: string;
AppleStoreURL: string;
APIKey: string;
APISecret: string;
Status: string;
private _CreatedById: number;
private _CreatedAt: Date;
private _UpdatedById: number;
private _UpdatedAt: Date;
private static _Repo = new SystemRepository();
get CreatedById(): number {
return this._CreatedById;
}
get CreatedAt(): Date {
return this._CreatedAt;
}
get UpdatedById(): number {
return this._UpdatedById;
}
get UpdatedAt(): Date {
return this._UpdatedAt;
}
private constructor(systemAttr?: ISystemAttr) {
super();
if (systemAttr) {
this.SystemCode = systemAttr.SystemCode;
this.Name = systemAttr.Name;
this.Description = systemAttr?.Description;
this.AccessURL = systemAttr?.AccessURL;
this.GooglePlayURL = systemAttr?.GooglePlayURL;
this.AppleStoreURL = systemAttr?.AppleStoreURL;
this.APIKey = systemAttr?.APIKey;
this.APISecret = systemAttr?.APISecret;
this.Status = systemAttr?.Status;
this._CreatedById = systemAttr.CreatedById;
this._CreatedAt = systemAttr.CreatedAt;
this._UpdatedById = systemAttr.UpdatedById;
this._UpdatedAt = systemAttr.UpdatedAt;
}
}
public static async init(dbTransaction: any, SystemCode?: string) {
try {
if (SystemCode) {
const system = await System._Repo.findByPk(SystemCode, {
transaction: dbTransaction,
});
if (system) {
return new System(system);
} else {
throw new ClassError('System', 'SystemErrMsg00', 'System Not Found');
}
}
return new System();
} catch (error) {
throw new ClassError(
'System',
'SystemErrMsg01',
'Failed To Initialize System',
);
}
}
public async createSystem(
loginUser: LoginUser,
dbTransaction: any,
): Promise<void> {
try {
//Creates a new system in the database.
//Part 1: Check Privilege
//Call loginUser.checkPrivilege() method to check if the user has the privilege to create a system.
const systemCode =
ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = await loginUser.checkPrivileges(
systemCode,
'System - Create',
);
if (!isPrivileged) {
throw new Error('You do not have permission to list UserGroup.');
}
//Part 2: Validate Input Params
//If this._SystemCode is missing, throw ClassError
if (!this.SystemCode) {
throw new ClassError(
'System',
'SystemErrMsg02',
'SystemCode must have value.',
);
}
//If this._Name missing, throw ClassError
if (!this.Name) {
throw new ClassError(
'System',
'SystemErrMsg03',
'Name must have value.',
);
}
//If this._Description missing, throw ClassError
if (!this.Description) {
throw new ClassError(
'System',
'SystemErrMsg04',
'Description must have value.',
);
}
//Part 3: Prepare to Insert Data
//Set private properties
this._CreatedById = loginUser.UserId;
this._CreatedAt = new Date();
this._UpdatedById = loginUser.UserId;
this._UpdatedAt = new Date();
//Call System._Repo create method
await 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,
},
);
//Part 4: Record Create System Activity
//Initialise EntityValueAfter variable and set to this class.
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,
};
//Instantiate new activity from Activity class, call createId() method, then set the properties.
const activity = new Activity();
activity.ActivityId = activity.createId();
activity.Action = ActionEnum.ADD;
activity.Description = 'Add System';
activity.EntityType = 'System';
activity.EntityId = this.SystemCode;
activity.EntityValueBefore = JSON.stringify({});
activity.EntityValueAfter = JSON.stringify(entityValueAfter);
} catch (error) {
throw error;
}
}
protected static async checkDuplicateSystemCode(
dbTransaction: any,
systemCode: string,
): Promise<void> {
//This method will make sure there is no duplicate system code.
try {
//Call System._Repo findOne() method by passing Params.SystemCode and dbTransaction, if SystemCode already exists, throw ClassError.
const system = await System._Repo.findOne({
where: {
SystemCode: systemCode,
},
transaction: dbTransaction,
});
if (system) {
throw new ClassError(
'System',
'SystemErrMsg05',
'System Code already exists.',
);
}
} catch (error) {
throw error;
}
}
public async setSystemCode(
dbTransaction: any,
systemCode: string,
): Promise<void> {
//Custom setter method for SystemCode
try {
//Call checkDuplicateSystemCode() method to make sure there is no duplicate system code.
await System.checkDuplicateSystemCode(dbTransaction, systemCode);
//Set this._SystemCode to Params.SystemCode
this.SystemCode = systemCode;
} catch (error) {
throw error;
}
}
public static async findAll(
dbTransaction: any,
loginUser: LoginUser,
page?: number,
rows?: number,
search?: ISystemSearchAttr,
): Promise<{ count: number; systems: System[] }> {
//This method list all system records based on filter.
try {
//Part 1: Retrieve listing
const queryObj: any = {};
const whereObj: any = {};
if (search) {
Object.entries(search).forEach(([key, value]) => {
if (value) {
queryObj[key] = {
[Op.substring]: value,
};
}
});
}
if (page && rows) {
whereObj.offset = (page - 1) * rows;
whereObj.limit = rows;
}
//Call System._Repo findAll() method by passing queryObj and whereObj
const result = await System._Repo.findAllWithPagination({
where: queryObj,
...whereObj,
order: [['CreatedAt', 'DESC']],
transaction: dbTransaction,
});
//Return result
const systems = result.rows.map((system) => new System(system));
return { count: result.count, systems };
} catch (error) {
throw error;
}
}
}
|