Uecko_ERP/packages/rdx-ddd/src/value-objects/postal-address.ts

93 lines
2.5 KiB
TypeScript
Raw Normal View History

2026-02-17 18:13:59 +00:00
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";
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 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
}
2026-03-09 20:23:48 +00:00
public update(partial: PostalAddressPatchProps): Result<PostalAddress, Error> {
2025-09-02 10:55:45 +00:00
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-10 18:14:19 +00:00
toString() {
return {
2026-02-17 18:13:59 +00:00
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()),
2025-09-10 18:14:19 +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
}