metis-desk/modules/assets/src/api/domain/workstation.aggregate.ts

74 lines
2.1 KiB
TypeScript
Raw Normal View History

2026-10-08 14:43:34 +00:00
import type { UniqueID } from "@repo/rdx-ddd";
import { AggregateRoot } from "@repo/rdx-ddd";
2026-10-04 19:04:14 +00:00
import { Result } from "@repo/rdx-utils";
import { z } from "zod/v4";
import { AssetCatalogError } from "./asset-definition";
2026-10-09 08:38:19 +00:00
import type { WorkstationValues } from "./value-objects/workstation-values.vo";
2026-10-04 19:04:14 +00:00
import { WorkstationRevision } from "./workstation-revision.entity";
type WorkstationProps = Readonly<{
companyId: UniqueID;
values: WorkstationValues;
history: readonly WorkstationRevision[];
}>;
export class Workstation extends AggregateRoot<WorkstationProps> {
private constructor(props: WorkstationProps, id?: UniqueID) {
super(Object.freeze({ ...props, history: Object.freeze([...props.history]) }), id);
}
static create(
companyId: UniqueID,
values: WorkstationValues,
actorId: UniqueID,
recordedAt: string
): Result<Workstation, AssetCatalogError> {
if (!z.iso.datetime().safeParse(recordedAt).success) {
return Result.fail(new AssetCatalogError("INVALID_REQUEST"));
}
return Result.ok(
new Workstation({
companyId,
values,
history: [WorkstationRevision.create({ values, actorId, recordedAt })],
})
);
}
static rehydrate(props: WorkstationProps, id: UniqueID) {
return new Workstation(props, id);
}
get companyId() {
return this.props.companyId;
}
get values() {
return this.props.values;
}
get history() {
return this.props.history;
}
update(
values: WorkstationValues,
actorId: UniqueID,
recordedAt: string
): Result<Workstation, AssetCatalogError> {
if (this.values.equals(values)) {
return Result.ok(this);
}
if (
!z.iso.datetime().safeParse(recordedAt).success ||
recordedAt < (this.history.at(-1)?.recordedAt ?? "")
) {
return Result.fail(new AssetCatalogError("INVALID_REQUEST"));
}
return Result.ok(
new Workstation(
{
...this.props,
values,
history: [...this.history, WorkstationRevision.create({ values, actorId, recordedAt })],
},
this.id
)
);
}
}