54 lines
1.9 KiB
TypeScript
54 lines
1.9 KiB
TypeScript
|
|
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,
|
||
|
|
options: { existing: boolean }
|
||
|
|
) {
|
||
|
|
const template = catalog.definitions.find(
|
||
|
|
(item) => item.id.toPrimitive() === templateId.toLowerCase()
|
||
|
|
);
|
||
|
|
|
||
|
|
if (
|
||
|
|
!template ||
|
||
|
|
template.stockKey.getOrUndefined() !== "template:application" ||
|
||
|
|
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"));
|
||
|
|
}
|
||
|
|
const fields = [];
|
||
|
|
|
||
|
|
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 } : {}),
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
return ResourceSpecification.create({
|
||
|
|
templateId: template.id.toPrimitive(),
|
||
|
|
templateName: template.values.data.name,
|
||
|
|
...(template.values.data.sections ? { sections: [...template.values.data.sections] } : {}),
|
||
|
|
fields,
|
||
|
|
});
|
||
|
|
}
|