2026-10-24 23:19:17 +00:00
|
|
|
import { Result } from "@repo/rdx-utils";
|
|
|
|
|
|
|
|
|
|
import type { ResourceCatalog } from "../domain/resource-catalog.aggregate";
|
|
|
|
|
import { ResourceCatalogError } from "../domain/resource-definition";
|
|
|
|
|
import { ResourceSpecification } from "../domain/value-objects/resource-values.vo";
|
|
|
|
|
|
|
|
|
|
/** Captura exclusivamente la plantilla de serie y campos de propietario global. */
|
|
|
|
|
export function captureGlobalResourceSpecification(
|
|
|
|
|
catalog: ResourceCatalog,
|
|
|
|
|
templateId: string,
|
2026-10-25 19:39:06 +00:00
|
|
|
options: { existing: boolean; kind?: "application" | "os" }
|
2026-10-24 23:19:17 +00:00
|
|
|
) {
|
|
|
|
|
const template = catalog.definitions.find(
|
|
|
|
|
(item) => item.id.toPrimitive() === templateId.toLowerCase()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
!template ||
|
2026-10-25 19:39:06 +00:00
|
|
|
template.stockKey.getOrUndefined() !== `template:${options.kind ?? "application"}` ||
|
2026-10-24 23:19:17 +00:00
|
|
|
template.companyId.getOrUndefined() ||
|
|
|
|
|
template.values.data.kind !== "template"
|
|
|
|
|
) {
|
|
|
|
|
return Result.fail(new ResourceCatalogError("RESOURCE_GLOBAL_TEMPLATE_NOT_ALLOWED"));
|
|
|
|
|
}
|
|
|
|
|
if (!(options.existing || template.active)) {
|
|
|
|
|
return Result.fail(new ResourceCatalogError("TEMPLATE_UNAVAILABLE"));
|
|
|
|
|
}
|
2026-10-25 19:39:06 +00:00
|
|
|
const fields: ReturnType<ResourceSpecification["toPrimitive"]>["fields"] = [];
|
2026-10-24 23:19:17 +00:00
|
|
|
|
|
|
|
|
for (const binding of template.values.data.fields) {
|
|
|
|
|
const field = catalog.definitions.find((item) => item.id.toPrimitive() === binding.fieldId);
|
|
|
|
|
|
|
|
|
|
if (!field || field.companyId.getOrUndefined() || field.values.data.kind !== "field") {
|
|
|
|
|
return Result.fail(new ResourceCatalogError("RESOURCE_GLOBAL_TEMPLATE_NOT_ALLOWED"));
|
|
|
|
|
}
|
|
|
|
|
fields.push({
|
|
|
|
|
fieldId: field.id.toPrimitive(),
|
|
|
|
|
name: field.values.data.name,
|
|
|
|
|
type: field.values.data.type,
|
|
|
|
|
unit: field.values.data.unit,
|
|
|
|
|
choices: [...field.values.data.choices],
|
|
|
|
|
required: binding.required,
|
|
|
|
|
...(binding.sectionId ? { sectionId: binding.sectionId } : {}),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-25 19:39:06 +00:00
|
|
|
if (
|
|
|
|
|
options.kind === "os" &&
|
|
|
|
|
["provider", "os_family", "os_product_version", "os_edition_state", "software_edition"].some(
|
|
|
|
|
(key) => {
|
|
|
|
|
const field = catalog.definitions.find(
|
|
|
|
|
(item) => item.stockKey.getOrUndefined() === `field:${key}`
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return !(field && fields.some((binding) => binding.fieldId === field.id.toPrimitive()));
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
return Result.fail(new ResourceCatalogError("RESOURCE_GLOBAL_TEMPLATE_NOT_ALLOWED"));
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-24 23:19:17 +00:00
|
|
|
return ResourceSpecification.create({
|
|
|
|
|
templateId: template.id.toPrimitive(),
|
|
|
|
|
templateName: template.values.data.name,
|
|
|
|
|
...(template.values.data.sections ? { sections: [...template.values.data.sections] } : {}),
|
|
|
|
|
fields,
|
|
|
|
|
});
|
|
|
|
|
}
|