metis-desk/modules/assets/src/api/domain/value-objects/workstation-values.vo.ts

104 lines
3.0 KiB
TypeScript
Raw Normal View History

2026-10-04 19:04:14 +00:00
import { Name, UniqueID } from "@repo/rdx-ddd";
import { Maybe, Result } from "@repo/rdx-utils";
import { AssetCatalogError } from "../asset-definition";
2026-10-08 14:43:34 +00:00
import type { WorkstationInput } from "../workstation-values";
import { WorkstationInputSchema } from "../workstation-values";
2026-10-10 10:01:58 +00:00
2026-10-04 19:04:14 +00:00
export class WorkstationValues {
private constructor(
public readonly computerAssetId: Maybe<UniqueID>,
2026-10-04 19:04:14 +00:00
public readonly name: Name,
public readonly active: boolean,
public readonly siteId: Maybe<UniqueID>,
public readonly personId: Maybe<UniqueID>,
public readonly assetIds: readonly UniqueID[]
2026-10-04 19:04:14 +00:00
) {
Object.freeze(this);
}
2026-10-10 10:01:58 +00:00
2026-10-04 19:04:14 +00:00
static create(input: WorkstationInput): Result<WorkstationValues, AssetCatalogError> {
const parsed = WorkstationInputSchema.safeParse(input);
const name = Name.create(input.name, 100);
const site = optionalIdentity(input.siteId);
const person = optionalIdentity(input.personId);
const computer = optionalIdentity(input.computerAssetId ?? null);
2026-10-10 10:01:58 +00:00
if (
!(
parsed.success &&
name.isSuccess &&
site.isSuccess &&
person.isSuccess &&
computer.isSuccess
)
) {
2026-10-04 19:04:14 +00:00
return Result.fail(new AssetCatalogError("INVALID_REQUEST"));
}
const ids: UniqueID[] = [];
2026-10-10 10:01:58 +00:00
2026-10-04 19:04:14 +00:00
for (const value of parsed.data.assetIds) {
const id = UniqueID.create(value);
2026-10-10 10:01:58 +00:00
2026-10-04 19:04:14 +00:00
if (!id.isSuccess || ids.some((previous) => previous.equals(id.data))) {
return Result.fail(new AssetCatalogError("INVALID_MEMBERS"));
}
ids.push(id.data);
}
2026-10-10 10:01:58 +00:00
if (input.computerAssetId && !input.assetIds.includes(input.computerAssetId)) {
return Result.fail(new AssetCatalogError("INVALID_MEMBERS"));
}
2026-10-04 19:04:14 +00:00
return Result.ok(
2026-10-05 07:53:29 +00:00
new WorkstationValues(
computer.data,
2026-10-05 07:53:29 +00:00
name.data,
input.active,
site.data,
person.data,
Object.freeze(ids)
2026-10-05 07:53:29 +00:00
)
2026-10-04 19:04:14 +00:00
);
}
2026-10-10 10:01:58 +00:00
2026-10-04 19:04:14 +00:00
toPrimitive(): WorkstationInput {
return {
...(this.computerAssetId.getOrUndefined()
? { computerAssetId: this.computerAssetId.getOrUndefined()?.toPrimitive() }
: {}),
2026-10-04 19:04:14 +00:00
name: this.name.toPrimitive(),
active: this.active,
siteId: this.siteId.getOrUndefined()?.toPrimitive() ?? null,
personId: this.personId.getOrUndefined()?.toPrimitive() ?? null,
assetIds: this.assetIds.map((id) => id.toPrimitive()),
};
}
2026-10-10 10:01:58 +00:00
2026-10-04 19:04:14 +00:00
equals(other: WorkstationValues) {
const a = this.toPrimitive();
const b = other.toPrimitive();
2026-10-10 10:01:58 +00:00
2026-10-04 19:04:14 +00:00
return (
a.computerAssetId === b.computerAssetId &&
2026-10-04 19:04:14 +00:00
a.name === b.name &&
a.active === b.active &&
a.siteId === b.siteId &&
a.personId === b.personId &&
a.assetIds.length === b.assetIds.length &&
a.assetIds.every((id) => b.assetIds.includes(id))
2026-10-04 19:04:14 +00:00
);
}
}
function optionalIdentity(value: string | null): Result<Maybe<UniqueID>, AssetCatalogError> {
if (!value) {
return Result.ok(Maybe.none());
}
const id = UniqueID.create(value);
2026-10-10 10:01:58 +00:00
2026-10-04 19:04:14 +00:00
return id.isSuccess
? Result.ok(Maybe.some(id.data))
: Result.fail(new AssetCatalogError("INVALID_REQUEST"));
}