import { type Maybe, Result } from "@repo/rdx-utils"; import { maybeToEmptyString } from "../helpers"; import type { City } from "./city"; import type { Country } from "./country"; import type { PostalCode } from "./postal-code"; import type { Province } from "./province"; import type { Street } from "./street"; import { ValueObject } from "./value-object"; export interface PostalAddressProps { street: Maybe; street2: Maybe; city: Maybe; postalCode: Maybe; province: Maybe; country: Maybe; } 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: PostalAddressPatchProps): Result { Object.assign(this.props, partial); return Result.ok(); } 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; } getProps(): PostalAddressProps { return this.props; } toPrimitive() { return this.getProps(); } toString() { return { street: maybeToEmptyString(this.street, (value) => value.toString()), street2: maybeToEmptyString(this.street2, (value) => value.toString()), city: maybeToEmptyString(this.city, (value) => value.toString()), postal_code: maybeToEmptyString(this.postalCode, (value) => value.toString()), province: maybeToEmptyString(this.province, (value) => value.toString()), country: maybeToEmptyString(this.country, (value) => value.toString()), }; } toFormat(): string { return `${this.props.street}, ${this.props.street2}, ${this.props.city}, ${this.props.postalCode}, ${this.props.province}, ${this.props.country}`; } }