FactuGES_ERP/modules/customer-invoices/src/api/domain/value-objects/customer-invoice-number.ts

43 lines
1.0 KiB
TypeScript
Raw Normal View History

2025-05-20 10:08:24 +00:00
import { ValueObject } from "@repo/rdx-ddd";
2025-05-09 10:45:32 +00:00
import { Result } from "@repo/rdx-utils";
2025-04-01 14:26:15 +00:00
import { z } from "zod";
2025-06-11 15:13:44 +00:00
interface ICustomerInvoiceNumberProps {
2025-04-01 14:26:15 +00:00
value: string;
}
2025-06-11 15:13:44 +00:00
export class CustomerInvoiceNumber extends ValueObject<ICustomerInvoiceNumberProps> {
2025-04-01 14:26:15 +00:00
private static readonly MAX_LENGTH = 255;
protected static validate(value: string) {
const schema = z
.string()
.trim()
2025-06-11 15:13:44 +00:00
.max(CustomerInvoiceNumber.MAX_LENGTH, {
message: `Name must be at most ${CustomerInvoiceNumber.MAX_LENGTH} characters long`,
2025-04-01 14:26:15 +00:00
});
return schema.safeParse(value);
}
static create(value: string) {
2025-06-11 15:13:44 +00:00
const valueIsValid = CustomerInvoiceNumber.validate(value);
2025-04-01 14:26:15 +00:00
if (!valueIsValid.success) {
return Result.fail(new Error(valueIsValid.error.errors[0].message));
}
2025-06-11 15:13:44 +00:00
return Result.ok(new CustomerInvoiceNumber({ value }));
2025-04-01 14:26:15 +00:00
}
getValue(): string {
return this.props.value;
}
toString(): string {
return this.getValue();
}
2025-04-01 15:32:53 +00:00
toPrimitive() {
return this.getValue();
}
2025-04-01 14:26:15 +00:00
}