From 24af3ca40310aebb484b0da3656bab4b8168c7fc Mon Sep 17 00:00:00 2001 From: Blake Matthes Date: Mon, 15 Jun 2026 17:16:00 -0500 Subject: [PATCH] fix(datamart): somereason it stopped working.. and added download types there was a weird issue with the req.query that cause nothing to pull and make the excel files lag..... now excel macros are using the csv pull from here and added in the xlsx to download to bc why not makes it easier for later and can have bbuttons for every thing in lst too :D --- backend/datamart/getDatamart.route.ts | 65 ++++++++++++++++++++++++++- backend/routeHandler.routes.ts | 2 +- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/backend/datamart/getDatamart.route.ts b/backend/datamart/getDatamart.route.ts index b42cdfb..3b3833f 100644 --- a/backend/datamart/getDatamart.route.ts +++ b/backend/datamart/getDatamart.route.ts @@ -1,4 +1,5 @@ import { Router } from "express"; +import * as XLSX from "xlsx"; import { apiReturn } from "../utils/returnHelper.utils.js"; import { runDatamartQuery } from "./datamart.controller.js"; @@ -7,13 +8,73 @@ const r = Router(); type Options = { name: string; value: string; + format: string; }; r.get("/:name", async (req, res) => { const { name } = req.params; - const options = req.query as Options; - + const options = { ...req.query } as Options; const dataRan = await runDatamartQuery({ name, options }); + + if (!dataRan.success) { + return apiReturn(res, { + success: false, + level: "error", + module: "datamart", + subModule: "query", + message: dataRan.message, + status: 500, + }); + } + + // XLSX Export + if (options.format?.toLowerCase() === "xlsx") { + const wb = XLSX.utils.book_new(); + + const ws = XLSX.utils.json_to_sheet(dataRan.data); + + XLSX.utils.book_append_sheet(wb, ws, name); + + const buffer = XLSX.write(wb, { + type: "buffer", + bookType: "xlsx", + }); + + res.setHeader( + "Content-Type", + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + ); + + res.setHeader("Content-Disposition", `attachment; filename="${name}.xlsx"`); + + return res.send(buffer); + } + + // CSV Export + if (options.format?.toLowerCase() === "csv") { + const rows = dataRan.data as any; + + if (!rows.length) { + return res.status(200).send(""); + } + + const headers = Object.keys(rows[0]); + + const csv = [ + headers.join(","), + ...rows.map((row: any) => + headers + .map((h) => `"${String(row[h] ?? "").replace(/"/g, '""')}"`) + .join(","), + ), + ].join("\r\n"); + + res.setHeader("Content-Type", "text/csv"); + res.setHeader("Content-Disposition", `attachment; filename="${name}.csv"`); + + return res.send(csv); + } + return apiReturn(res, { success: dataRan.success, level: "info", diff --git a/backend/routeHandler.routes.ts b/backend/routeHandler.routes.ts index 5da4a99..520830d 100644 --- a/backend/routeHandler.routes.ts +++ b/backend/routeHandler.routes.ts @@ -19,13 +19,13 @@ import { setupUtilsRoutes } from "./utils/utils.routes.js"; export const setupRoutes = (baseUrl: string, app: Express) => { //routes that are on by default + setupDatamartRoutes(baseUrl, app); setupMobileRoutes(baseUrl, app); setupSystemRoutes(baseUrl, app); setupAdminRoutes(baseUrl, app); setupApiDocsRoutes(baseUrl, app); setupProdSqlRoutes(baseUrl, app); setupGPSqlRoutes(baseUrl, app); - setupDatamartRoutes(baseUrl, app); setupAuthRoutes(baseUrl, app); setupUtilsRoutes(baseUrl, app); setupOpendockRoutes(baseUrl, app);