metis-desk/modules/resources/src/api/domain/template-sections.ts

50 lines
1.5 KiB
TypeScript

import type { z } from "zod/v4";
import type { TemplateSectionSchema } from "./resource-definition";
export type TemplateSectionData = z.infer<typeof TemplateSectionSchema>;
/** Secciones de un solo nivel; las referencias pertenecen exclusivamente a esta plantilla. */
export function validTemplateSections(
sections: readonly TemplateSectionData[],
fields: readonly { sectionId?: string | undefined }[]
) {
const ids = new Set(sections.map((section) => section.id.toLowerCase()));
const names = new Set(sections.map((section) => section.name.trim().toLocaleLowerCase("es")));
return (
ids.size === sections.length &&
names.size === sections.length &&
fields.every((field) => !field.sectionId || ids.has(field.sectionId.toLowerCase()))
);
}
export function freezeTemplateSections(sections: readonly TemplateSectionData[]) {
const frozen = sections.map((section) =>
Object.freeze({
id: section.id.toLowerCase(),
name: section.name.trim(),
description: section.description.trim(),
})
);
Object.freeze(frozen);
return frozen;
}
export function sameTemplateSections(
first: readonly TemplateSectionData[],
second: readonly TemplateSectionData[]
) {
return (
first.length === second.length &&
first.every((section, index) => {
const candidate = second[index];
return (
candidate !== undefined &&
section.id === candidate.id &&
section.name === candidate.name &&
section.description === candidate.description
);
})
);
}