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