42 lines
980 B
TypeScript
42 lines
980 B
TypeScript
import { UniqueID } from "./value-objects";
|
|
|
|
export abstract class DomainEntity<T extends object> {
|
|
private _props: T;
|
|
public readonly id: UniqueID;
|
|
|
|
protected constructor(props: T, id?: UniqueID) {
|
|
this.id = id ?? UniqueID.generateNewID();
|
|
this._props = props;
|
|
}
|
|
|
|
protected get props(): T {
|
|
return this._props;
|
|
}
|
|
|
|
protected set props(value: T) {
|
|
this._props = value;
|
|
}
|
|
|
|
protected _flattenProps(props: T): { [s: string]: any } {
|
|
return Object.entries(props).reduce((result: any, [key, valueObject]) => {
|
|
result[key] = valueObject.value;
|
|
|
|
return result;
|
|
}, {});
|
|
}
|
|
|
|
equals(other?: DomainEntity<T>): boolean {
|
|
if (!other) return false;
|
|
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)),
|
|
};
|
|
}
|
|
}
|