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

49 lines
1.2 KiB
TypeScript
Raw Normal View History

2025-08-11 17:49:52 +00:00
import { DomainValidationError } from "@erp/core/api";
import { ValueObject } from "@repo/rdx-ddd";
import { Result } from "@repo/rdx-utils";
import * as z from "zod/v4";
interface ICustomerNumberProps {
value: string;
}
export class CustomerNumber extends ValueObject<ICustomerNumberProps> {
private static readonly MAX_LENGTH = 255;
private static readonly FIELD = "invoiceNumber";
private static readonly ERROR_CODE = "INVALID_INVOICE_NUMBER";
protected static validate(value: string) {
const schema = z
.string()
.trim()
.max(CustomerNumber.MAX_LENGTH, {
message: `String must be at most ${CustomerNumber.MAX_LENGTH} characters long`,
});
return schema.safeParse(value);
}
static create(value: string) {
const result = CustomerNumber.validate(value);
if (!result.success) {
const detail = result.error.message;
return Result.fail(
new DomainValidationError(CustomerNumber.ERROR_CODE, CustomerNumber.FIELD, detail)
);
}
return Result.ok(new CustomerNumber({ value }));
}
2025-09-04 10:02:24 +00:00
getProps(): string {
2025-08-11 17:49:52 +00:00
return this.props.value;
}
2025-09-10 18:14:19 +00:00
toString() {
return String(this.props.value);
2025-08-11 17:49:52 +00:00
}
toPrimitive() {
2025-09-04 10:02:24 +00:00
return this.getProps();
2025-08-11 17:49:52 +00:00
}
}