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)); } static update( oldAddress: PostalAddress, data: Partial ): Result { return PostalAddress.create({ street: data.street ?? oldAddress.street, street2: data.street2 ?? oldAddress.street2, city: data.city ?? oldAddress.city, postalCode: data.postalCode ?? oldAddress.postalCode, province: data.province ?? oldAddress.province, country: data.country ?? oldAddress.country, // biome-ignore lint/complexity/noThisInStatic: }).getOrElse(this); } 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}`; } public toSnapshot(): PostalAddressSnapshot { return { street: this.props.street.isSome() ? this.props.street.unwrap()!.toString() : null, street2: this.props.street2.isSome() ? this.props.street2.unwrap()!.toString() : null, city: this.props.city.isSome() ? this.props.city.unwrap()!.toString() : null, postalCode: this.props.postalCode.isSome() ? this.props.postalCode.unwrap()!.toString() : null, province: this.props.province.isSome() ? this.props.province.unwrap()!.toString() : null, country: this.props.country.isSome() ? this.props.country.unwrap()!.toString() : null, }; } }