Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] Export survey to CSV #29

Merged
merged 4 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions src/app/api/export/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { NextRequest, NextResponse } from "next/server";
import { survey2csv } from "@/lib/firebase/export/exportSurvey";

export async function POST(req: NextRequest) {
const body = await req.json();
try {
const csv = await survey2csv(body.surveyIds);
return NextResponse.json(csv, {status: 200});
} catch (err) {
return NextResponse.json({error: 'error creating csv file'}, {status: 500});
}
}
73 changes: 73 additions & 0 deletions src/lib/firebase/export/exportSurvey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import os from "os";
import { db } from "../firebaseConfig";
import { collection, query, where, getDocs } from "firebase/firestore";

type flattenDoc = {
[key: string]: any
}

export async function survey2csv(surveyIds: string[]) {
const csv = [];
const surveyCollection = collection(db, "surveys");
const q = query(surveyCollection, where("formId", "in", surveyIds));
const snapshot = await getDocs(q);

// Define CSV fields and fix its order
const flattenDoc = flattenDocument(snapshot.docs[0].data(), []);
const fields = Object.keys(flattenDoc);
const idx = fields.indexOf("formId");
if (idx != 0) {
[fields[0], fields[idx]] = [fields[idx], fields[0]];
}
csv.push(fields.join(","));

// Fix the order of fields of map in items field
let itemsOrder: string[];
const items = snapshot.docs[0].data().items;
if (items.length > 0) {
const flattenItem = flattenDocument(items[0], []);
itemsOrder = Object.keys(flattenItem);
}

// For each document, create each row
snapshot.docs.map((doc) => {
const surveyDoc = doc.data();
const surveyDocFlatten = flattenDocument(surveyDoc, itemsOrder);
const survey = fields.map(field => {
return surveyDocFlatten[field];
});
csv.push(survey.join(","));
});

return csv.join(os.EOL);
}

function flattenDocument(obj: object, order: string[], prefix = '') {
let result: flattenDoc = {};

for (const [key, value] of Object.entries(obj)) {
const newKey = prefix ? `${prefix}.${key}` : key;
if (value !== null && typeof value === 'object' && !Array.isArray(value)) {
Object.assign(result, flattenDocument(value, order, newKey));
} else if (Array.isArray(value)) {
result[newKey] = value.map((item) => {
if (typeof item === 'object') {
const flatten = flattenDocument(item, order, '');
const orderRevised: flattenDoc = {};
order.forEach((field) => {
if (field in flatten) {
orderRevised[field] = flatten[field];
}
});
return JSON.stringify(orderRevised).replace(",", "; ");
} else {
return item.toString();
}
}).join(' | ');
} else {
result[newKey] = value;
}
}

return result;
}
Loading