diff --git a/src/routes/wordGenerator/wordGeneratorModel.ts b/src/routes/wordGenerator/wordGeneratorModel.ts index b2947c5..db0f2c5 100644 --- a/src/routes/wordGenerator/wordGeneratorModel.ts +++ b/src/routes/wordGenerator/wordGeneratorModel.ts @@ -45,25 +45,25 @@ const ContentSchema = z.object({ }); // Define the base schema for a section -const BaseSectionSchema = z.object({ +const SectionSchema = z.object({ + sectionId: z.string().openapi({ + description: 'A unique identifier for the section.', + }), heading: z.string().optional().openapi({ description: 'Heading of the section.', }), headingLevel: z.number().int().min(1).optional().openapi({ description: 'Level of the heading (e.g., 1 for main heading, 2 for subheading).', }), + parentSectionId: z.string().optional().openapi({ + description: + 'The unique identifier of the parent section, if this section is a child of another. Leave empty if this section has no parent.', + }), content: z.array(ContentSchema).optional().openapi({ description: 'Content contained within the section, including paragraphs, tables, etc.', }), }); -// Extend the base schema with subSections -const SectionSchema = BaseSectionSchema.extend({ - subSections: z.array(BaseSectionSchema).optional().openapi({ - description: 'Subsections within the main section.', - }), -}); - // Request Body Schema export const WordGeneratorRequestBodySchema = z.object({ title: z.string().openapi({ diff --git a/src/routes/wordGenerator/wordGeneratorRouter.ts b/src/routes/wordGenerator/wordGeneratorRouter.ts index a8551b0..8823b0a 100644 --- a/src/routes/wordGenerator/wordGeneratorRouter.ts +++ b/src/routes/wordGenerator/wordGeneratorRouter.ts @@ -393,6 +393,36 @@ const generateSectionContent = (section: any, config: any) => { return sectionContent; }; +// Function to build a hierarchical structure from a flat list of sections +const buildSectionsHierarchy = (sections: any[]) => { + const sectionMap = new Map(); + + // Create a map of sections by ID + sections.forEach((section) => { + sectionMap.set(section.sectionId, { ...section, subSections: [] }); + }); + + const rootSections: any[] = []; + + // Organize sections into a hierarchy + sections.forEach((section) => { + if (section.parentSectionId) { + // If the section has a parent, add it as a subSection + const parent = sectionMap.get(section.parentSectionId); + if (parent) { + parent.subSections.push(sectionMap.get(section.sectionId)); + } else { + console.warn(`Parent section with ID ${section.parentSectionId} not found.`); + } + } else { + // If no parent, it's a root section + rootSections.push(sectionMap.get(section.sectionId)); + } + }); + + return rootSections; +}; + async function execGenWordFuncs( data: { title: string; @@ -491,6 +521,9 @@ async function execGenWordFuncs( ); } + // Build sections hierarchy + const sectionsHierarchy = buildSectionsHierarchy(data.sections); + // Create the document based on JSON data const doc = new Document({ styles: { @@ -533,7 +566,7 @@ async function execGenWordFuncs( }), ...tableOfContentConfigs, // Generate all sections and sub-sections - ...data.sections.flatMap((section) => + ...sectionsHierarchy.flatMap((section) => generateSectionContent(section, { ...config, numberingReference: selectedNumberingOption?.reference }) ), ],