refactor: front end code to unified manager api
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
+48
-48
@@ -2,64 +2,59 @@
|
||||
* Class model for Entity Interface
|
||||
*/
|
||||
|
||||
import type { EntityInterface } from "@/types/entity";
|
||||
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 class EntityObject implements EntityInterface {
|
||||
export class EntityObject implements EntityModelInterface {
|
||||
|
||||
private _data!: EntityInterface<EntityPropertiesInterface>;
|
||||
private _properties: IndividualObject | OrganizationObject | GroupObject | undefined = undefined;
|
||||
|
||||
_data!: EntityInterface;
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
'@type': 'people:entity',
|
||||
version: 1,
|
||||
provider: '',
|
||||
service: '',
|
||||
collection: '',
|
||||
identifier: '',
|
||||
collection: '' as CollectionIdentifier,
|
||||
identifier: '' as EntityIdentifier,
|
||||
signature: null,
|
||||
created: null,
|
||||
modified: null,
|
||||
properties: new IndividualObject(),
|
||||
properties: new IndividualObject().toJson(),
|
||||
};
|
||||
}
|
||||
|
||||
fromJson(data: EntityInterface) : EntityObject {
|
||||
this._data = data;
|
||||
if (data.properties) {
|
||||
const type = data.properties.type;
|
||||
if (type === 'organization') {
|
||||
this._data.properties = new OrganizationObject().fromJson(data.properties as OrganizationInterface);
|
||||
} else if (type === 'group') {
|
||||
this._data.properties = new GroupObject().fromJson(data.properties as GroupInterface);
|
||||
} else {
|
||||
this._data.properties = new IndividualObject().fromJson(data.properties as IndividualInterface);
|
||||
}
|
||||
}
|
||||
|
||||
fromJson(data: EntityInterface): EntityObject {
|
||||
this._data = clonePlain(data);
|
||||
this._properties = undefined;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
toJson(): EntityInterface {
|
||||
const json = { ...this._data };
|
||||
if (this._data.properties instanceof IndividualObject ||
|
||||
this._data.properties instanceof OrganizationObject ||
|
||||
this._data.properties instanceof GroupObject) {
|
||||
json.properties = this._data.properties.toJson();
|
||||
}
|
||||
return json;
|
||||
const json = this._properties
|
||||
? {
|
||||
...this._data,
|
||||
properties: this._properties.toJson(),
|
||||
}
|
||||
: this._data;
|
||||
|
||||
return clonePlain(json);
|
||||
}
|
||||
|
||||
clone(): EntityObject {
|
||||
const cloned = new EntityObject();
|
||||
cloned._data = { ...this._data };
|
||||
return cloned;
|
||||
return new EntityObject().fromJson(this.toJson());
|
||||
}
|
||||
|
||||
/** Immutable Properties */
|
||||
|
||||
|
||||
/** Metadata Properties */
|
||||
|
||||
get provider(): string {
|
||||
return this._data.provider;
|
||||
}
|
||||
@@ -68,11 +63,11 @@ export class EntityObject implements EntityInterface {
|
||||
return this._data.service;
|
||||
}
|
||||
|
||||
get collection(): string | number {
|
||||
get collection(): CollectionIdentifier {
|
||||
return this._data.collection;
|
||||
}
|
||||
|
||||
get identifier(): string | number {
|
||||
get identifier(): EntityIdentifier {
|
||||
return this._data.identifier;
|
||||
}
|
||||
|
||||
@@ -88,20 +83,25 @@ export class EntityObject implements EntityInterface {
|
||||
return this._data.modified;
|
||||
}
|
||||
|
||||
/** Entity Properties (individual | organization | group) */
|
||||
|
||||
get properties(): IndividualObject | OrganizationObject | GroupObject {
|
||||
if (this._data.properties instanceof IndividualObject ||
|
||||
this._data.properties instanceof OrganizationObject ||
|
||||
this._data.properties instanceof GroupObject) {
|
||||
return this._data.properties;
|
||||
if (this._properties) {
|
||||
return this._properties;
|
||||
}
|
||||
|
||||
const defaultProperties = new IndividualObject();
|
||||
this._data.properties = defaultProperties;
|
||||
return defaultProperties;
|
||||
const raw = this._data.properties as EntityPropertiesInterface;
|
||||
const type = (raw as { type?: string })?.type;
|
||||
|
||||
if (type === 'organization') {
|
||||
this._properties = new OrganizationObject().fromJson(raw as OrganizationInterface);
|
||||
} else if (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: IndividualObject | OrganizationObject | GroupObject) {
|
||||
this._data.properties = value;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user