metis-desk/modules/companies/src/api/domain/intervention-template-step.entity.ts

58 lines
1.8 KiB
TypeScript

import { DomainEntity, Name, UniqueID } from "@repo/rdx-ddd";
import { Result } from "@repo/rdx-utils";
import {
type InterventionStepInput,
InterventionStepInputSchema,
InterventionTemplateError,
} from "./intervention-template-values";
type Props = Readonly<{ title: Name; instructions: string; required: boolean }>;
export class InterventionTemplateStep extends DomainEntity<Props> {
private constructor(props: Props, id: UniqueID) {
super(Object.freeze({ ...props }), id);
}
static create(
input: InterventionStepInput
): Result<InterventionTemplateStep, InterventionTemplateError> {
const parsed = InterventionStepInputSchema.safeParse(input);
if (!parsed.success) {
return Result.fail(new InterventionTemplateError("INVALID_REQUEST"));
}
const title = Name.create(parsed.data.title, 200);
const id = UniqueID.create(parsed.data.id);
if (!(title.isSuccess && id.isSuccess)) {
return Result.fail(new InterventionTemplateError("INVALID_REQUEST"));
}
return Result.ok(
new InterventionTemplateStep(
{
title: title.data,
instructions: parsed.data.instructions,
required: parsed.data.required,
},
id.data
)
);
}
static rehydrate(props: Props, id: UniqueID) {
return new InterventionTemplateStep(props, id);
}
equals(other: InterventionTemplateStep) {
return (
this.id.equals(other.id) &&
this.props.title.equals(other.props.title) &&
this.props.instructions === other.props.instructions &&
this.props.required === other.props.required
);
}
toPrimitive(): InterventionStepInput {
return {
id: this.id.toPrimitive(),
title: this.props.title.toPrimitive(),
instructions: this.props.instructions,
required: this.props.required,
};
}
}