2025-09-30 11:05:12 +00:00
|
|
|
import { DomainValidationError, ValueObject } from "@repo/rdx-ddd";
|
2025-06-11 15:13:44 +00:00
|
|
|
import { Maybe, Result } from "@repo/rdx-utils";
|
2025-09-24 17:30:35 +00:00
|
|
|
import { z } from "zod/v4";
|
2025-06-11 15:13:44 +00:00
|
|
|
|
2025-09-03 10:41:12 +00:00
|
|
|
interface CustomerInvoiceItemDescriptionProps {
|
2025-06-11 15:13:44 +00:00
|
|
|
value: string;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-03 10:41:12 +00:00
|
|
|
export class CustomerInvoiceItemDescription extends ValueObject<CustomerInvoiceItemDescriptionProps> {
|
2025-10-30 12:39:56 +00:00
|
|
|
private static readonly MAX_LENGTH = 2000;
|
2025-06-24 18:38:57 +00:00
|
|
|
private static readonly FIELD = "invoiceItemDescription";
|
|
|
|
|
private static readonly ERROR_CODE = "INVALID_INVOICE_ITEM_DESCRIPTION";
|
2025-06-11 15:13:44 +00:00
|
|
|
|
|
|
|
|
protected static validate(value: string) {
|
|
|
|
|
const schema = z
|
|
|
|
|
.string()
|
|
|
|
|
.trim()
|
|
|
|
|
.max(CustomerInvoiceItemDescription.MAX_LENGTH, {
|
|
|
|
|
message: `Description must be at most ${CustomerInvoiceItemDescription.MAX_LENGTH} characters long`,
|
|
|
|
|
});
|
|
|
|
|
return schema.safeParse(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static create(value: string) {
|
2025-09-03 10:41:12 +00:00
|
|
|
const valueIsValid = CustomerInvoiceItemDescription.validate(value);
|
2025-06-24 18:38:57 +00:00
|
|
|
|
2025-09-03 10:41:12 +00:00
|
|
|
if (!valueIsValid.success) {
|
|
|
|
|
const detail = valueIsValid.error.message;
|
2025-06-24 18:38:57 +00:00
|
|
|
return Result.fail(
|
|
|
|
|
new DomainValidationError(
|
|
|
|
|
CustomerInvoiceItemDescription.ERROR_CODE,
|
|
|
|
|
CustomerInvoiceItemDescription.FIELD,
|
|
|
|
|
detail
|
|
|
|
|
)
|
|
|
|
|
);
|
2025-06-11 15:13:44 +00:00
|
|
|
}
|
|
|
|
|
return Result.ok(new CustomerInvoiceItemDescription({ value }));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static createNullable(value?: string): Result<Maybe<CustomerInvoiceItemDescription>, Error> {
|
|
|
|
|
if (!value || value.trim() === "") {
|
|
|
|
|
return Result.ok(Maybe.none<CustomerInvoiceItemDescription>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return CustomerInvoiceItemDescription.create(value).map((value) => Maybe.some(value));
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-04 10:02:24 +00:00
|
|
|
getProps(): string {
|
2025-06-11 15:13:44 +00:00
|
|
|
return this.props.value;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-10 18:14:19 +00:00
|
|
|
toString() {
|
|
|
|
|
return String(this.props.value);
|
2025-06-11 15:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toPrimitive() {
|
2025-09-04 10:02:24 +00:00
|
|
|
return this.getProps();
|
2025-06-11 15:13:44 +00:00
|
|
|
}
|
|
|
|
|
}
|