import { DomainEntity, Name, UniqueID } from "@repo/rdx-ddd"; import { Maybe } from "@repo/rdx-utils"; import type { ProcessReferenceInput, ProcessTaskInput } from "./process-values"; import { ProcessReferenceSchema, ProcessTaskSchema } from "./process-values"; class ProcessReferenceValues { private constructor( public readonly id: UniqueID, public readonly label: Name, public readonly kind: ProcessReferenceInput["kind"], public readonly sourceKind: Maybe ) { Object.freeze(this); } static create(input: ProcessReferenceInput) { const value = ProcessReferenceSchema.parse(input); const id = UniqueID.create(value.id); const label = Name.create(value.label, 500); if (!(id.isSuccess && label.isSuccess)) { throw new Error("Referencia de tarea inconsistente."); } return new ProcessReferenceValues( id.data, label.data, value.kind, value.sourceKind ? Maybe.some(value.sourceKind) : Maybe.none() ); } toPrimitive(): ProcessReferenceInput { const sourceKind = this.sourceKind.getOrUndefined(); return { kind: this.kind, id: this.id.toPrimitive(), label: this.label.toPrimitive(), ...(sourceKind ? { sourceKind } : {}), }; } equals(other: ProcessReferenceValues) { return ( this.id.equals(other.id) && this.label.equals(other.label) && this.kind === other.kind && this.sourceKind.getOrUndefined() === other.sourceKind.getOrUndefined() ); } } type Values = { title: Name; status: ProcessTaskInput["status"]; notes: Maybe; reference: Maybe; }; function mapped(value: Omit): Values { const title = Name.create(value.title, 160); if (!title.isSuccess) { throw new Error("Título de tarea inconsistente."); } return { title: title.data, status: value.status, notes: value.notes ? Maybe.some(value.notes) : Maybe.none(), reference: value.reference ? Maybe.some(ProcessReferenceValues.create(value.reference)) : Maybe.none(), }; } export class ProcessTask extends DomainEntity { static create(title: string, reference: ProcessReferenceInput | null = null) { return new ProcessTask( Object.freeze(mapped({ title, status: "pending", notes: null, reference })) ); } static rehydrate(input: ProcessTaskInput) { const value = ProcessTaskSchema.parse(input); const id = UniqueID.create(value.id); if (!id.isSuccess) { throw new Error("Identidad de tarea inconsistente."); } return new ProcessTask(Object.freeze(mapped(value)), id.data); } toPrimitive(): ProcessTaskInput { return { id: this.id.toPrimitive(), title: this.props.title.toPrimitive(), status: this.props.status, notes: this.props.notes.getOrUndefined() ?? null, reference: this.props.reference.getOrUndefined()?.toPrimitive() ?? null, }; } sameValues(other: ProcessTask) { const a = this.props; const b = other.props; const ref = a.reference.getOrUndefined(); const otherRef = b.reference.getOrUndefined(); const referencesEqual = ref ? Boolean(otherRef && ref.equals(otherRef)) : !otherRef; return ( this.id.equals(other.id) && a.title.equals(b.title) && a.status === b.status && a.notes.getOrUndefined() === b.notes.getOrUndefined() && referencesEqual ); } }