Uecko_ERP/modules/customer-invoices/src/api/domain/value-objects/customer-invoice-serie.ts

60 lines
1.6 KiB
TypeScript
Raw Normal View History

2025-09-30 11:05:12 +00:00
import { DomainValidationError, ValueObject } from "@repo/rdx-ddd";
2025-05-09 10:45:32 +00:00
import { Maybe, Result } from "@repo/rdx-utils";
2025-09-24 17:30:35 +00:00
import { z } from "zod/v4";
2025-04-01 14:26:15 +00:00
2025-06-11 15:13:44 +00:00
interface ICustomerInvoiceSerieProps {
2025-04-01 14:26:15 +00:00
value: string;
}
2025-06-11 15:13:44 +00:00
export class CustomerInvoiceSerie extends ValueObject<ICustomerInvoiceSerieProps> {
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 = "invoiceSeries";
private static readonly ERROR_CODE = "INVALID_INVOICE_SERIE";
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(CustomerInvoiceSerie.MAX_LENGTH, {
2025-06-24 18:38:57 +00:00
message: `String must be at most ${CustomerInvoiceSerie.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 = CustomerInvoiceSerie.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(
CustomerInvoiceSerie.ERROR_CODE,
CustomerInvoiceSerie.FIELD,
detail
)
);
2025-04-01 14:26:15 +00:00
}
2025-06-11 15:13:44 +00:00
return Result.ok(new CustomerInvoiceSerie({ value }));
2025-04-01 14:26:15 +00:00
}
2025-06-11 15:13:44 +00:00
static createNullable(value?: string): Result<Maybe<CustomerInvoiceSerie>, Error> {
2025-04-01 14:26:15 +00:00
if (!value || value.trim() === "") {
2025-06-11 15:13:44 +00:00
return Result.ok(Maybe.none<CustomerInvoiceSerie>());
2025-04-01 14:26:15 +00:00
}
2025-06-11 15:13:44 +00:00
return CustomerInvoiceSerie.create(value).map((value) => Maybe.some(value));
2025-04-01 14:26:15 +00:00
}
2025-09-04 10:02:24 +00:00
getProps(): string {
2025-04-01 14:26:15 +00:00
return this.props.value;
}
2025-09-10 18:14:19 +00:00
toString() {
return String(this.props.value);
2025-04-01 14:26:15 +00:00
}
2025-04-01 15:32:53 +00:00
toPrimitive() {
2025-09-04 10:02:24 +00:00
return this.getProps();
2025-04-01 15:32:53 +00:00
}
2025-04-01 14:26:15 +00:00
}