import { Name, UniqueID } from "@repo/rdx-ddd"; import { Maybe, Result } from "@repo/rdx-utils"; import { AssetCatalogError } from "../asset-definition"; import { type WorkstationInput, WorkstationInputSchema } from "../workstation-values"; export class WorkstationValues { private constructor( public readonly name: Name, public readonly active: boolean, public readonly siteId: Maybe, public readonly personId: Maybe, public readonly assetIds: readonly UniqueID[], public readonly sharedAssetIds: readonly UniqueID[] ) { Object.freeze(this); } static create(input: WorkstationInput): Result { const parsed = WorkstationInputSchema.safeParse(input); const name = Name.create(input.name, 100); const site = optionalIdentity(input.siteId); const person = optionalIdentity(input.personId); if (!(parsed.success && name.isSuccess && site.isSuccess && person.isSuccess)) { return Result.fail(new AssetCatalogError("INVALID_REQUEST")); } const ids: UniqueID[] = []; const shared: UniqueID[] = []; for (const value of parsed.data.assetIds) { const id = UniqueID.create(value); if (!id.isSuccess || ids.some((previous) => previous.equals(id.data))) { return Result.fail(new AssetCatalogError("INVALID_MEMBERS")); } ids.push(id.data); } for (const value of parsed.data.sharedAssetIds) { const id = UniqueID.create(value); if (!id.isSuccess || [...ids, ...shared].some((previous) => previous.equals(id.data))) { return Result.fail(new AssetCatalogError("INVALID_MEMBERS")); } shared.push(id.data); } return Result.ok( new WorkstationValues( name.data, input.active, site.data, person.data, Object.freeze(ids), Object.freeze(shared) ) ); } toPrimitive(): WorkstationInput { return { 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()), sharedAssetIds: this.sharedAssetIds.map((id) => id.toPrimitive()), }; } equals(other: WorkstationValues) { const a = this.toPrimitive(); const b = other.toPrimitive(); return ( 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)) && a.sharedAssetIds.length === b.sharedAssetIds.length && a.sharedAssetIds.every((id) => b.sharedAssetIds.includes(id)) ); } } function optionalIdentity(value: string | null): Result, AssetCatalogError> { if (!value) { return Result.ok(Maybe.none()); } const id = UniqueID.create(value); return id.isSuccess ? Result.ok(Maybe.some(id.data)) : Result.fail(new AssetCatalogError("INVALID_REQUEST")); }