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

53 lines
1.3 KiB
TypeScript
Raw Normal View History

2025-06-24 18:38:57 +00:00
import { DomainValidationError } from "@erp/core/api";
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-06-24 18:38:57 +00:00
import * as z from "zod/v4";
2025-04-01 14:26:15 +00:00
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;
2025-06-24 18:38:57 +00:00
private static readonly FIELD = "invoiceNumber";
private static readonly ERROR_CODE = "INVALID_INVOICE_NUMBER";
2025-04-01 14:26:15 +00:00
protected static validate(value: string) {
const schema = z
.string()
.trim()
2025-06-11 15:13:44 +00:00
.max(CustomerInvoiceNumber.MAX_LENGTH, {
2025-06-24 18:38:57 +00:00
message: `String 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-09-03 10:41:12 +00:00
const valueIsValid = CustomerInvoiceNumber.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(
CustomerInvoiceNumber.ERROR_CODE,
CustomerInvoiceNumber.FIELD,
detail
)
);
2025-04-01 14:26:15 +00:00
}
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
}