Initial Version
This commit is contained in:
98
core/src/models/userProfile.ts
Normal file
98
core/src/models/userProfile.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
/**
|
||||
* User Profile Model
|
||||
*/
|
||||
|
||||
import type { ProfileFieldInterface, UserProfileInterface } from '@KTXC/types/user/userProfileTypes';
|
||||
|
||||
export class UserProfile {
|
||||
private _data: UserProfileInterface;
|
||||
|
||||
constructor(data: UserProfileInterface = {}) {
|
||||
this._data = data;
|
||||
}
|
||||
|
||||
fromJson(data: UserProfileInterface): UserProfile {
|
||||
this._data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
toJson(): UserProfileInterface {
|
||||
return { ...this._data };
|
||||
}
|
||||
|
||||
clone(): UserProfile {
|
||||
return new UserProfile(JSON.parse(JSON.stringify(this._data)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a profile field value
|
||||
*/
|
||||
get(key: string): any {
|
||||
return this._data[key]?.value ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a profile field value (only if editable)
|
||||
*/
|
||||
set(key: string, value: any): boolean {
|
||||
if (!this._data[key]) {
|
||||
console.warn(`Profile field "${key}" does not exist`);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!this._data[key].editable) {
|
||||
console.warn(`Profile field "${key}" is managed by ${this._data[key].provider} and cannot be edited`);
|
||||
return false;
|
||||
}
|
||||
|
||||
this._data[key].value = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a field is editable
|
||||
*/
|
||||
isEditable(key: string): boolean {
|
||||
return this._data[key]?.editable ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get field metadata
|
||||
*/
|
||||
getField(key: string): ProfileFieldInterface | null {
|
||||
return this._data[key] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all fields
|
||||
*/
|
||||
get fields(): UserProfileInterface {
|
||||
return this._data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get only editable fields
|
||||
*/
|
||||
get editableFields(): UserProfileInterface {
|
||||
return Object.entries(this._data)
|
||||
.filter(([_, field]) => (field as ProfileFieldInterface).editable)
|
||||
.reduce((acc, [key, field]) => ({ ...acc, [key]: field }), {} as UserProfileInterface);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get only managed (non-editable) fields
|
||||
*/
|
||||
get managedFields(): UserProfileInterface {
|
||||
return Object.entries(this._data)
|
||||
.filter(([_, field]) => !(field as ProfileFieldInterface).editable)
|
||||
.reduce((acc, [key, field]) => ({ ...acc, [key]: field }), {} as UserProfileInterface);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get provider managing this profile
|
||||
*/
|
||||
get provider(): string | null {
|
||||
const managedField = Object.values(this._data).find(f => !(f as ProfileFieldInterface).editable);
|
||||
return (managedField as ProfileFieldInterface)?.provider ?? null;
|
||||
}
|
||||
}
|
||||
73
core/src/models/userSettings.ts
Normal file
73
core/src/models/userSettings.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* User Settings Model
|
||||
*/
|
||||
|
||||
import type { UserSettingsInterface } from '@KTXC/types/user/userSettingsTypes';
|
||||
|
||||
export class UserSettings {
|
||||
private _data: UserSettingsInterface;
|
||||
|
||||
constructor(data: UserSettingsInterface = {}) {
|
||||
this._data = data;
|
||||
}
|
||||
|
||||
fromJson(data: UserSettingsInterface): UserSettings {
|
||||
this._data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
toJson(): UserSettingsInterface {
|
||||
return { ...this._data };
|
||||
}
|
||||
|
||||
clone(): UserSettings {
|
||||
return new UserSettings(JSON.parse(JSON.stringify(this._data)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a setting value
|
||||
*/
|
||||
get(key: string): any {
|
||||
return this._data[key] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a setting value
|
||||
*/
|
||||
set(key: string, value: any): void {
|
||||
this._data[key] = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a setting exists
|
||||
*/
|
||||
has(key: string): boolean {
|
||||
return key in this._data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all settings
|
||||
*/
|
||||
get all(): UserSettingsInterface {
|
||||
return this._data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get multiple settings
|
||||
*/
|
||||
getMany(keys: string[]): Record<string, any> {
|
||||
return keys.reduce((acc, key) => ({
|
||||
...acc,
|
||||
[key]: this._data[key] ?? null
|
||||
}), {});
|
||||
}
|
||||
|
||||
/**
|
||||
* Set multiple settings
|
||||
*/
|
||||
setMany(settings: Record<string, any>): void {
|
||||
Object.entries(settings).forEach(([key, value]) => {
|
||||
this._data[key] = value;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user