@tomei/sso
Version:
Tomei SSO Package
125 lines (110 loc) • 3.87 kB
text/typescript
import { ClassError, ObjectBase } from '@tomei/general';
import { LoginHistoryRepository } from './login-history.repository';
import { ILoginHistoryAttr } from '../../interfaces/login-history.interface';
import { LoginUser } from '../login-user/login-user';
import { ApplicationConfig } from '@tomei/config';
import { ILoginHistorySearchAttr } from '../../interfaces/login-history-search-attr.interface';
import { Op } from 'sequelize';
import User from '../../models/user.entity';
export class LoginHistory extends ObjectBase {
ObjectId: string;
ObjectName: string;
TableName = 'sso_LoginHistory';
ObjectType = 'Login History';
HistoryId: number;
UserId: number;
UserName: string;
OriginIP: string;
SystemCode: string;
LoginStatus: string;
private _CreatedAt: Date;
private static _Repo = new LoginHistoryRepository();
get CreatedAt(): Date {
return this._CreatedAt;
}
private constructor(loginHistoryAttr?: ILoginHistoryAttr) {
super();
if (loginHistoryAttr) {
this.HistoryId = loginHistoryAttr?.HistoryId;
this.UserId = loginHistoryAttr?.UserId;
this.UserName = loginHistoryAttr?.User?.UserName;
this.OriginIP = loginHistoryAttr?.OriginIp;
this.SystemCode = loginHistoryAttr?.SystemCode;
this.LoginStatus = loginHistoryAttr?.LoginStatus;
this._CreatedAt = loginHistoryAttr?.CreatedAt;
}
}
public static async findAll(
dbTransaction: any,
loginUser: LoginUser,
page?: number,
rows?: number,
search?: ILoginHistorySearchAttr,
): Promise<{ count: number; loginHistory: LoginHistory[] }> {
try {
// Part 1: Make sure loginUser got "LOGIN_HISTORY_REPORT" privilege.
const sc = ApplicationConfig.getComponentConfigValue('system-code');
const isPrivileged = await loginUser.checkPrivileges(
sc,
'LOGIN_HISTORY_REPORT',
);
if (!isPrivileged) {
throw new ClassError(
'System',
'SystemErrMsg06',
'You do not have permission to list login histories.',
);
}
//Part 2: Transform page and row into sequelize limit and offset & If search object exists, transform search object to Sequelize where object.
const queryObj: any = {};
const whereObj: any = {};
if (search) {
Object.entries(search).forEach(([key, value]) => {
if (value !== undefined && value !== null) {
if (key === 'DateFrom' || key === 'DateTo') {
// We'll handle DateFrom and DateTo after this loop
return;
}
if (key === 'UserId') {
queryObj[key] = value; // Directly assign UserId
return;
}
queryObj[key] = {
[Op.substring]: value,
};
}
});
}
if (page && rows) {
whereObj.offset = (page - 1) * rows;
whereObj.limit = rows;
}
if (search?.DateFrom || search?.DateTo) {
queryObj.createdAt = {
...(search?.DateFrom && { [Op.gte]: search.DateFrom }),
...(search?.DateTo && { [Op.lte]: search.DateTo }),
};
}
//Part 3: Call the class' repo class findAndCountAll() method queryObj and whereObj
const result = await LoginHistory._Repo.findAllWithPagination({
distinct: true,
where: queryObj,
...whereObj,
order: [['CreatedAt', 'DESC']],
include: [
{
model: User,
required: false,
attributes: ['UserName'],
},
],
transaction: dbTransaction,
});
//Part 4: Return the Count and LoginHistories.
const loginHistory = result.rows.map((data) => new LoginHistory(data));
return { count: result.count, loginHistory };
} catch (error) {
throw error;
}
}
}