Uecko_ERP/packages/rdx-ddd/src/domain-entity.ts

34 lines
888 B
TypeScript
Raw Normal View History

2025-05-09 10:45:32 +00:00
import { UniqueID } from "./value-objects";
2025-02-01 21:48:13 +00:00
export abstract class DomainEntity<T extends object> {
2025-02-20 18:55:24 +00:00
protected readonly props: T;
public readonly id: UniqueID;
2025-02-01 21:48:13 +00:00
protected constructor(props: T, id?: UniqueID) {
2025-02-20 18:55:24 +00:00
this.id = id ? id : UniqueID.generateNewID().data;
this.props = props;
2025-02-01 21:48:13 +00:00
}
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;
}, {});
}
equals(other: DomainEntity<T>): boolean {
return other instanceof DomainEntity && this.id.equals(other.id);
}
toString(): { [s: string]: string } {
2025-02-20 18:55:24 +00:00
const flattenProps = this._flattenProps(this.props);
2025-02-01 21:48:13 +00:00
return {
2025-02-20 18:55:24 +00:00
id: this.id.toString(),
2025-02-01 21:48:13 +00:00
...flattenProps.map((prop: any) => String(prop)),
};
}
}