metis-desk/modules/companies/src/api/domain/department.entity.ts

35 lines
1.2 KiB
TypeScript
Raw Normal View History

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<DepartmentProps> {
static create(props: DepartmentProps): Result<Department, CompanyError> {
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<DepartmentProps, "companyId">): Result<Department, CompanyError> {
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));
}
}