metis-desk/modules/resources/src/api/domain/resource-catalog-seed.ts

58 lines
2.4 KiB
TypeScript
Raw Normal View History

2026-10-05 11:13:51 +00:00
import type { ResourceCatalog } from "./resource-catalog.aggregate";
import type { ResourceDefinitionSpec, ResourceFieldType } from "./resource-definition";
import { ResourceDefinitionValues } from "./value-objects/resource-definition-values";
const FIELDS: readonly [string, string, ResourceFieldType][] = [
["email", "Dirección de correo", "text"],
["provider", "Proveedor", "text"],
["context", "Contexto", "text"],
["unc", "Ruta UNC", "text"],
["server", "Servidor (referencia documental)", "text"],
["description", "Descripción", "text"],
["url", "URL", "text"],
["holder", "Titular", "text"],
["issuer", "Emisor", "text"],
["expires", "Caducidad", "date"],
["fingerprint", "Huella digital", "text"],
["purpose", "Finalidad", "text"],
2026-10-05 15:01:32 +00:00
["reference", "Referencia", "text"],
2026-10-05 11:13:51 +00:00
];
const TEMPLATES: readonly [string, string, readonly string[]][] = [
["mailbox", "Buzón de correo", ["email", "provider", "context"]],
2026-10-05 15:01:32 +00:00
["access", "Recurso de acceso", ["reference"]],
2026-10-05 11:13:51 +00:00
["shared_folder", "Carpeta compartida", ["unc", "server", "description"]],
["application", "Aplicación", ["provider", "url", "description"]],
["certificate", "Certificado digital", ["holder", "issuer", "expires", "fingerprint", "purpose"]],
];
/** Solo alimenta definiciones; no crea recursos ni migra registros anteriores. */
export function seedResourceCatalog(catalog: ResourceCatalog) {
let current = catalog;
const add = (key: string, input: ResourceDefinitionSpec) => {
const values = ResourceDefinitionValues.create(input);
if (!values.isSuccess) {
throw new Error("Definición inicial de recursos inválida.");
}
const result = current.seed(key, values.data);
if (!result.isSuccess) {
throw new Error("Catálogo inicial de recursos inconsistente.");
}
current = result.data;
};
for (const [key, name, type] of FIELDS) {
add(`field:${key}`, { kind: "field", name, description: "", type, unit: "", choices: [] });
}
for (const [key, name, fields] of TEMPLATES) {
const references = fields.map((field) => {
const id = current.definitions
.find((item) => item.stockKey.getOrUndefined() === `field:${field}`)
?.id.toPrimitive();
if (!id) {
throw new Error("Campo inicial de recursos ausente.");
}
return { fieldId: id, required: false };
});
add(`template:${key}`, { kind: "template", name, description: "", fields: references });
}
return current;
}