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

107 lines
3.0 KiB
TypeScript
Raw Normal View History

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-03-04 17:08:33 +00:00
static update(
oldAddress: PostalAddress,
data: Partial<PostalAddress>
): Result<PostalAddress, Error> {
return PostalAddress.create({
street: data.street ?? oldAddress.street,
2025-08-26 18:55:59 +00:00
street2: data.street2 ?? oldAddress.street2,
2025-03-04 17:08:33 +00:00
city: data.city ?? oldAddress.city,
postalCode: data.postalCode ?? oldAddress.postalCode,
2025-09-01 14:07:59 +00:00
province: data.province ?? oldAddress.province,
2025-03-04 17:08:33 +00:00
country: data.country ?? oldAddress.country,
2025-05-09 10:45:32 +00:00
// biome-ignore lint/complexity/noThisInStatic: <explanation>
2025-03-04 17:08:33 +00:00
}).getOrElse(this);
}
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-01 14:07:59 +00:00
getValue(): PostalAddressProps {
2025-02-20 18:55:24 +00:00
return this.props;
}
2025-04-01 14:26:15 +00:00
toPrimitive() {
return this.getValue();
}
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}`;
}
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,
};
2025-02-20 18:55:24 +00:00
}
}