chore: standardize chrono provider
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
+64
-100
@@ -9,154 +9,118 @@ import { EventObject } from "./event";
|
||||
import { TaskObject } from "./task";
|
||||
import { JournalObject } from "./journal";
|
||||
|
||||
type EntityPropertiesData = EventInterface | TaskInterface | JournalInterface;
|
||||
type EntityPropertiesObject = EventObject | TaskObject | JournalObject;
|
||||
type EntityInternal = Omit<EntityInterface<EntityPropertiesData>, 'properties'> & {
|
||||
properties: EntityPropertiesData | EntityPropertiesObject;
|
||||
};
|
||||
|
||||
export class EntityObject implements EntityInterface {
|
||||
|
||||
_data!: EntityInterface;
|
||||
_data!: EntityInternal;
|
||||
|
||||
constructor() {
|
||||
this._data = {
|
||||
'@type': 'chrono:entity',
|
||||
version: 1,
|
||||
in: null,
|
||||
id: null,
|
||||
createdOn: null,
|
||||
createdBy: null,
|
||||
modifiedOn: null,
|
||||
modifiedBy: null,
|
||||
provider: '',
|
||||
service: '',
|
||||
collection: '',
|
||||
identifier: '',
|
||||
signature: null,
|
||||
data: null,
|
||||
created: null,
|
||||
modified: null,
|
||||
properties: new EventObject().toJson(),
|
||||
};
|
||||
}
|
||||
|
||||
fromJson(data: EntityInterface): EntityObject {
|
||||
this._data = data;
|
||||
if (data.data) {
|
||||
const type = data.data.type;
|
||||
if (type === 'task') {
|
||||
this._data.data = new TaskObject().fromJson(data.data as TaskInterface);
|
||||
} else if (type === 'journal') {
|
||||
this._data.data = new JournalObject().fromJson(data.data as JournalInterface);
|
||||
} else {
|
||||
this._data.data = new EventObject().fromJson(data.data as EventInterface);
|
||||
}
|
||||
this._data = data as EntityInternal;
|
||||
if (data.properties) {
|
||||
this._data.properties = this.hydrateProperties(data.properties);
|
||||
} else {
|
||||
this._data.data = null;
|
||||
this._data.properties = new EventObject();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
toJson(): EntityInterface {
|
||||
toJson(): EntityInterface {
|
||||
const json = { ...this._data };
|
||||
if (this._data.data instanceof EventObject ||
|
||||
this._data.data instanceof TaskObject ||
|
||||
this._data.data instanceof JournalObject) {
|
||||
json.data = this._data.data.toJson();
|
||||
if (this._data.properties instanceof EventObject ||
|
||||
this._data.properties instanceof TaskObject ||
|
||||
this._data.properties instanceof JournalObject) {
|
||||
json.properties = this._data.properties.toJson();
|
||||
}
|
||||
return json;
|
||||
return json as EntityInterface;
|
||||
}
|
||||
|
||||
clone(): EntityObject {
|
||||
const cloned = new EntityObject();
|
||||
cloned._data = JSON.parse(JSON.stringify(this._data));
|
||||
cloned._data = JSON.parse(JSON.stringify(this.toJson())) as EntityInternal;
|
||||
return cloned;
|
||||
}
|
||||
|
||||
/** Properties */
|
||||
private hydrateProperties(properties: EntityPropertiesData): EntityPropertiesObject {
|
||||
const type = properties.type;
|
||||
if (type === 'task') {
|
||||
return new TaskObject().fromJson(properties as TaskInterface);
|
||||
}
|
||||
|
||||
get '@type'(): string {
|
||||
return this._data['@type'];
|
||||
if (type === 'journal') {
|
||||
return new JournalObject().fromJson(properties as JournalInterface);
|
||||
}
|
||||
|
||||
return new EventObject().fromJson(properties as EventInterface);
|
||||
}
|
||||
|
||||
get version(): number {
|
||||
return this._data.version;
|
||||
/** Immutable Properties */
|
||||
|
||||
get provider(): string {
|
||||
return this._data.provider;
|
||||
}
|
||||
|
||||
set version(value: number) {
|
||||
this._data.version = value;
|
||||
get service(): string {
|
||||
return this._data.service;
|
||||
}
|
||||
|
||||
get in(): string | number | null {
|
||||
return this._data.in;
|
||||
get collection(): string | number {
|
||||
return this._data.collection;
|
||||
}
|
||||
|
||||
set in(value: string | number | null) {
|
||||
this._data.in = value;
|
||||
}
|
||||
|
||||
get id(): string | number | null {
|
||||
return this._data.id;
|
||||
}
|
||||
|
||||
set id(value: string | number | null) {
|
||||
this._data.id = value;
|
||||
}
|
||||
|
||||
get createdOn(): Date | null {
|
||||
return this._data.createdOn;
|
||||
}
|
||||
|
||||
set createdOn(value: Date | null) {
|
||||
this._data.createdOn = value;
|
||||
}
|
||||
|
||||
get createdBy(): string | null {
|
||||
return this._data.createdBy;
|
||||
}
|
||||
|
||||
set createdBy(value: string | null) {
|
||||
this._data.createdBy = value;
|
||||
}
|
||||
|
||||
get modifiedOn(): Date | null {
|
||||
return this._data.modifiedOn;
|
||||
}
|
||||
|
||||
set modifiedOn(value: Date | null) {
|
||||
this._data.modifiedOn = value;
|
||||
}
|
||||
|
||||
get modifiedBy(): string | null {
|
||||
return this._data.modifiedBy;
|
||||
}
|
||||
|
||||
set modifiedBy(value: string | null) {
|
||||
this._data.modifiedBy = value;
|
||||
get identifier(): string | number {
|
||||
return this._data.identifier;
|
||||
}
|
||||
|
||||
get signature(): string | null {
|
||||
return this._data.signature;
|
||||
}
|
||||
|
||||
set signature(value: string | null) {
|
||||
this._data.signature = value;
|
||||
get created(): string | null {
|
||||
return this._data.created;
|
||||
}
|
||||
|
||||
get data(): EventObject | TaskObject | JournalObject | null {
|
||||
if (this._data.data instanceof EventObject ||
|
||||
this._data.data instanceof TaskObject ||
|
||||
this._data.data instanceof JournalObject) {
|
||||
return this._data.data;
|
||||
get modified(): string | null {
|
||||
return this._data.modified;
|
||||
}
|
||||
|
||||
get properties(): EntityPropertiesObject {
|
||||
if (this._data.properties instanceof EventObject ||
|
||||
this._data.properties instanceof TaskObject ||
|
||||
this._data.properties instanceof JournalObject) {
|
||||
return this._data.properties;
|
||||
}
|
||||
|
||||
if (this._data.data) {
|
||||
const type = this._data.data.type;
|
||||
let hydrated;
|
||||
if (type === 'task') {
|
||||
hydrated = new TaskObject().fromJson(this._data.data as TaskInterface);
|
||||
} else if (type === 'journal') {
|
||||
hydrated = new JournalObject().fromJson(this._data.data as JournalInterface);
|
||||
} else {
|
||||
hydrated = new EventObject().fromJson(this._data.data as EventInterface);
|
||||
}
|
||||
this._data.data = hydrated;
|
||||
if (this._data.properties) {
|
||||
const hydrated = this.hydrateProperties(this._data.properties as EntityPropertiesData);
|
||||
this._data.properties = hydrated;
|
||||
return hydrated;
|
||||
}
|
||||
|
||||
return null;
|
||||
const defaultProperties = new EventObject();
|
||||
this._data.properties = defaultProperties;
|
||||
return defaultProperties;
|
||||
}
|
||||
|
||||
set data(value: EventObject | TaskObject | JournalObject | null) {
|
||||
this._data.data = value;
|
||||
set properties(value: EntityPropertiesObject) {
|
||||
this._data.properties = value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user