42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
import { AggregateID, AggregateRoot } from "@src/libs/ddd";
|
|
import { UserCreatedDomainEvent } from "./events/user-created.domain-event";
|
|
import { CreateUserProps, UserProps, UserRoles } from "./user.types";
|
|
import { v4 } from 'uuid';
|
|
import { UserDeletedDomainEvent } from "./events/user-deleted.domain.event";
|
|
|
|
export class UserEntity extends AggregateRoot<UserProps> {
|
|
protected readonly _id: AggregateID;
|
|
|
|
static create(create: CreateUserProps): UserEntity {
|
|
const id = v4();
|
|
const props: UserProps = { ...create };
|
|
const user = new UserEntity({ id, props });
|
|
user.addEvent(
|
|
new UserCreatedDomainEvent({
|
|
aggregateId: id,
|
|
email: props.email,
|
|
password: props.password,
|
|
phone_number: props.password,
|
|
fullname: props.fullname,
|
|
role_id: props.role_id
|
|
})
|
|
);
|
|
return user;
|
|
}
|
|
|
|
get role(): UserRoles {
|
|
return this.props.role_id;
|
|
}
|
|
|
|
delete(): void {
|
|
this.addEvent(
|
|
new UserDeletedDomainEvent({
|
|
aggregateId: this.id,
|
|
})
|
|
)
|
|
}
|
|
|
|
public validate(): void {
|
|
// throw new Error("Method not implemented.");
|
|
}
|
|
} |