From 2ffb3d8fb493c4c7f3dcf65d997fc008a0aa75cd Mon Sep 17 00:00:00 2001 From: Sweety3106 Date: Wed, 20 May 2026 19:17:20 +0530 Subject: [PATCH] fix: resolve column corruption in CSV downloads. --- backend/controllers/csvDownload.controller.js | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/backend/controllers/csvDownload.controller.js b/backend/controllers/csvDownload.controller.js index 5ad0b63..bfce18c 100644 --- a/backend/controllers/csvDownload.controller.js +++ b/backend/controllers/csvDownload.controller.js @@ -10,6 +10,16 @@ const { db } = require("../../database.js"); */ +function escapeCSVField(val) { + if (val === null || val === undefined) return ''; + const str = String(val); + // If the field contains quotes, commas, or newlines, escape quotes and wrap in quotes + if (/[",\r\n]/.test(str)) { + return `"${str.replace(/"/g, '""')}"`; + } + return str; +} + async function downloadData(req, res) { try { const query = ` @@ -24,21 +34,21 @@ async function downloadData(req, res) { }); }); - const rows = [ - ["Task ID", "Subject", "Title", "Due At", "Status", "Priority", "Confidence Score", "Notes"], - ...data.map(task => [ - task.id, - task.subject_name, - task.title, - task.due_at, - task.status, - task.priority, - task.confidence_score, - `"${(task.notes || '').replace(/"/g, '""')}"` - ]) - ]; - - const csvString = rows.map(row => row.join(',')).join('\n'); + const headers = ["Task ID", "Subject", "Title", "Due At", "Status", "Priority", "Confidence Score", "Notes"]; + const rows = data.map(task => [ + task.id, + task.subject_name, + task.title, + task.due_at, + task.status, + task.priority, + task.confidence_score, + task.notes + ]); + + const csvString = [headers, ...rows] + .map(row => row.map(escapeCSVField).join(',')) + .join('\n'); res.setHeader('Content-Type', 'text/csv'); res.setHeader('Content-Disposition', 'attachment; filename="study_data.csv"');