56 lines
2.3 KiB
TypeScript
56 lines
2.3 KiB
TypeScript
|
|
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"],
|
||
|
|
];
|
||
|
|
const TEMPLATES: readonly [string, string, readonly string[]][] = [
|
||
|
|
["mailbox", "Buzón de correo", ["email", "provider", "context"]],
|
||
|
|
["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;
|
||
|
|
}
|