Uecko_ERP/apps/server/src/common/domain/domain-entity.ts

38 lines
956 B
TypeScript
Raw Normal View History

2025-02-01 21:48:13 +00:00
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)),
};
}
}