import type { Name, UniqueID } from "@repo/rdx-ddd"; import { DomainEntity } from "@repo/rdx-ddd"; import { Result } from "@repo/rdx-utils"; import { CompanyError } from "./company-error"; export type DepartmentProps = Readonly<{ companyId: UniqueID; name: Name; active: boolean }>; export class Department extends DomainEntity { static create(props: DepartmentProps): Result { if (props.name.toPrimitive().length > 100) { return Result.fail(new CompanyError("INVALID_REQUEST")); } return Result.ok(new Department(props)); } static rehydrate(props: DepartmentProps, id: UniqueID) { return new Department(props, id); } get companyId() { return this.props.companyId; } get name() { return this.props.name; } get active() { return this.props.active; } update(values: Omit): Result { if (values.name.toPrimitive().length > 100) { return Result.fail(new CompanyError("INVALID_REQUEST")); } return this.name.equals(values.name) && this.active === values.active ? Result.ok(this) : Result.ok(new Department({ ...values, companyId: this.companyId }, this.id)); } }