metis-desk/modules/resources/src/api/domain/resource.aggregate.ts

150 lines
4.0 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-05 11:13:51 +00:00
import { Result } from "@repo/rdx-utils";
import { ResourceCatalogError } from "./resource-definition";
import { ResourceRevision } from "./resource-revision.entity";
import type { ResourceSpecification } from "./value-objects/resource-values.vo";
import { ResourceValues } from "./value-objects/resource-values.vo";
2026-10-05 11:13:51 +00:00
type ResourceProps = Readonly<{
companyId: UniqueID;
specification: ResourceSpecification;
values: ResourceValues;
history: readonly ResourceRevision[];
}>;
2026-10-10 10:01:58 +00:00
2026-10-05 11:13:51 +00:00
export class Resource extends AggregateRoot<ResourceProps> {
private constructor(props: ResourceProps, id?: UniqueID) {
super({ ...props, history: Object.freeze([...props.history]) }, id);
}
2026-10-10 10:01:58 +00:00
2026-10-05 11:13:51 +00:00
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, specification, actorId, recordedAt })],
2026-10-05 11:13:51 +00:00
})
);
}
2026-10-10 10:01:58 +00:00
2026-10-05 11:13:51 +00:00
static rehydrate(props: ResourceProps, id: UniqueID) {
return new Resource(props, id);
}
2026-10-10 10:01:58 +00:00
2026-10-05 11:13:51 +00:00
get companyId() {
return this.props.companyId;
}
2026-10-10 10:01:58 +00:00
2026-10-05 11:13:51 +00:00
get specification() {
return this.props.specification;
}
2026-10-10 10:01:58 +00:00
2026-10-05 11:13:51 +00:00
get values() {
return this.props.values;
}
2026-10-10 10:01:58 +00:00
2026-10-05 11:13:51 +00:00
get history() {
return this.props.history;
}
2026-10-10 10:01:58 +00:00
synchronizeSpecification(
specification: ResourceSpecification,
actorId: UniqueID,
recordedAt: string
) {
const allowed = new Set(specification.toPrimitive().fields.map((field) => field.fieldId));
const values = ResourceValues.reconstruct(
{
...this.values.toPrimitive(),
characteristics: Object.fromEntries(
Object.entries(this.values.toPrimitive().characteristics).filter(([id]) =>
allowed.has(id)
)
),
},
specification
);
if (!values.isSuccess) {
return values;
}
if (this.specification.equals(specification) && this.values.equals(values.data)) {
return Result.ok(this);
}
return Result.ok(
new Resource(
{
...this.props,
specification,
values: values.data,
history: [
...this.history,
ResourceRevision.create({ specification, values: values.data, actorId, recordedAt }),
],
},
this.id
)
);
}
2026-10-05 11:13:51 +00:00
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"));
}
2026-10-05 15:01:32 +00:00
const previous = this.values.toPrimitive().integration;
const next = values.toPrimitive().integration;
2026-10-10 10:01:58 +00:00
2026-10-05 15:01:32 +00:00
if (
previous?.kind !== next?.kind ||
(previous?.kind === "mailbox" &&
next?.kind === "mailbox" &&
(previous.mailboxKind !== next.mailboxKind ||
previous.addressFieldId !== next.addressFieldId ||
previous.contextFieldId !== next.contextFieldId)) ||
(previous?.kind === "access" &&
next?.kind === "access" &&
(previous.referenceFieldId !== next.referenceFieldId ||
previous.resourceTypeId !== next.resourceTypeId ||
previous.systemId !== next.systemId ||
previous.environmentId !== next.environmentId))
) {
return Result.fail(new ResourceCatalogError("CONTEXT_FIXED"));
}
2026-10-10 10:01:58 +00:00
2026-10-05 11:13:51 +00:00
return Result.ok(
this.values.equals(values)
? this
: new Resource(
{
...this.props,
values,
history: [
...this.history,
ResourceRevision.create({
values,
specification: this.specification,
actorId,
recordedAt,
}),
],
2026-10-05 11:13:51 +00:00
},
this.id
)
);
}
}