import { Maybe, Result } from "@repo/rdx-utils"; import { City } from "./city"; import { Country } from "./country"; import { PostalCode } from "./postal-code"; import { Province } from "./province"; import { Street } from "./street"; import { ValueObject } from "./value-object"; export interface PostalAddressProps { street: Maybe; street2: Maybe; city: Maybe; postalCode: Maybe; province: Maybe; country: Maybe; } export interface PostalAddressSnapshot { street: string | null; street2: string | null; city: string | null; postalCode: string | null; province: string | null; country: string | null; } export type PostalAddressPatchProps = Partial; export class PostalAddress extends ValueObject { protected static validate(values: PostalAddressProps) { return Result.ok(values); } static create(values: PostalAddressProps): Result { const valueIsValid = PostalAddress.validate(values); if (valueIsValid.isFailure) { return Result.fail(valueIsValid.error); } return Result.ok(new PostalAddress(values)); } public update(partial: Partial): Result { const updatedProps = { ...this.props, ...partial, } as PostalAddressProps; return PostalAddress.create(updatedProps); } get street(): Maybe { return this.props.street; } get street2(): Maybe { return this.props.street2; } get city(): Maybe { return this.props.city; } get postalCode(): Maybe { return this.props.postalCode; } get province(): Maybe { return this.props.province; } get country(): Maybe { return this.props.country; } getValue(): PostalAddressProps { return this.props; } toPrimitive() { return this.getValue(); } toFormat(): string { return `${this.props.street}, ${this.props.street2}, ${this.props.city}, ${this.props.postalCode}, ${this.props.province}, ${this.props.country}`; } }