metis-desk/modules/resources/src/api/application/resource-catalog-response.builder.ts

34 lines
1.1 KiB
TypeScript
Raw Normal View History

2026-10-05 11:13:51 +00:00
import type { UniqueID } from "@repo/rdx-ddd";
import type { Maybe } from "@repo/rdx-utils";
import type { CatalogResponseDTO } from "../../common";
import type { ResourceCatalog } from "../domain/resource-catalog.aggregate";
2026-10-10 10:01:58 +00:00
2026-10-05 11:13:51 +00:00
export function buildCatalogResponse(
catalog: ResourceCatalog,
scope: Maybe<UniqueID>
): CatalogResponseDTO {
return {
items: catalog.visible(scope).map((item) => ({
id: item.id.toPrimitive(),
companyId: item.companyId.getOrUndefined()?.toPrimitive() ?? null,
builtin: item.stockKey.getOrUndefined() !== undefined,
2026-10-05 15:01:32 +00:00
builtinKey: item.stockKey.getOrUndefined() ?? null,
2026-10-05 11:13:51 +00:00
active: item.active,
effectiveActive: catalog.active(item, scope),
overridden: catalog.availability.some(
(entry) =>
entry.companyId.equals(scope.getOrUndefined()) &&
entry.definitionId.equals(item.id) &&
entry.active.getOrUndefined() !== undefined
),
values: item.values.data,
bindings: item.bindings.map((binding) => ({
id: binding.id.toPrimitive(),
fieldId: binding.fieldId.toPrimitive(),
required: binding.required,
})),
})),
};
}