71 lines
1.9 KiB
TypeScript
71 lines
1.9 KiB
TypeScript
|
|
import { AggregateRoot, type UniqueID } from "@repo/rdx-ddd";
|
||
|
|
import { Result } from "@repo/rdx-utils";
|
||
|
|
|
||
|
|
import { ResourceCatalogError } from "./resource-definition";
|
||
|
|
import { ResourceRevision } from "./resource-revision.entity";
|
||
|
|
import type { ResourceSpecification, ResourceValues } from "./value-objects/resource-values";
|
||
|
|
|
||
|
|
type ResourceProps = Readonly<{
|
||
|
|
companyId: UniqueID;
|
||
|
|
specification: ResourceSpecification;
|
||
|
|
values: ResourceValues;
|
||
|
|
history: readonly ResourceRevision[];
|
||
|
|
}>;
|
||
|
|
export class Resource extends AggregateRoot<ResourceProps> {
|
||
|
|
private constructor(props: ResourceProps, id?: UniqueID) {
|
||
|
|
super({ ...props, history: Object.freeze([...props.history]) }, id);
|
||
|
|
}
|
||
|
|
static create(
|
||
|
|
companyId: UniqueID,
|
||
|
|
specification: ResourceSpecification,
|
||
|
|
values: ResourceValues,
|
||
|
|
actorId: UniqueID,
|
||
|
|
recordedAt: string
|
||
|
|
) {
|
||
|
|
return Result.ok<Resource>(
|
||
|
|
new Resource({
|
||
|
|
companyId,
|
||
|
|
specification,
|
||
|
|
values,
|
||
|
|
history: [ResourceRevision.create({ values, actorId, recordedAt })],
|
||
|
|
})
|
||
|
|
);
|
||
|
|
}
|
||
|
|
static rehydrate(props: ResourceProps, id: UniqueID) {
|
||
|
|
return new Resource(props, id);
|
||
|
|
}
|
||
|
|
get companyId() {
|
||
|
|
return this.props.companyId;
|
||
|
|
}
|
||
|
|
get specification() {
|
||
|
|
return this.props.specification;
|
||
|
|
}
|
||
|
|
get values() {
|
||
|
|
return this.props.values;
|
||
|
|
}
|
||
|
|
get history() {
|
||
|
|
return this.props.history;
|
||
|
|
}
|
||
|
|
update(
|
||
|
|
values: ResourceValues,
|
||
|
|
actorId: UniqueID,
|
||
|
|
recordedAt: string
|
||
|
|
): Result<Resource, ResourceCatalogError> {
|
||
|
|
if (values.toPrimitive().templateId !== this.values.toPrimitive().templateId) {
|
||
|
|
return Result.fail(new ResourceCatalogError("TEMPLATE_FIXED"));
|
||
|
|
}
|
||
|
|
return Result.ok(
|
||
|
|
this.values.equals(values)
|
||
|
|
? this
|
||
|
|
: new Resource(
|
||
|
|
{
|
||
|
|
...this.props,
|
||
|
|
values,
|
||
|
|
history: [...this.history, ResourceRevision.create({ values, actorId, recordedAt })],
|
||
|
|
},
|
||
|
|
this.id
|
||
|
|
)
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|