2025-05-09 10:45:32 +00:00
|
|
|
import { Maybe, Result } from "@repo/rdx-utils";
|
2025-09-01 14:07:59 +00:00
|
|
|
import { City } from "./city";
|
|
|
|
|
import { Country } from "./country";
|
|
|
|
|
import { PostalCode } from "./postal-code";
|
|
|
|
|
import { Province } from "./province";
|
|
|
|
|
import { Street } from "./street";
|
2025-02-20 18:55:24 +00:00
|
|
|
import { ValueObject } from "./value-object";
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
export interface PostalAddressProps {
|
|
|
|
|
street: Maybe<Street>;
|
|
|
|
|
street2: Maybe<Street>;
|
|
|
|
|
city: Maybe<City>;
|
|
|
|
|
postalCode: Maybe<PostalCode>;
|
|
|
|
|
province: Maybe<Province>;
|
|
|
|
|
country: Maybe<Country>;
|
2025-02-20 18:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
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<PostalAddressProps>;
|
|
|
|
|
|
|
|
|
|
export class PostalAddress extends ValueObject<PostalAddressProps> {
|
|
|
|
|
protected static validate(values: PostalAddressProps) {
|
|
|
|
|
return Result.ok(values);
|
2025-02-20 18:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
static create(values: PostalAddressProps): Result<PostalAddress, Error> {
|
2025-02-20 18:55:24 +00:00
|
|
|
const valueIsValid = PostalAddress.validate(values);
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
if (valueIsValid.isFailure) {
|
|
|
|
|
return Result.fail(valueIsValid.error);
|
2025-02-20 18:55:24 +00:00
|
|
|
}
|
2025-02-25 19:17:30 +00:00
|
|
|
return Result.ok(new PostalAddress(values));
|
2025-02-20 18:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-02 10:55:45 +00:00
|
|
|
public update(partial: Partial<PostalAddressPatchProps>): Result<PostalAddress, Error> {
|
|
|
|
|
const updatedProps = {
|
|
|
|
|
...this.props,
|
|
|
|
|
...partial,
|
|
|
|
|
} as PostalAddressProps;
|
|
|
|
|
|
|
|
|
|
return PostalAddress.create(updatedProps);
|
2025-03-04 17:08:33 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
get street(): Maybe<Street> {
|
2025-02-20 18:55:24 +00:00
|
|
|
return this.props.street;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
get street2(): Maybe<Street> {
|
|
|
|
|
return this.props.street2;
|
2025-02-25 19:17:30 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
get city(): Maybe<City> {
|
2025-02-20 18:55:24 +00:00
|
|
|
return this.props.city;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
get postalCode(): Maybe<PostalCode> {
|
2025-02-20 18:55:24 +00:00
|
|
|
return this.props.postalCode;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
get province(): Maybe<Province> {
|
|
|
|
|
return this.props.province;
|
2025-02-20 18:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
get country(): Maybe<Country> {
|
2025-02-20 18:55:24 +00:00
|
|
|
return this.props.country;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-04 10:02:24 +00:00
|
|
|
getProps(): PostalAddressProps {
|
2025-02-20 18:55:24 +00:00
|
|
|
return this.props;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-01 14:26:15 +00:00
|
|
|
toPrimitive() {
|
2025-09-04 10:02:24 +00:00
|
|
|
return this.getProps();
|
2025-04-01 14:26:15 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-01 14:07:59 +00:00
|
|
|
toFormat(): string {
|
|
|
|
|
return `${this.props.street}, ${this.props.street2}, ${this.props.city}, ${this.props.postalCode}, ${this.props.province}, ${this.props.country}`;
|
|
|
|
|
}
|
2025-02-20 18:55:24 +00:00
|
|
|
}
|