Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions backend/controllers/csvDownload.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand All @@ -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"');
Expand Down