38 lines
956 B
TypeScript
38 lines
956 B
TypeScript
|
|
import { UniqueID } from "./value-objects/unique-id";
|
||
|
|
|
||
|
|
export abstract class DomainEntity<T extends object> {
|
||
|
|
protected readonly _id: UniqueID;
|
||
|
|
protected readonly _props: T;
|
||
|
|
|
||
|
|
protected constructor(props: T, id?: UniqueID) {
|
||
|
|
this._id = id ? id : UniqueID.generateNewID().data;
|
||
|
|
this._props = props;
|
||
|
|
}
|
||
|
|
|
||
|
|
protected _flattenProps(props: T): { [s: string]: any } {
|
||
|
|
return Object.entries(props).reduce((result: any, [key, valueObject]) => {
|
||
|
|
console.log(key, valueObject.value);
|
||
|
|
result[key] = valueObject.value;
|
||
|
|
|
||
|
|
return result;
|
||
|
|
}, {});
|
||
|
|
}
|
||
|
|
|
||
|
|
get id(): UniqueID {
|
||
|
|
return this._id;
|
||
|
|
}
|
||
|
|
|
||
|
|
equals(other: DomainEntity<T>): boolean {
|
||
|
|
return other instanceof DomainEntity && this.id.equals(other.id);
|
||
|
|
}
|
||
|
|
|
||
|
|
toString(): { [s: string]: string } {
|
||
|
|
const flattenProps = this._flattenProps(this._props);
|
||
|
|
|
||
|
|
return {
|
||
|
|
id: this._id.toString(),
|
||
|
|
...flattenProps.map((prop: any) => String(prop)),
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|