41 lines
1.4 KiB
TypeScript
41 lines
1.4 KiB
TypeScript
|
|
import { Maybe } from "@repo/rdx-utils";
|
||
|
|
|
||
|
|
import type { ResourceCatalog } from "./resource-catalog.aggregate";
|
||
|
|
import { ResourceDefinitionValues } from "./value-objects/resource-definition-values.vo";
|
||
|
|
|
||
|
|
/** Evolución de bindings stock: conserva nombres, orden, secciones y campos personalizados. */
|
||
|
|
export function evolveOperatingSystemTemplate(catalog: ResourceCatalog) {
|
||
|
|
let current = catalog;
|
||
|
|
const template = current.definitions.find(
|
||
|
|
(item) => item.stockKey.getOrUndefined() === "template:os"
|
||
|
|
);
|
||
|
|
const data = template?.values.data;
|
||
|
|
|
||
|
|
if (template && data?.kind === "template") {
|
||
|
|
const fields = [...data.fields];
|
||
|
|
|
||
|
|
for (const key of ["os_family", "os_product_version", "os_edition_state"]) {
|
||
|
|
const field = current.definitions.find(
|
||
|
|
(item) => item.stockKey.getOrUndefined() === `field:${key}`
|
||
|
|
);
|
||
|
|
|
||
|
|
if (field && !fields.some((entry) => entry.fieldId === field.id.toPrimitive())) {
|
||
|
|
fields.push({ fieldId: field.id.toPrimitive(), required: false });
|
||
|
|
}
|
||
|
|
}
|
||
|
|
const values = ResourceDefinitionValues.create({ ...data, fields });
|
||
|
|
|
||
|
|
if (!values.isSuccess) {
|
||
|
|
throw new Error("Evolución de plantilla SO inválida.");
|
||
|
|
}
|
||
|
|
const updated = current.save(template.companyId, Maybe.some(template.id), values.data);
|
||
|
|
|
||
|
|
if (!updated.isSuccess) {
|
||
|
|
throw new Error("Evolución de plantilla SO rechazada.");
|
||
|
|
}
|
||
|
|
current = updated.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
return current;
|
||
|
|
}
|