From 656e0bde02662e5d12353f21a1e703f22e5cc717 Mon Sep 17 00:00:00 2001
From: Kyle Belanger <Kyleb44@hotmail.com>
Date: Mon, 17 Feb 2025 08:00:07 -0500
Subject: [PATCH] up generate.js

condense generating the cover letter
---
 routes/generate.js | 68 ++++++++++++----------------------------------
 1 file changed, 18 insertions(+), 50 deletions(-)

diff --git a/routes/generate.js b/routes/generate.js
index 531aada..ef1810e 100644
--- a/routes/generate.js
+++ b/routes/generate.js
@@ -84,58 +84,26 @@ router.post('/download', async (req, res) => {
 });
 
 function generateCoverLetter(rawText, outputFilename) {
-  const header = rawText.match(/<header>(.*?)<\/header>/s)[1].trim();
-  const greeting = rawText.match(/<greeting>(.*?)<\/greeting>/s)[1].trim();
-  const introduction = rawText.match(/<introduction>(.*?)<\/introduction>/s)[1].trim();
-  const body = rawText.match(/<body>(.*?)<\/body>/s)[1].trim();
-  const conclusion = rawText.match(/<conclusion>(.*?)<\/conclusion>/s)[1].trim();
-  const signature = rawText.match(/<signature>(.*?)<\/signature>/s)[1].trim();
+  // Extract all sections in one go using an object
+  const sections = ['header', 'greeting', 'introduction', 'body', 'conclusion', 'signature']
+    .reduce((acc, section) => ({
+      ...acc,
+      [section]: rawText.match(new RegExp(`<${section}>(.*?)<\/${section}>`, 's'))[1].trim()
+    }), {});
 
-  // Create a new document using docx
+  // Create document with all sections
   const doc = new Document({
-    sections: [
-      {
-        properties: {},
-        children: [
-          new Paragraph({
-            children: [
-              new TextRun(header),
-              new TextRun("\n\n"), // Add line breaks between sections
-            ],
-          }),
-          new Paragraph({
-            children: [
-              new TextRun(greeting),
-              new TextRun("\n\n"),
-            ],
-          }),
-          new Paragraph({
-            children: [
-              new TextRun(introduction),
-              new TextRun("\n\n"),
-            ],
-          }),
-          new Paragraph({
-            children: [
-              new TextRun(body),
-              new TextRun("\n\n"),
-            ],
-          }),
-          new Paragraph({
-            children: [
-              new TextRun(conclusion),
-              new TextRun("\n\n"),
-            ],
-          }),
-          new Paragraph({
-            children: [
-              new TextRun(signature),
-              new TextRun("\n\n"),
-            ],
-          }),
-        ],
-      },
-    ],
+    sections: [{
+      properties: {},
+      children: Object.values(sections).map(text => 
+        new Paragraph({
+          children: [
+            new TextRun(text),
+            new TextRun("\n\n")
+          ]
+        })
+      )
+    }]
   });
 
   return Packer.toBuffer(doc);