90 lines
2.7 KiB
TypeScript
90 lines
2.7 KiB
TypeScript
import type { UniqueID } from "@repo/rdx-ddd";
|
|
import { Result } from "@repo/rdx-utils";
|
|
|
|
import type { Asset } from "../domain/asset.aggregate";
|
|
import type { AssetCatalog } from "../domain/asset-catalog.aggregate";
|
|
import { AssetCatalogError } from "../domain/asset-definition";
|
|
import type { AssetRelationship } from "../domain/asset-relationship.aggregate";
|
|
import type { Brand } from "../domain/brand.aggregate";
|
|
import type { AssetValues } from "../domain/value-objects/asset-values.vo";
|
|
import type { Workstation } from "../domain/workstation.aggregate";
|
|
|
|
export type InventoryContext = Readonly<{
|
|
workstations: readonly Workstation[];
|
|
catalog: AssetCatalog;
|
|
brands: readonly Brand[];
|
|
assets: readonly Asset[];
|
|
relationships: readonly AssetRelationship[];
|
|
sites: readonly { id: UniqueID; active: boolean }[];
|
|
people: readonly { id: UniqueID; active: boolean }[];
|
|
actorNames: ReadonlyMap<string, string>;
|
|
}>;
|
|
|
|
export interface IAssetRepository {
|
|
remove(
|
|
company: UniqueID,
|
|
id: UniqueID
|
|
): Promise<
|
|
Result<
|
|
{
|
|
asset: Asset;
|
|
actorNames: ReadonlyMap<string, string>;
|
|
workstations: readonly Workstation[];
|
|
},
|
|
AssetCatalogError
|
|
>
|
|
>;
|
|
removeWorkstation(company: UniqueID, id: UniqueID): Promise<Result<void, AssetCatalogError>>;
|
|
removeRelationship(company: UniqueID, id: UniqueID): Promise<Result<void, AssetCatalogError>>;
|
|
saveWorkstation(
|
|
companyId: UniqueID,
|
|
action: (context: InventoryContext) => Result<Workstation, AssetCatalogError>
|
|
): Promise<
|
|
Result<{ workstation: Workstation; actorNames: ReadonlyMap<string, string> }, AssetCatalogError>
|
|
>;
|
|
get(companyId: UniqueID): Promise<Result<InventoryContext, AssetCatalogError>>;
|
|
saveRelationship(
|
|
companyId: UniqueID,
|
|
action: (context: InventoryContext) => Result<AssetRelationship, AssetCatalogError>
|
|
): Promise<
|
|
Result<
|
|
{ relationship: AssetRelationship; actorNames: ReadonlyMap<string, string> },
|
|
AssetCatalogError
|
|
>
|
|
>;
|
|
save(
|
|
companyId: UniqueID,
|
|
action: (context: InventoryContext) => Result<Asset, AssetCatalogError>
|
|
): Promise<
|
|
Result<
|
|
{
|
|
asset: Asset;
|
|
actorNames: ReadonlyMap<string, string>;
|
|
workstations: readonly Workstation[];
|
|
},
|
|
AssetCatalogError
|
|
>
|
|
>;
|
|
}
|
|
|
|
export function validateAssetReferences(
|
|
context: InventoryContext,
|
|
values: AssetValues,
|
|
previous?: Asset
|
|
) {
|
|
const next = values.toPrimitive();
|
|
|
|
if (
|
|
next.siteId &&
|
|
!context.sites.some(
|
|
(site) =>
|
|
site.id.toPrimitive() === next.siteId &&
|
|
(site.active || previous?.values.toPrimitive().siteId === next.siteId)
|
|
)
|
|
) {
|
|
return Result.fail(new AssetCatalogError("SITE_NOT_FOUND"));
|
|
}
|
|
|
|
return Result.ok<true>(true);
|
|
}
|