Files
people_manager/src/models/entity.ts
T
Sebastian 85faaa3747 refactor: minor code cleanup
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
2026-06-28 12:22:15 -04:00

105 lines
3.0 KiB
TypeScript

/**
* Class model for Entity Interface
*/
import type { EntityInterface, EntityModelInterface, EntityPropertiesInterface } from "@/types/entity";
import type { IndividualInterface } from "@/types/individual";
import type { OrganizationInterface } from "@/types/organization";
import type { GroupInterface } from "@/types/group";
import type { CollectionIdentifier, EntityIdentifier } from "@/types/common";
import { IndividualObject } from "./individual";
import { OrganizationObject } from "./organization";
import { GroupObject } from "./group";
import { clonePlain } from './clone-plain';
export type EntityPropertiesObject = IndividualObject | OrganizationObject | GroupObject;
export class EntityObject implements EntityModelInterface {
private _data!: EntityInterface<EntityPropertiesInterface>;
private _properties: EntityPropertiesObject | undefined = undefined;
constructor() {
this._data = {
'@type': 'people:entity',
version: 1,
provider: '',
service: '',
collection: '' as CollectionIdentifier,
identifier: '' as EntityIdentifier,
signature: null,
created: null,
modified: null,
properties: new IndividualObject().toJson(),
};
}
fromJson(data: EntityInterface): EntityObject {
this._data = clonePlain(data);
this._properties = undefined;
return this;
}
toJson(): EntityInterface {
const json = this._properties
? { ...this._data, properties: this._properties.toJson() }
: this._data;
return clonePlain(json);
}
clone(): EntityObject {
return new EntityObject().fromJson(this.toJson());
}
/** Metadata Properties */
get provider(): string {
return this._data.provider;
}
get service(): string {
return this._data.service;
}
get collection(): CollectionIdentifier {
return this._data.collection;
}
get identifier(): EntityIdentifier {
return this._data.identifier;
}
get signature(): string | null {
return this._data.signature;
}
get created(): string | null {
return this._data.created;
}
get modified(): string | null {
return this._data.modified;
}
/** Entity Properties (individual | organization | group) */
get properties(): EntityPropertiesObject {
if (this._properties) return this._properties;
const raw = this._data.properties;
if (raw.type === 'organization') {
this._properties = new OrganizationObject().fromJson(raw as OrganizationInterface);
} else if (raw.type === 'group') {
this._properties = new GroupObject().fromJson(raw as GroupInterface);
} else {
this._properties = new IndividualObject().fromJson(raw as IndividualInterface);
}
return this._properties;
}
set properties(value: EntityPropertiesObject) {
this._properties = value;
}
}