From c711817e40ec1b67fdfdd0d8511768dd69858e53 Mon Sep 17 00:00:00 2001 From: Marco Gallegos Date: Sat, 13 Dec 2025 14:35:50 -0600 Subject: [PATCH] refactor: Reformat admin view and update survey mapping database files. --- data/survey_mappings.db-shm | Bin 32768 -> 32768 bytes data/survey_mappings.db-wal | Bin 24752 -> 0 bytes src/routes/admin.js | 10 + src/server.js | 54 ++- src/services/formbricks.js | 2 + src/views/admin.ejs | 827 +++++++++++++++++++----------------- 6 files changed, 485 insertions(+), 408 deletions(-) diff --git a/data/survey_mappings.db-shm b/data/survey_mappings.db-shm index 28f666b34fa96029e81234e57f261c11b71a9f99..fe9ac2845eca6fe6da8a63cd096d9cf9e24ece10 100644 GIT binary patch delta 89 zcmZo@U}|V!;+1%$%K!t66BkO0TChv7nNGgVi7uf4lYpuIj|Mg_jCI<$LE9Dp%F!O} delta 197 zcmZo@U}|V!s+V}A%K!q*K+MR%AixHsIf1xb(Msm6RncklxAh*GLRWfM71dqZkb7u0 zsp^47fdMO!{Eq~n!g35uP!U^QjI0|Q|1z;M M0(CKOY~NN|Cn9d_7uKs=5e&nfZNp_-}U_egH)=dpj^ z{65d~IPJH6p7;gdCD-CFkGoutxwI0$ap4vA(6y`6rJJktGe4Y1RU_ya?bRRtv59~F z?Q=f`#y@lg#_qVr?gZZtE*-dj;OzrbfsX@g6Ms&;GjVkMi}4LD8WbP^1b_e#00KY& z2mk>fu+IscbNiOkPZ6P76z`N|B_hWnY^hMD8d9BSc#fv|LRn6-l8ECtr$q2@RcePL zr2>0g6jYf~MN}6d7sHXoFoA_*sYonECOKB9Z3KMjbci@i^VMjDEvZoyfa1z{m}(0N zl2t3+$gz4{K@CYQG8-jZYC&zNjG_(*N=Xu8J)I^9o{QGw6*(GDw4w?ZkyoVrEXHJ_T_Z}> zT}Z5RRENS&56Dl0#*86oNg|{LYSoxZ1hK)lWyFS$B*bPj(N!4K9p@O17I_9!MeH?7 zVAVEu^MiM<*vPy&ie%<>nNBgu7m-R>lc^U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ced successfully" }); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}); + router.get("/environments", (req, res) => { try { const environments = getAllEnvironments(); diff --git a/src/server.js b/src/server.js index 1412f2e..748fa16 100644 --- a/src/server.js +++ b/src/server.js @@ -1,55 +1,55 @@ -require('dotenv').config(); -const express = require('express'); -const path = require('path'); -const surveyRoutes = require('./routes/surveys'); -const adminRoutes = require('./routes/admin'); -const { refreshSurveyCache } = require('./services/formbricks'); +require("dotenv").config(); +const express = require("express"); +const path = require("path"); +const surveyRoutes = require("./routes/surveys"); +const adminRoutes = require("./routes/admin"); +const { refreshSurveyCache } = require("./services/formbricks"); const app = express(); const PORT = process.env.PORT || 3011; // Template Engine Setup -app.set('view engine', 'ejs'); -app.set('views', path.join(__dirname, 'views')); +app.set("view engine", "ejs"); +app.set("views", path.join(__dirname, "views")); // Middleware app.use(express.json()); app.use(express.urlencoded({ extended: true })); // Health check endpoint -app.get('/health', (req, res) => { - res.status(200).json({ status: 'ok' }); +app.get("/health", (req, res) => { + res.status(200).json({ status: "ok" }); }); // Root landing page -app.get('/', (req, res) => { - res.render('index', { - title: 'Formbricks Survey Portal' +app.get("/", (req, res) => { + res.render("index", { + title: "Formbricks Survey Portal", }); }); // Admin UI -app.get('/admin', (req, res) => { - res.render('admin', { - title: 'Admin - Formbricks Vanity' +app.get("/admin", (req, res) => { + res.render("admin", { + title: "Admin - Formbricks Vanity", }); }); // Admin API routes -app.use('/api/mappings', adminRoutes); +app.use("/api/mappings", adminRoutes); // Main survey routes (catch-all for vanity URLs) -app.use('/', surveyRoutes); +app.use("/", surveyRoutes); // Handle 404 for any other route app.use((req, res, next) => { - res.status(404).send('Sorry, that page does not exist.'); + res.status(404).send("Sorry, that page does not exist."); }); // Global error handler app.use((err, req, res, next) => { console.error(err.stack); - res.status(500).send('Something broke!'); + res.status(500).send("Something broke!"); }); // Initialize the survey cache at startup @@ -59,7 +59,15 @@ refreshSurveyCache() console.log(`Server is running at http://localhost:${PORT}`); }); }) - .catch(error => { - console.error('Failed to initialize Formbricks survey cache. Please check API key and connection.', error); - process.exit(1); // Exit if we can't load the initial surveys + .catch((error) => { + console.error( + "Failed to initialize Formbricks survey cache. Please check API key and connection.", + error.message + ); + // process.exit(1); // Don't exit, allow the server to start even if sync fails + app.listen(PORT, () => { + console.log( + `Server is running at http://localhost:${PORT} (Survey sync failed)` + ); + }); }); diff --git a/src/services/formbricks.js b/src/services/formbricks.js index cea439b..2dbafe0 100644 --- a/src/services/formbricks.js +++ b/src/services/formbricks.js @@ -64,6 +64,7 @@ async function fetchSurveysFromAPI() { headers: { "x-api-key": process.env.FORMBRICKS_API_KEY, }, + timeout: 15000, // 15 seconds timeout } ); @@ -108,6 +109,7 @@ async function refreshSurveyCache() { console.log(`Successfully synced ${synced} surveys into the database.`); } catch (error) { console.error("Failed to refresh survey cache:", error.message); + throw error; // Re-throw so server.js knows initialization failed } } diff --git a/src/views/admin.ejs b/src/views/admin.ejs index 8f3e7af..75e9403 100644 --- a/src/views/admin.ejs +++ b/src/views/admin.ejs @@ -1,365 +1,386 @@ - - - + + + <%= title %> - - - + + + - - + +
-

Admin Authentication

-

Enter your Admin Token to manage surveys.

-
- -
- +

Admin Authentication

+

+ Enter your Admin Token to manage surveys. +

+
+ +
+