143 lines
5.1 KiB
JavaScript
143 lines
5.1 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.UserData = void 0;
|
|
class UserData {
|
|
id;
|
|
authentikId;
|
|
username;
|
|
email;
|
|
password;
|
|
roles;
|
|
permissions;
|
|
isActive;
|
|
deletedAt;
|
|
lastSeenAt;
|
|
storageQuota;
|
|
storageUsed;
|
|
constructor(id, authentikId, username, email, password, roles, permissions, isActive, deletedAt, lastSeenAt, storageQuota, storageUsed) {
|
|
this.id = id;
|
|
this.authentikId = authentikId;
|
|
this.username = username;
|
|
this.email = email;
|
|
this.password = password;
|
|
this.roles = roles;
|
|
this.permissions = permissions;
|
|
this.isActive = isActive;
|
|
this.deletedAt = deletedAt;
|
|
this.lastSeenAt = lastSeenAt;
|
|
this.storageQuota = storageQuota;
|
|
this.storageUsed = storageUsed;
|
|
}
|
|
//#region Create
|
|
static create(data) {
|
|
const quota = data.storageQuota !== undefined ? data.storageQuota : 10737418240; // 10 GB
|
|
const used = data.storageUsed !== undefined ? data.storageUsed : 0;
|
|
return new UserData(crypto.randomUUID(), data.authentikId || null, data.username, data.email, data.password ?? null, [], [], true, null, null, quota, used);
|
|
}
|
|
//#endregion
|
|
//#region Read
|
|
hasRole(roleId) {
|
|
return this.roles.some(x => x.id.toLowerCase() === roleId.toLowerCase());
|
|
}
|
|
assignRole(roleData) {
|
|
if (this.hasRole(roleData.id))
|
|
throw new Error("Role Sudah dimiliki.");
|
|
this.roles.push(roleData);
|
|
}
|
|
hasPermissions(permissionId) {
|
|
return this.permissions.some(x => x.id.toLowerCase() === permissionId.toLowerCase());
|
|
}
|
|
hasPermission(permission) {
|
|
const byPerm = this.permissions.some(x => x.id.toLowerCase() === permission.toLowerCase() || x.name.toLowerCase() === permission.toLowerCase());
|
|
if (byPerm)
|
|
return true;
|
|
// check roles
|
|
return this.roles.some(r => r.permissions.some(p => p.id.toLowerCase() === permission.toLowerCase() || p.name.toLowerCase() === permission.toLowerCase()));
|
|
}
|
|
assignPermission(permissionData) {
|
|
if (this.hasPermissions(permissionData.id))
|
|
throw new Error("Permission sudah dimiliki.");
|
|
this.permissions.push(permissionData);
|
|
}
|
|
//#endregion
|
|
//#region Update
|
|
changeEmail(email) {
|
|
this.email = email;
|
|
}
|
|
changeUsername(username) {
|
|
this.username = username;
|
|
}
|
|
touchLastSeen() {
|
|
this.lastSeenAt = new Date();
|
|
}
|
|
enable() {
|
|
this.isActive = true;
|
|
}
|
|
disable() {
|
|
this.isActive = false;
|
|
}
|
|
//#endregion
|
|
//#region Delete
|
|
softDelete() {
|
|
this.deletedAt = new Date();
|
|
this.isActive = false;
|
|
}
|
|
restoreInstance() {
|
|
this.deletedAt = null;
|
|
}
|
|
//#endregion
|
|
static restore(props) {
|
|
return new UserData(props.id, props.authentikId || null, props.username, props.email, props.password, props.roles || [], props.permissions || [], props.isActive !== undefined ? props.isActive : true, props.deletedAt || null, props.lastSeenAt || null, props.storageQuota !== undefined && props.storageQuota !== null ? props.storageQuota : 10737418240, props.storageUsed !== undefined && props.storageUsed !== null ? props.storageUsed : 0);
|
|
}
|
|
removeRole(roleId) {
|
|
const idx = this.roles.findIndex(r => r.id.toLowerCase() === roleId.toLowerCase());
|
|
if (idx === -1)
|
|
throw new Error('Role tidak ditemukan pada user.');
|
|
this.roles.splice(idx, 1);
|
|
}
|
|
removePermission(permissionId) {
|
|
const idx = this.permissions.findIndex(p => p.id.toLowerCase() === permissionId.toLowerCase());
|
|
if (idx === -1)
|
|
throw new Error('Permission tidak ditemukan pada user.');
|
|
this.permissions.splice(idx, 1);
|
|
}
|
|
// Storage helpers
|
|
setStorageQuota(bytes) {
|
|
if (bytes < 0)
|
|
throw new Error('storageQuota must be >= 0');
|
|
if (this.storageUsed > bytes)
|
|
throw new Error('storageQuota cannot be less than storageUsed');
|
|
this.storageQuota = bytes;
|
|
}
|
|
setStorageUsed(bytes) {
|
|
if (bytes < 0)
|
|
throw new Error('storageUsed must be >= 0');
|
|
if (bytes > this.storageQuota)
|
|
throw new Error('storageUsed cannot exceed storageQuota');
|
|
this.storageUsed = bytes;
|
|
}
|
|
// Response helper for API
|
|
toResponse() {
|
|
const remaining = this.storageQuota - this.storageUsed;
|
|
const usagePercentage = this.storageQuota > 0 ? Math.round((this.storageUsed / this.storageQuota) * 100) : 0;
|
|
return {
|
|
id: this.id,
|
|
authentikId: this.authentikId,
|
|
username: this.username,
|
|
email: this.email,
|
|
roles: this.roles,
|
|
permissions: this.permissions,
|
|
isActive: this.isActive,
|
|
deletedAt: this.deletedAt,
|
|
lastSeenAt: this.lastSeenAt,
|
|
storage: {
|
|
quota: this.storageQuota,
|
|
used: this.storageUsed,
|
|
remaining,
|
|
usagePercentage,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
exports.UserData = UserData;
|
|
//# sourceMappingURL=user.entity.js.map
|