2025-05-20 10:08:24 +00:00
|
|
|
import { ValueObject } from "@repo/rdx-ddd";
|
2025-05-09 10:45:32 +00:00
|
|
|
import { Maybe, Result } from "@repo/rdx-utils";
|
2025-04-01 14:26:15 +00:00
|
|
|
import { z } from "zod";
|
|
|
|
|
|
|
|
|
|
interface IInvoiceSerieProps {
|
|
|
|
|
value: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class InvoiceSerie extends ValueObject<IInvoiceSerieProps> {
|
|
|
|
|
private static readonly MAX_LENGTH = 255;
|
|
|
|
|
|
|
|
|
|
protected static validate(value: string) {
|
|
|
|
|
const schema = z
|
|
|
|
|
.string()
|
|
|
|
|
.trim()
|
|
|
|
|
.max(InvoiceSerie.MAX_LENGTH, {
|
|
|
|
|
message: `Name must be at most ${InvoiceSerie.MAX_LENGTH} characters long`,
|
|
|
|
|
});
|
|
|
|
|
return schema.safeParse(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static create(value: string) {
|
|
|
|
|
const valueIsValid = InvoiceSerie.validate(value);
|
|
|
|
|
|
|
|
|
|
if (!valueIsValid.success) {
|
|
|
|
|
return Result.fail(new Error(valueIsValid.error.errors[0].message));
|
|
|
|
|
}
|
|
|
|
|
return Result.ok(new InvoiceSerie({ value }));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static createNullable(value?: string): Result<Maybe<InvoiceSerie>, Error> {
|
|
|
|
|
if (!value || value.trim() === "") {
|
|
|
|
|
return Result.ok(Maybe.none<InvoiceSerie>());
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-09 10:45:32 +00:00
|
|
|
return InvoiceSerie.create(value).map((value) => Maybe.some(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
|
|
|
}
|